util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. for (file = file_list; file; file = file->next) {
  16. if (!strcmp(name, file->name)) {
  17. return file;
  18. }
  19. }
  20. file = xmalloc(sizeof(*file));
  21. memset(file, 0, sizeof(*file));
  22. file->name = xstrdup(name);
  23. file->next = file_list;
  24. file_list = file;
  25. return file;
  26. }
  27. /* write a dependency file as used by kbuild to track dependencies */
  28. int file_write_dep(const char *name)
  29. {
  30. struct file *file;
  31. FILE *out;
  32. if (!name)
  33. name = ".kconfig.d";
  34. out = fopen("..config.tmp", "w");
  35. if (!out)
  36. return 1;
  37. fprintf(out, "deps_config := \\\n");
  38. for (file = file_list; file; file = file->next) {
  39. if (file->next)
  40. fprintf(out, "\t%s \\\n", file->name);
  41. else
  42. fprintf(out, "\t%s\n", file->name);
  43. }
  44. fprintf(out, "\n%s: \\\n"
  45. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  46. env_write_dep(out, conf_get_autoconfig_name());
  47. fprintf(out, "\n$(deps_config): ;\n");
  48. fclose(out);
  49. rename("..config.tmp", name);
  50. return 0;
  51. }
  52. /* Allocate initial growable string */
  53. struct gstr str_new(void)
  54. {
  55. struct gstr gs;
  56. gs.s = xmalloc(sizeof(char) * 64);
  57. gs.len = 64;
  58. gs.max_width = 0;
  59. strcpy(gs.s, "\0");
  60. return gs;
  61. }
  62. /* Free storage for growable string */
  63. void str_free(struct gstr *gs)
  64. {
  65. if (gs->s)
  66. free(gs->s);
  67. gs->s = NULL;
  68. gs->len = 0;
  69. }
  70. /* Append to growable string */
  71. void str_append(struct gstr *gs, const char *s)
  72. {
  73. size_t l;
  74. if (s) {
  75. l = strlen(gs->s) + strlen(s) + 1;
  76. if (l > gs->len) {
  77. gs->s = xrealloc(gs->s, l);
  78. gs->len = l;
  79. }
  80. strcat(gs->s, s);
  81. }
  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. }
  98. void *xmalloc(size_t size)
  99. {
  100. void *p = malloc(size);
  101. if (p)
  102. return p;
  103. fprintf(stderr, "Out of memory.\n");
  104. exit(1);
  105. }
  106. void *xcalloc(size_t nmemb, size_t size)
  107. {
  108. void *p = calloc(nmemb, size);
  109. if (p)
  110. return p;
  111. fprintf(stderr, "Out of memory.\n");
  112. exit(1);
  113. }
  114. void *xrealloc(void *p, size_t size)
  115. {
  116. p = realloc(p, size);
  117. if (p)
  118. return p;
  119. fprintf(stderr, "Out of memory.\n");
  120. exit(1);
  121. }
  122. char *xstrdup(const char *s)
  123. {
  124. char *p;
  125. p = strdup(s);
  126. if (p)
  127. return p;
  128. fprintf(stderr, "Out of memory.\n");
  129. exit(1);
  130. }
  131. char *xstrndup(const char *s, size_t n)
  132. {
  133. char *p;
  134. p = strndup(s, n);
  135. if (p)
  136. return p;
  137. fprintf(stderr, "Out of memory.\n");
  138. exit(1);
  139. }