Merge tag 'for-linus-20180713' of git://git.kernel.dk/linux-block
[linux-2.6-block.git] / scripts / kconfig / util.c
CommitLineData
1da177e4
LT
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
10a4b277 8#include <stdarg.h>
02d95c96 9#include <stdlib.h>
1da177e4
LT
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;
17
18 for (file = file_list; file; file = file->next) {
c7abe863 19 if (!strcmp(name, file->name)) {
1da177e4 20 return file;
c7abe863 21 }
1da177e4
LT
22 }
23
177acf78 24 file = xmalloc(sizeof(*file));
1da177e4 25 memset(file, 0, sizeof(*file));
bb222cee 26 file->name = xstrdup(name);
1da177e4
LT
27 file->next = file_list;
28 file_list = file;
29 return file;
30}
31
32/* write a dependency file as used by kbuild to track dependencies */
33int file_write_dep(const char *name)
34{
35 struct file *file;
36 FILE *out;
37
38 if (!name)
752625cf 39 name = ".kconfig.d";
1da177e4
LT
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);
49 }
12122f62
MH
50 fprintf(out, "\n%s: \\\n"
51 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
93449082 52
104daea1 53 env_write_dep(out, conf_get_autoconfig_name());
93449082
RZ
54
55 fprintf(out, "\n$(deps_config): ;\n");
1da177e4
LT
56 fclose(out);
57 rename("..config.tmp", name);
58 return 0;
59}
60
61
31a2d31d 62/* Allocate initial growable string */
1da177e4
LT
63struct gstr str_new(void)
64{
65 struct gstr gs;
177acf78 66 gs.s = xmalloc(sizeof(char) * 64);
107f43a0 67 gs.len = 64;
da60fbbc 68 gs.max_width = 0;
1da177e4
LT
69 strcpy(gs.s, "\0");
70 return gs;
71}
72
1da177e4
LT
73/* Free storage for growable string */
74void str_free(struct gstr *gs)
75{
76 if (gs->s)
77 free(gs->s);
78 gs->s = NULL;
79 gs->len = 0;
80}
81
82/* Append to growable string */
83void str_append(struct gstr *gs, const char *s)
84{
a67cb131
SR
85 size_t l;
86 if (s) {
87 l = strlen(gs->s) + strlen(s) + 1;
88 if (l > gs->len) {
d717f24d 89 gs->s = xrealloc(gs->s, l);
a67cb131
SR
90 gs->len = l;
91 }
92 strcat(gs->s, s);
1da177e4 93 }
1da177e4
LT
94}
95
96/* Append printf formatted string to growable string */
97void str_printf(struct gstr *gs, const char *fmt, ...)
98{
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);
105}
106
4a4efbde 107/* Retrieve value of growable string */
1da177e4
LT
108const char *str_get(struct gstr *gs)
109{
110 return gs->s;
111}
112
177acf78
AC
113void *xmalloc(size_t size)
114{
115 void *p = malloc(size);
116 if (p)
117 return p;
118 fprintf(stderr, "Out of memory.\n");
119 exit(1);
120}
121
122void *xcalloc(size_t nmemb, size_t size)
123{
124 void *p = calloc(nmemb, size);
125 if (p)
126 return p;
127 fprintf(stderr, "Out of memory.\n");
128 exit(1);
129}
d717f24d
MY
130
131void *xrealloc(void *p, size_t size)
132{
133 p = realloc(p, size);
134 if (p)
135 return p;
136 fprintf(stderr, "Out of memory.\n");
137 exit(1);
138}
cd81fc82
MY
139
140char *xstrdup(const char *s)
141{
142 char *p;
143
144 p = strdup(s);
145 if (p)
146 return p;
147 fprintf(stderr, "Out of memory.\n");
148 exit(1);
149}
104daea1
MY
150
151char *xstrndup(const char *s, size_t n)
152{
153 char *p;
154
155 p = strndup(s, n);
156 if (p)
157 return p;
158 fprintf(stderr, "Out of memory.\n");
159 exit(1);
160}