Linux 4.18.10
[linux/fpc-iii.git] / scripts / kconfig / util.c
bloba365594770d9daaaaf3220c80eb1522113930072
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 /* write a dependency file as used by kbuild to track dependencies */
33 int file_write_dep(const char *name)
35 struct file *file;
36 FILE *out;
38 if (!name)
39 name = ".kconfig.d";
40 out = fopen("..config.tmp", "w");
41 if (!out)
42 return 1;
43 fprintf(out, "deps_config := \\\n");
44 for (file = file_list; file; file = file->next) {
45 if (file->next)
46 fprintf(out, "\t%s \\\n", file->name);
47 else
48 fprintf(out, "\t%s\n", file->name);
50 fprintf(out, "\n%s: \\\n"
51 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
53 env_write_dep(out, conf_get_autoconfig_name());
55 fprintf(out, "\n$(deps_config): ;\n");
56 fclose(out);
57 rename("..config.tmp", name);
58 return 0;
62 /* Allocate initial growable string */
63 struct gstr str_new(void)
65 struct gstr gs;
66 gs.s = xmalloc(sizeof(char) * 64);
67 gs.len = 64;
68 gs.max_width = 0;
69 strcpy(gs.s, "\0");
70 return gs;
73 /* Free storage for growable string */
74 void str_free(struct gstr *gs)
76 if (gs->s)
77 free(gs->s);
78 gs->s = NULL;
79 gs->len = 0;
82 /* Append to growable string */
83 void str_append(struct gstr *gs, const char *s)
85 size_t l;
86 if (s) {
87 l = strlen(gs->s) + strlen(s) + 1;
88 if (l > gs->len) {
89 gs->s = xrealloc(gs->s, l);
90 gs->len = l;
92 strcat(gs->s, s);
96 /* Append printf formatted string to growable string */
97 void str_printf(struct gstr *gs, const char *fmt, ...)
99 va_list ap;
100 char s[10000]; /* big enough... */
101 va_start(ap, fmt);
102 vsnprintf(s, sizeof(s), fmt, ap);
103 str_append(gs, s);
104 va_end(ap);
107 /* Retrieve value of growable string */
108 const char *str_get(struct gstr *gs)
110 return gs->s;
113 void *xmalloc(size_t size)
115 void *p = malloc(size);
116 if (p)
117 return p;
118 fprintf(stderr, "Out of memory.\n");
119 exit(1);
122 void *xcalloc(size_t nmemb, size_t size)
124 void *p = calloc(nmemb, size);
125 if (p)
126 return p;
127 fprintf(stderr, "Out of memory.\n");
128 exit(1);
131 void *xrealloc(void *p, size_t size)
133 p = realloc(p, size);
134 if (p)
135 return p;
136 fprintf(stderr, "Out of memory.\n");
137 exit(1);
140 char *xstrdup(const char *s)
142 char *p;
144 p = strdup(s);
145 if (p)
146 return p;
147 fprintf(stderr, "Out of memory.\n");
148 exit(1);
151 char *xstrndup(const char *s, size_t n)
153 char *p;
155 p = strndup(s, n);
156 if (p)
157 return p;
158 fprintf(stderr, "Out of memory.\n");
159 exit(1);