1
0

util.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 symbol *sym, *env_sym;
  28. struct expr *e;
  29. struct file *file;
  30. FILE *out;
  31. if (!name)
  32. name = ".kconfig.d";
  33. out = fopen("..config.tmp", "w");
  34. if (!out)
  35. return 1;
  36. fprintf(out, "deps_config := \\\n");
  37. for (file = file_list; file; file = file->next) {
  38. if (file->next)
  39. fprintf(out, "\t%s \\\n", file->name);
  40. else
  41. fprintf(out, "\t%s\n", file->name);
  42. }
  43. fprintf(out, "\n%s: \\\n"
  44. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  45. expr_list_for_each_sym(sym_env_list, e, sym) {
  46. struct property *prop;
  47. const char *value;
  48. prop = sym_get_env_prop(sym);
  49. env_sym = prop_get_symbol(prop);
  50. if (!env_sym)
  51. continue;
  52. value = getenv(env_sym->name);
  53. if (!value)
  54. value = "";
  55. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  56. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  57. fprintf(out, "endif\n");
  58. }
  59. fprintf(out, "\n$(deps_config): ;\n");
  60. fclose(out);
  61. rename("..config.tmp", name);
  62. return 0;
  63. }
  64. /* Allocate initial growable sting */
  65. struct gstr str_new(void)
  66. {
  67. struct gstr gs;
  68. gs.s = malloc(sizeof(char) * 64);
  69. gs.len = 64;
  70. strcpy(gs.s, "\0");
  71. return gs;
  72. }
  73. /* Allocate and assign growable string */
  74. struct gstr str_assign(const char *s)
  75. {
  76. struct gstr gs;
  77. gs.s = strdup(s);
  78. gs.len = strlen(s) + 1;
  79. return gs;
  80. }
  81. /* Free storage for growable string */
  82. void str_free(struct gstr *gs)
  83. {
  84. if (gs->s)
  85. free(gs->s);
  86. gs->s = NULL;
  87. gs->len = 0;
  88. }
  89. /* Append to growable string */
  90. void str_append(struct gstr *gs, const char *s)
  91. {
  92. size_t l;
  93. if (s) {
  94. l = strlen(gs->s) + strlen(s) + 1;
  95. if (l > gs->len) {
  96. gs->s = realloc(gs->s, l);
  97. gs->len = l;
  98. }
  99. strcat(gs->s, s);
  100. }
  101. }
  102. /* Append printf formatted string to growable string */
  103. void str_printf(struct gstr *gs, const char *fmt, ...)
  104. {
  105. va_list ap;
  106. char s[10000]; /* big enough... */
  107. va_start(ap, fmt);
  108. vsnprintf(s, sizeof(s), fmt, ap);
  109. str_append(gs, s);
  110. va_end(ap);
  111. }
  112. /* Retrieve value of growable string */
  113. const char *str_get(struct gstr *gs)
  114. {
  115. return gs->s;
  116. }