blob: dbf23edef6c86d221b30a251adf5a18913bafd5e [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002/*
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +00003 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
4 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 *
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +00006 * Released under the terms of the GNU GPL v2.0.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00007 */
8
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +00009#include <string.h>
10#include "lkc.h"
Eric Andersenbdfd0d72001-10-24 05:00:29 +000011
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000012/* file already present in list? If not add it */
13struct file *file_lookup(const char *name)
Eric Andersenbdfd0d72001-10-24 05:00:29 +000014{
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000015 struct file *file;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000016
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000017 for (file = file_list; file; file = file->next) {
18 if (!strcmp(name, file->name))
19 return file;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000020 }
21
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000022 file = malloc(sizeof(*file));
23 memset(file, 0, sizeof(*file));
24 file->name = strdup(name);
25 file->next = file_list;
26 file_list = file;
27 return file;
28}
29
30/* write a dependency file as used by kbuild to track dependencies */
31int file_write_dep(const char *name)
32{
33 struct file *file;
34 FILE *out;
35
36 if (!name)
37 name = ".config.cmd";
38 out = fopen(".config.tmp", "w");
39 if (!out)
40 return 1;
41 fprintf(out, "deps_config := \\\n");
42 for (file = file_list; file; file = file->next) {
43 if (file->next)
44 fprintf(out, "\t%s \\\n", file->name);
45 else
46 fprintf(out, "\t%s\n", file->name);
47 }
48 fprintf(out, "\n.config include/config.h: $(deps_config)\n\n$(deps_config):\n");
49 fclose(out);
50 rename(".config.tmp", name);
Eric Andersenbdfd0d72001-10-24 05:00:29 +000051 return 0;
52}
Eric Andersenc9f20d92002-12-05 08:41:41 +000053
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000054
55/* Allocate initial growable sting */
56struct gstr str_new(void)
Eric Andersenc9f20d92002-12-05 08:41:41 +000057{
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000058 struct gstr gs;
59 gs.s = malloc(sizeof(char) * 64);
60 gs.len = 16;
61 strcpy(gs.s, "\0");
62 return gs;
Eric Andersenc9f20d92002-12-05 08:41:41 +000063}
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +000064
65/* Allocate and assign growable string */
66struct gstr str_assign(const char *s)
67{
68 struct gstr gs;
69 gs.s = strdup(s);
70 gs.len = strlen(s) + 1;
71 return gs;
72}
73
74/* Free storage for growable string */
75void str_free(struct gstr *gs)
76{
77 if (gs->s)
78 free(gs->s);
79 gs->s = NULL;
80 gs->len = 0;
81}
82
83/* Append to growable string */
84void str_append(struct gstr *gs, const char *s)
85{
86 size_t l = strlen(gs->s) + strlen(s) + 1;
87 if (l > gs->len) {
88 gs->s = realloc(gs->s, l);
89 gs->len = l;
90 }
91 strcat(gs->s, s);
92}
93
94/* Append printf formatted string to growable string */
95void str_printf(struct gstr *gs, const char *fmt, ...)
96{
97 va_list ap;
98 char s[10000]; /* big enough... */
99 va_start(ap, fmt);
100 vsnprintf(s, sizeof(s), fmt, ap);
101 str_append(gs, s);
102 va_end(ap);
103}
104
105/* Retreive value of growable string */
106const char *str_get(struct gstr *gs)
107{
108 return gs->s;
109}
110