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