Linux 4.19.133
[linux/fpc-iii.git] / scripts / kconfig / util.c
blobd999683bb2a778ae05841531abad3fc3e323d4a0
1 /*
2 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
3 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
5 * Released under the terms of the GNU GPL v2.0.
6 */
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "lkc.h"
13 /* file already present in list? If not add it */
14 struct file *file_lookup(const char *name)
16 struct file *file;
18 for (file = file_list; file; file = file->next) {
19 if (!strcmp(name, file->name)) {
20 return file;
24 file = xmalloc(sizeof(*file));
25 memset(file, 0, sizeof(*file));
26 file->name = xstrdup(name);
27 file->next = file_list;
28 file_list = file;
29 return file;
32 /* Allocate initial growable string */
33 struct gstr str_new(void)
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;
43 /* Free storage for growable string */
44 void str_free(struct gstr *gs)
46 if (gs->s)
47 free(gs->s);
48 gs->s = NULL;
49 gs->len = 0;
52 /* Append to growable string */
53 void str_append(struct gstr *gs, const char *s)
55 size_t l;
56 if (s) {
57 l = strlen(gs->s) + strlen(s) + 1;
58 if (l > gs->len) {
59 gs->s = xrealloc(gs->s, l);
60 gs->len = l;
62 strcat(gs->s, s);
66 /* Append printf formatted string to growable string */
67 void str_printf(struct gstr *gs, const char *fmt, ...)
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);
77 /* Retrieve value of growable string */
78 const char *str_get(struct gstr *gs)
80 return gs->s;
83 void *xmalloc(size_t size)
85 void *p = malloc(size);
86 if (p)
87 return p;
88 fprintf(stderr, "Out of memory.\n");
89 exit(1);
92 void *xcalloc(size_t nmemb, size_t size)
94 void *p = calloc(nmemb, size);
95 if (p)
96 return p;
97 fprintf(stderr, "Out of memory.\n");
98 exit(1);
101 void *xrealloc(void *p, size_t size)
103 p = realloc(p, size);
104 if (p)
105 return p;
106 fprintf(stderr, "Out of memory.\n");
107 exit(1);
110 char *xstrdup(const char *s)
112 char *p;
114 p = strdup(s);
115 if (p)
116 return p;
117 fprintf(stderr, "Out of memory.\n");
118 exit(1);
121 char *xstrndup(const char *s, size_t n)
123 char *p;
125 p = strndup(s, n);
126 if (p)
127 return p;
128 fprintf(stderr, "Out of memory.\n");
129 exit(1);