util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /* Allocate initial growable string */
  28. struct gstr str_new(void)
  29. {
  30. struct gstr gs;
  31. gs.s = xmalloc(sizeof(char) * 64);
  32. gs.len = 64;
  33. gs.max_width = 0;
  34. strcpy(gs.s, "\0");
  35. return gs;
  36. }
  37. /* Free storage for growable string */
  38. void str_free(struct gstr *gs)
  39. {
  40. if (gs->s)
  41. free(gs->s);
  42. gs->s = NULL;
  43. gs->len = 0;
  44. }
  45. /* Append to growable string */
  46. void str_append(struct gstr *gs, const char *s)
  47. {
  48. size_t l;
  49. if (s) {
  50. l = strlen(gs->s) + strlen(s) + 1;
  51. if (l > gs->len) {
  52. gs->s = xrealloc(gs->s, l);
  53. gs->len = l;
  54. }
  55. strcat(gs->s, s);
  56. }
  57. }
  58. /* Append printf formatted string to growable string */
  59. void str_printf(struct gstr *gs, const char *fmt, ...)
  60. {
  61. va_list ap;
  62. char s[10000]; /* big enough... */
  63. va_start(ap, fmt);
  64. vsnprintf(s, sizeof(s), fmt, ap);
  65. str_append(gs, s);
  66. va_end(ap);
  67. }
  68. /* Retrieve value of growable string */
  69. const char *str_get(struct gstr *gs)
  70. {
  71. return gs->s;
  72. }
  73. void *xmalloc(size_t size)
  74. {
  75. void *p = malloc(size);
  76. if (p)
  77. return p;
  78. fprintf(stderr, "Out of memory.\n");
  79. exit(1);
  80. }
  81. void *xcalloc(size_t nmemb, size_t size)
  82. {
  83. void *p = calloc(nmemb, size);
  84. if (p)
  85. return p;
  86. fprintf(stderr, "Out of memory.\n");
  87. exit(1);
  88. }
  89. void *xrealloc(void *p, size_t size)
  90. {
  91. p = realloc(p, size);
  92. if (p)
  93. return p;
  94. fprintf(stderr, "Out of memory.\n");
  95. exit(1);
  96. }
  97. char *xstrdup(const char *s)
  98. {
  99. char *p;
  100. p = strdup(s);
  101. if (p)
  102. return p;
  103. fprintf(stderr, "Out of memory.\n");
  104. exit(1);
  105. }
  106. char *xstrndup(const char *s, size_t n)
  107. {
  108. char *p;
  109. p = strndup(s, n);
  110. if (p)
  111. return p;
  112. fprintf(stderr, "Out of memory.\n");
  113. exit(1);
  114. }