util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
  3. * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
  4. *
  5. * Released under the terms of the GNU GPL v2.0.
  6. */
  7. #include <string.h>
  8. #include "lkc.h"
  9. /* file already present in list? If not add it */
  10. struct file *file_lookup(const char *name)
  11. {
  12. struct file *file;
  13. for (file = file_list; file; file = file->next) {
  14. if (!strcmp(name, file->name))
  15. return file;
  16. }
  17. file = malloc(sizeof(*file));
  18. memset(file, 0, sizeof(*file));
  19. file->name = strdup(name);
  20. file->next = file_list;
  21. file_list = file;
  22. return file;
  23. }
  24. /* write a dependency file as used by kbuild to track dependencies */
  25. int file_write_dep(const char *name)
  26. {
  27. struct file *file;
  28. FILE *out;
  29. if (!name)
  30. name = ".kconfig.d";
  31. out = fopen("..config.tmp", "w");
  32. if (!out)
  33. return 1;
  34. fprintf(out, "deps_config := \\\n");
  35. for (file = file_list; file; file = file->next) {
  36. if (file->next)
  37. fprintf(out, "\t%s \\\n", file->name);
  38. else
  39. fprintf(out, "\t%s\n", file->name);
  40. }
  41. fprintf(out, "\ninclude/config/auto.conf: \\\n"
  42. "\t$(deps_config)\n\n"
  43. "$(deps_config): ;\n");
  44. fclose(out);
  45. rename("..config.tmp", name);
  46. return 0;
  47. }
  48. /* Allocate initial growable sting */
  49. struct gstr str_new(void)
  50. {
  51. struct gstr gs;
  52. gs.s = malloc(sizeof(char) * 64);
  53. gs.len = 16;
  54. strcpy(gs.s, "\0");
  55. return gs;
  56. }
  57. /* Allocate and assign growable string */
  58. struct gstr str_assign(const char *s)
  59. {
  60. struct gstr gs;
  61. gs.s = strdup(s);
  62. gs.len = strlen(s) + 1;
  63. return gs;
  64. }
  65. /* Free storage for growable string */
  66. void str_free(struct gstr *gs)
  67. {
  68. if (gs->s)
  69. free(gs->s);
  70. gs->s = NULL;
  71. gs->len = 0;
  72. }
  73. /* Append to growable string */
  74. void str_append(struct gstr *gs, const char *s)
  75. {
  76. size_t l = strlen(gs->s) + strlen(s) + 1;
  77. if (l > gs->len) {
  78. gs->s = realloc(gs->s, l);
  79. gs->len = l;
  80. }
  81. strcat(gs->s, s);
  82. }
  83. /* Append printf formatted string to growable string */
  84. void str_printf(struct gstr *gs, const char *fmt, ...)
  85. {
  86. va_list ap;
  87. char s[10000]; /* big enough... */
  88. va_start(ap, fmt);
  89. vsnprintf(s, sizeof(s), fmt, ap);
  90. str_append(gs, s);
  91. va_end(ap);
  92. }
  93. /* Retrieve value of growable string */
  94. const char *str_get(struct gstr *gs)
  95. {
  96. return gs->s;
  97. }