1
0

util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
  4. * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
  5. */
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "lkc.h"
  10. /* file already present in list? If not add it */
  11. struct file *file_lookup(const char *name)
  12. {
  13. struct file *file;
  14. for (file = file_list; file; file = file->next) {
  15. if (!strcmp(name, file->name)) {
  16. return file;
  17. }
  18. }
  19. file = xmalloc(sizeof(*file));
  20. memset(file, 0, sizeof(*file));
  21. file->name = xstrdup(name);
  22. file->next = file_list;
  23. file_list = file;
  24. return file;
  25. }
  26. /* Allocate initial growable string */
  27. struct gstr str_new(void)
  28. {
  29. struct gstr gs;
  30. gs.s = xmalloc(sizeof(char) * 64);
  31. gs.len = 64;
  32. gs.max_width = 0;
  33. strcpy(gs.s, "\0");
  34. return gs;
  35. }
  36. /* Free storage for growable string */
  37. void str_free(struct gstr *gs)
  38. {
  39. if (gs->s)
  40. free(gs->s);
  41. gs->s = NULL;
  42. gs->len = 0;
  43. }
  44. /* Append to growable string */
  45. void str_append(struct gstr *gs, const char *s)
  46. {
  47. size_t l;
  48. if (s) {
  49. l = strlen(gs->s) + strlen(s) + 1;
  50. if (l > gs->len) {
  51. gs->s = xrealloc(gs->s, l);
  52. gs->len = l;
  53. }
  54. strcat(gs->s, s);
  55. }
  56. }
  57. /* Append printf formatted string to growable string */
  58. void str_printf(struct gstr *gs, const char *fmt, ...)
  59. {
  60. va_list ap;
  61. char s[10000]; /* big enough... */
  62. va_start(ap, fmt);
  63. vsnprintf(s, sizeof(s), fmt, ap);
  64. str_append(gs, s);
  65. va_end(ap);
  66. }
  67. /* Retrieve value of growable string */
  68. const char *str_get(struct gstr *gs)
  69. {
  70. return gs->s;
  71. }
  72. void *xmalloc(size_t size)
  73. {
  74. void *p = malloc(size);
  75. if (p)
  76. return p;
  77. fprintf(stderr, "Out of memory.\n");
  78. exit(1);
  79. }
  80. void *xcalloc(size_t nmemb, size_t size)
  81. {
  82. void *p = calloc(nmemb, size);
  83. if (p)
  84. return p;
  85. fprintf(stderr, "Out of memory.\n");
  86. exit(1);
  87. }
  88. void *xrealloc(void *p, size_t size)
  89. {
  90. p = realloc(p, size);
  91. if (p)
  92. return p;
  93. fprintf(stderr, "Out of memory.\n");
  94. exit(1);
  95. }
  96. char *xstrdup(const char *s)
  97. {
  98. char *p;
  99. p = strdup(s);
  100. if (p)
  101. return p;
  102. fprintf(stderr, "Out of memory.\n");
  103. exit(1);
  104. }
  105. char *xstrndup(const char *s, size_t n)
  106. {
  107. char *p;
  108. p = strndup(s, n);
  109. if (p)
  110. return p;
  111. fprintf(stderr, "Out of memory.\n");
  112. exit(1);
  113. }