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.
13 /* file already present in list? If not add it */
14 struct file *file_lookup(const char *name)
18 for (file = file_list; file; file = file->next) {
19 if (!strcmp(name, file->name)) {
24 file = xmalloc(sizeof(*file));
25 memset(file, 0, sizeof(*file));
26 file->name = xstrdup(name);
27 file->next = file_list;
32 /* write a dependency file as used by kbuild to track dependencies */
33 int file_write_dep(const char *name)
40 out = fopen("..config.tmp", "w");
43 fprintf(out, "deps_config := \\\n");
44 for (file = file_list; file; file = file->next) {
46 fprintf(out, "\t%s \\\n", file->name);
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");
57 rename("..config.tmp", name);
62 /* Allocate initial growable string */
63 struct gstr str_new(void)
66 gs.s = xmalloc(sizeof(char) * 64);
73 /* Free storage for growable string */
74 void str_free(struct gstr *gs)
82 /* Append to growable string */
83 void str_append(struct gstr *gs, const char *s)
87 l = strlen(gs->s) + strlen(s) + 1;
89 gs->s = xrealloc(gs->s, l);
96 /* Append printf formatted string to growable string */
97 void str_printf(struct gstr *gs, const char *fmt, ...)
100 char s[10000]; /* big enough... */
102 vsnprintf(s, sizeof(s), fmt, ap);
107 /* Retrieve value of growable string */
108 const char *str_get(struct gstr *gs)
113 void *xmalloc(size_t size)
115 void *p = malloc(size);
118 fprintf(stderr, "Out of memory.\n");
122 void *xcalloc(size_t nmemb, size_t size)
124 void *p = calloc(nmemb, size);
127 fprintf(stderr, "Out of memory.\n");
131 void *xrealloc(void *p, size_t size)
133 p = realloc(p, size);
136 fprintf(stderr, "Out of memory.\n");
140 char *xstrdup(const char *s)
147 fprintf(stderr, "Out of memory.\n");
151 char *xstrndup(const char *s, size_t n)
158 fprintf(stderr, "Out of memory.\n");