3
0

util.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, "\n.config include/autoconf.h: $(deps_config)\n\n$(deps_config):\n");
  42. fclose(out);
  43. rename("..config.tmp", name);
  44. return 0;
  45. }
  46. /* Allocate initial growable sting */
  47. struct gstr str_new(void)
  48. {
  49. struct gstr gs;
  50. gs.s = malloc(sizeof(char) * 64);
  51. gs.len = 16;
  52. strcpy(gs.s, "\0");
  53. return gs;
  54. }
  55. /* Allocate and assign growable string */
  56. struct gstr str_assign(const char *s)
  57. {
  58. struct gstr gs;
  59. gs.s = strdup(s);
  60. gs.len = strlen(s) + 1;
  61. return gs;
  62. }
  63. /* Free storage for growable string */
  64. void str_free(struct gstr *gs)
  65. {
  66. if (gs->s)
  67. free(gs->s);
  68. gs->s = NULL;
  69. gs->len = 0;
  70. }
  71. /* Append to growable string */
  72. void str_append(struct gstr *gs, const char *s)
  73. {
  74. size_t l = strlen(gs->s) + strlen(s) + 1;
  75. if (l > gs->len) {
  76. gs->s = realloc(gs->s, l);
  77. gs->len = l;
  78. }
  79. strcat(gs->s, s);
  80. }
  81. /* Append printf formatted string to growable string */
  82. void str_printf(struct gstr *gs, const char *fmt, ...)
  83. {
  84. va_list ap;
  85. char s[10000]; /* big enough... */
  86. va_start(ap, fmt);
  87. vsnprintf(s, sizeof(s), fmt, ap);
  88. str_append(gs, s);
  89. va_end(ap);
  90. }
  91. /* Retrieve value of growable string */
  92. const char *str_get(struct gstr *gs)
  93. {
  94. return gs->s;
  95. }