xref: /openbmc/linux/scripts/kconfig/util.c (revision f79e4d5f)
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;
17 
18 	for (file = file_list; file; file = file->next) {
19 		if (!strcmp(name, file->name)) {
20 			return file;
21 		}
22 	}
23 
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;
30 }
31 
32 /* write a dependency file as used by kbuild to track dependencies */
33 int file_write_dep(const char *name)
34 {
35 	struct file *file;
36 	FILE *out;
37 
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);
49 	}
50 	fprintf(out, "\n%s: \\\n"
51 		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
52 
53 	env_write_dep(out, conf_get_autoconfig_name());
54 
55 	fprintf(out, "\n$(deps_config): ;\n");
56 	fclose(out);
57 	rename("..config.tmp", name);
58 	return 0;
59 }
60 
61 
62 /* Allocate initial growable string */
63 struct gstr str_new(void)
64 {
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;
71 }
72 
73 /* Free storage for growable string */
74 void 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 */
83 void str_append(struct gstr *gs, const char *s)
84 {
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;
91 		}
92 		strcat(gs->s, s);
93 	}
94 }
95 
96 /* Append printf formatted string to growable string */
97 void 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 
107 /* Retrieve value of growable string */
108 const char *str_get(struct gstr *gs)
109 {
110 	return gs->s;
111 }
112 
113 void *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 
122 void *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 }
130 
131 void *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 }
139 
140 char *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 }
150 
151 char *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 }
161