blob: a365594770d9daaaaf3220c80eb1522113930072 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Arnaud Lacombe10a4b272011-06-01 16:00:46 -04008#include <stdarg.h>
Arnaud Lacombe02d95c92011-06-01 16:08:14 -04009#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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) {
Arnaud Lacombec7abe862010-09-04 16:11:26 -040019 if (!strcmp(name, file->name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 return file;
Arnaud Lacombec7abe862010-09-04 16:11:26 -040021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 }
23
Alan Cox177acf72012-11-06 14:32:08 +000024 file = xmalloc(sizeof(*file));
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 memset(file, 0, sizeof(*file));
Masahiro Yamadabb222ce2018-05-28 18:21:41 +090026 file->name = xstrdup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 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)
Sam Ravnborg752625c2005-12-26 23:34:03 +010039 name = ".kconfig.d";
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 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 }
Markus Heidelberg12122f62009-05-18 01:36:54 +020050 fprintf(out, "\n%s: \\\n"
51 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
Roman Zippel93449082008-01-14 04:50:54 +010052
Masahiro Yamada104daea2018-05-28 18:21:40 +090053 env_write_dep(out, conf_get_autoconfig_name());
Roman Zippel93449082008-01-14 04:50:54 +010054
55 fprintf(out, "\n$(deps_config): ;\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 fclose(out);
57 rename("..config.tmp", name);
58 return 0;
59}
60
61
Thomas Weber31a2d312010-02-19 12:43:44 +010062/* Allocate initial growable string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063struct gstr str_new(void)
64{
65 struct gstr gs;
Alan Cox177acf72012-11-06 14:32:08 +000066 gs.s = xmalloc(sizeof(char) * 64);
Christophe Jaillet107f43a2008-05-18 23:10:24 +020067 gs.len = 64;
Vadim Bendebury (вб)da60fbb2009-12-20 00:29:49 -080068 gs.max_width = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 strcpy(gs.s, "\0");
70 return gs;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* 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{
Sam Ravnborga67cb132007-09-19 21:23:09 +020085 size_t l;
86 if (s) {
87 l = strlen(gs->s) + strlen(s) + 1;
88 if (l > gs->len) {
Masahiro Yamadad717f242018-02-09 01:19:07 +090089 gs->s = xrealloc(gs->s, l);
Sam Ravnborga67cb132007-09-19 21:23:09 +020090 gs->len = l;
91 }
92 strcat(gs->s, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
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
Matt Mackall4a4efbd2006-01-03 13:27:11 +0100107/* Retrieve value of growable string */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108const char *str_get(struct gstr *gs)
109{
110 return gs->s;
111}
112
Alan Cox177acf72012-11-06 14:32:08 +0000113void *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}
Masahiro Yamadad717f242018-02-09 01:19:07 +0900130
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}
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900139
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}
Masahiro Yamada104daea2018-05-28 18:21:40 +0900150
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}