Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 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 | |
| 8 | #include <stdarg.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include "lkc.h" |
| 12 | |
| 13 | /* file already present in list? If not add it */ |
| 14 | struct file *file_lookup(const char *name) |
| 15 | { |
| 16 | struct file *file; |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 17 | |
| 18 | for (file = file_list; file; file = file->next) { |
| 19 | if (!strcmp(name, file->name)) { |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 20 | return file; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | file = xmalloc(sizeof(*file)); |
| 25 | memset(file, 0, sizeof(*file)); |
Tom Rini | 5972ff0 | 2020-03-11 18:11:17 -0400 | [diff] [blame] | 26 | file->name = xstrdup(name); |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 27 | file->next = file_list; |
| 28 | file_list = file; |
| 29 | return file; |
| 30 | } |
| 31 | |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 32 | /* Allocate initial growable string */ |
| 33 | struct gstr str_new(void) |
| 34 | { |
| 35 | struct gstr gs; |
| 36 | gs.s = xmalloc(sizeof(char) * 64); |
| 37 | gs.len = 64; |
| 38 | gs.max_width = 0; |
| 39 | strcpy(gs.s, "\0"); |
| 40 | return gs; |
| 41 | } |
| 42 | |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 43 | /* Free storage for growable string */ |
| 44 | void str_free(struct gstr *gs) |
| 45 | { |
| 46 | if (gs->s) |
| 47 | free(gs->s); |
| 48 | gs->s = NULL; |
| 49 | gs->len = 0; |
| 50 | } |
| 51 | |
| 52 | /* Append to growable string */ |
| 53 | void str_append(struct gstr *gs, const char *s) |
| 54 | { |
| 55 | size_t l; |
| 56 | if (s) { |
| 57 | l = strlen(gs->s) + strlen(s) + 1; |
| 58 | if (l > gs->len) { |
Eugeniu Rosca | e91610d | 2018-05-19 14:13:50 +0200 | [diff] [blame] | 59 | gs->s = xrealloc(gs->s, l); |
Masahiro Yamada | 0a9064f | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 60 | gs->len = l; |
| 61 | } |
| 62 | strcat(gs->s, s); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* Append printf formatted string to growable string */ |
| 67 | void str_printf(struct gstr *gs, const char *fmt, ...) |
| 68 | { |
| 69 | va_list ap; |
| 70 | char s[10000]; /* big enough... */ |
| 71 | va_start(ap, fmt); |
| 72 | vsnprintf(s, sizeof(s), fmt, ap); |
| 73 | str_append(gs, s); |
| 74 | va_end(ap); |
| 75 | } |
| 76 | |
| 77 | /* Retrieve value of growable string */ |
| 78 | const char *str_get(struct gstr *gs) |
| 79 | { |
| 80 | return gs->s; |
| 81 | } |
| 82 | |
| 83 | void *xmalloc(size_t size) |
| 84 | { |
| 85 | void *p = malloc(size); |
| 86 | if (p) |
| 87 | return p; |
| 88 | fprintf(stderr, "Out of memory.\n"); |
| 89 | exit(1); |
| 90 | } |
| 91 | |
| 92 | void *xcalloc(size_t nmemb, size_t size) |
| 93 | { |
| 94 | void *p = calloc(nmemb, size); |
| 95 | if (p) |
| 96 | return p; |
| 97 | fprintf(stderr, "Out of memory.\n"); |
| 98 | exit(1); |
| 99 | } |
Eugeniu Rosca | e91610d | 2018-05-19 14:13:50 +0200 | [diff] [blame] | 100 | |
| 101 | void *xrealloc(void *p, size_t size) |
| 102 | { |
| 103 | p = realloc(p, size); |
| 104 | if (p) |
| 105 | return p; |
| 106 | fprintf(stderr, "Out of memory.\n"); |
| 107 | exit(1); |
| 108 | } |
| 109 | |
| 110 | char *xstrdup(const char *s) |
| 111 | { |
| 112 | char *p; |
| 113 | |
| 114 | p = strdup(s); |
| 115 | if (p) |
| 116 | return p; |
| 117 | fprintf(stderr, "Out of memory.\n"); |
| 118 | exit(1); |
| 119 | } |
Tom Rini | 5972ff0 | 2020-03-11 18:11:17 -0400 | [diff] [blame] | 120 | |
| 121 | char *xstrndup(const char *s, size_t n) |
| 122 | { |
| 123 | char *p; |
| 124 | |
| 125 | p = strndup(s, n); |
| 126 | if (p) |
| 127 | return p; |
| 128 | fprintf(stderr, "Out of memory.\n"); |
| 129 | exit(1); |
| 130 | } |