blob: d999683bb2a778ae05841531abad3fc3e323d4a0 [file] [log] [blame]
Masahiro Yamada0a9064f2014-07-30 14:08:13 +09001/*
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 */
14struct file *file_lookup(const char *name)
15{
16 struct file *file;
Masahiro Yamada0a9064f2014-07-30 14:08:13 +090017
18 for (file = file_list; file; file = file->next) {
19 if (!strcmp(name, file->name)) {
Masahiro Yamada0a9064f2014-07-30 14:08:13 +090020 return file;
21 }
22 }
23
24 file = xmalloc(sizeof(*file));
25 memset(file, 0, sizeof(*file));
Tom Rini5972ff02020-03-11 18:11:17 -040026 file->name = xstrdup(name);
Masahiro Yamada0a9064f2014-07-30 14:08:13 +090027 file->next = file_list;
28 file_list = file;
29 return file;
30}
31
Masahiro Yamada0a9064f2014-07-30 14:08:13 +090032/* Allocate initial growable string */
33struct 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 Yamada0a9064f2014-07-30 14:08:13 +090043/* Free storage for growable string */
44void 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 */
53void 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 Roscae91610d2018-05-19 14:13:50 +020059 gs->s = xrealloc(gs->s, l);
Masahiro Yamada0a9064f2014-07-30 14:08:13 +090060 gs->len = l;
61 }
62 strcat(gs->s, s);
63 }
64}
65
66/* Append printf formatted string to growable string */
67void 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 */
78const char *str_get(struct gstr *gs)
79{
80 return gs->s;
81}
82
83void *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
92void *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 Roscae91610d2018-05-19 14:13:50 +0200100
101void *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
110char *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 Rini5972ff02020-03-11 18:11:17 -0400120
121char *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}