util.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lkc.h"
  11. /* file already present in list? If not add it */
  12. struct file *file_lookup(const char *name)
  13. {
  14. struct file *file;
  15. const char *file_name = sym_expand_string_value(name);
  16. for (file = file_list; file; file = file->next) {
  17. if (!strcmp(name, file->name)) {
  18. free((void *)file_name);
  19. return file;
  20. }
  21. }
  22. file = xmalloc(sizeof(*file));
  23. memset(file, 0, sizeof(*file));
  24. file->name = file_name;
  25. file->next = file_list;
  26. file_list = file;
  27. return file;
  28. }
  29. /* write a dependency file as used by kbuild to track dependencies */
  30. int file_write_dep(const char *name)
  31. {
  32. struct symbol *sym, *env_sym;
  33. struct expr *e;
  34. struct file *file;
  35. char tmpf[PATH_MAX+1];
  36. FILE *out;
  37. if (!name)
  38. name = ".kconfig.d";
  39. strcpy(tmpf, name);
  40. dir_name(tmpf);
  41. strcat(tmpf, "..config.tmp");
  42. out = fopen(tmpf, "w");
  43. if (!out)
  44. return 1;
  45. fprintf(out, "deps_config := \\\n");
  46. for (file = file_list; file; file = file->next) {
  47. if (file->next)
  48. fprintf(out, "\t%s \\\n", file->name);
  49. else
  50. fprintf(out, "\t%s\n", file->name);
  51. }
  52. fprintf(out, "\n%s: \\\n"
  53. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  54. expr_list_for_each_sym(sym_env_list, e, sym) {
  55. struct property *prop;
  56. const char *value;
  57. prop = sym_get_env_prop(sym);
  58. env_sym = prop_get_symbol(prop);
  59. if (!env_sym)
  60. continue;
  61. value = getenv(env_sym->name);
  62. if (!value)
  63. value = "";
  64. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  65. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  66. fprintf(out, "endif\n");
  67. }
  68. fprintf(out, "\n$(deps_config): ;\n");
  69. fclose(out);
  70. rename(tmpf, name);
  71. return 0;
  72. }
  73. /* Allocate initial growable string */
  74. struct gstr str_new(void)
  75. {
  76. struct gstr gs;
  77. gs.s = xmalloc(sizeof(char) * 64);
  78. gs.len = 64;
  79. gs.max_width = 0;
  80. strcpy(gs.s, "\0");
  81. return gs;
  82. }
  83. /* Allocate and assign growable string */
  84. struct gstr str_assign(const char *s)
  85. {
  86. struct gstr gs;
  87. gs.s = strdup(s);
  88. gs.len = strlen(s) + 1;
  89. gs.max_width = 0;
  90. return gs;
  91. }
  92. /* Free storage for growable string */
  93. void str_free(struct gstr *gs)
  94. {
  95. if (gs->s)
  96. free(gs->s);
  97. gs->s = NULL;
  98. gs->len = 0;
  99. }
  100. /* Append to growable string */
  101. void str_append(struct gstr *gs, const char *s)
  102. {
  103. size_t l;
  104. if (s) {
  105. l = strlen(gs->s) + strlen(s) + 1;
  106. if (l > gs->len) {
  107. gs->s = realloc(gs->s, l);
  108. gs->len = l;
  109. }
  110. strcat(gs->s, s);
  111. }
  112. }
  113. /* Append printf formatted string to growable string */
  114. void str_printf(struct gstr *gs, const char *fmt, ...)
  115. {
  116. va_list ap;
  117. char s[10000]; /* big enough... */
  118. va_start(ap, fmt);
  119. vsnprintf(s, sizeof(s), fmt, ap);
  120. str_append(gs, s);
  121. va_end(ap);
  122. }
  123. /* Retrieve value of growable string */
  124. const char *str_get(struct gstr *gs)
  125. {
  126. return gs->s;
  127. }
  128. void *xmalloc(size_t size)
  129. {
  130. void *p = malloc(size);
  131. if (p)
  132. return p;
  133. fprintf(stderr, "Out of memory.\n");
  134. exit(1);
  135. }
  136. void *xcalloc(size_t nmemb, size_t size)
  137. {
  138. void *p = calloc(nmemb, size);
  139. if (p)
  140. return p;
  141. fprintf(stderr, "Out of memory.\n");
  142. exit(1);
  143. }
  144. /* basename, dirname - parse pathname components */
  145. char *dir_name(char *path)
  146. {
  147. char *slash = strrchr(path, '/');
  148. int size = 0;
  149. if (slash)
  150. size = slash - path + 1;
  151. path[size] = 0;
  152. return path;
  153. }
  154. char *base_name(char *path)
  155. {
  156. char *slash = strrchr(path, '/');
  157. if (slash)
  158. path += slash - path + 1;
  159. return path;
  160. }