kconfig/util.c
changeset 1 eeea35fbf182
child 943 1cca90ce0481
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/kconfig/util.c	Sat Feb 24 11:00:05 2007 +0000
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
     1.6 + * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
     1.7 + *
     1.8 + * Released under the terms of the GNU GPL v2.0.
     1.9 + */
    1.10 +
    1.11 +#include <string.h>
    1.12 +#include "lkc.h"
    1.13 +
    1.14 +/* file already present in list? If not add it */
    1.15 +struct file *file_lookup(const char *name)
    1.16 +{
    1.17 +	struct file *file;
    1.18 +
    1.19 +	for (file = file_list; file; file = file->next) {
    1.20 +		if (!strcmp(name, file->name))
    1.21 +			return file;
    1.22 +	}
    1.23 +
    1.24 +	file = malloc(sizeof(*file));
    1.25 +	memset(file, 0, sizeof(*file));
    1.26 +	file->name = strdup(name);
    1.27 +	file->next = file_list;
    1.28 +	file_list = file;
    1.29 +	return file;
    1.30 +}
    1.31 +
    1.32 +/* write a dependency file as used by kbuild to track dependencies */
    1.33 +int file_write_dep(const char *name)
    1.34 +{
    1.35 +	struct file *file;
    1.36 +	FILE *out;
    1.37 +
    1.38 +	if (!name)
    1.39 +		name = ".kconfig.d";
    1.40 +	out = fopen("..config.tmp", "w");
    1.41 +	if (!out)
    1.42 +		return 1;
    1.43 +	fprintf(out, "deps_config := \\\n");
    1.44 +	for (file = file_list; file; file = file->next) {
    1.45 +		if (file->next)
    1.46 +			fprintf(out, "\t%s \\\n", file->name);
    1.47 +		else
    1.48 +			fprintf(out, "\t%s\n", file->name);
    1.49 +	}
    1.50 +	fprintf(out, "\ninclude/config/auto.conf: \\\n"
    1.51 +		     "\t$(deps_config)\n\n"
    1.52 +		     "$(deps_config): ;\n");
    1.53 +	fclose(out);
    1.54 +	rename("..config.tmp", name);
    1.55 +	return 0;
    1.56 +}
    1.57 +
    1.58 +
    1.59 +/* Allocate initial growable sting */
    1.60 +struct gstr str_new(void)
    1.61 +{
    1.62 +	struct gstr gs;
    1.63 +	gs.s = malloc(sizeof(char) * 64);
    1.64 +	gs.len = 16;
    1.65 +	strcpy(gs.s, "\0");
    1.66 +	return gs;
    1.67 +}
    1.68 +
    1.69 +/* Allocate and assign growable string */
    1.70 +struct gstr str_assign(const char *s)
    1.71 +{
    1.72 +	struct gstr gs;
    1.73 +	gs.s = strdup(s);
    1.74 +	gs.len = strlen(s) + 1;
    1.75 +	return gs;
    1.76 +}
    1.77 +
    1.78 +/* Free storage for growable string */
    1.79 +void str_free(struct gstr *gs)
    1.80 +{
    1.81 +	if (gs->s)
    1.82 +		free(gs->s);
    1.83 +	gs->s = NULL;
    1.84 +	gs->len = 0;
    1.85 +}
    1.86 +
    1.87 +/* Append to growable string */
    1.88 +void str_append(struct gstr *gs, const char *s)
    1.89 +{
    1.90 +	size_t l = strlen(gs->s) + strlen(s) + 1;
    1.91 +	if (l > gs->len) {
    1.92 +		gs->s   = realloc(gs->s, l);
    1.93 +		gs->len = l;
    1.94 +	}
    1.95 +	strcat(gs->s, s);
    1.96 +}
    1.97 +
    1.98 +/* Append printf formatted string to growable string */
    1.99 +void str_printf(struct gstr *gs, const char *fmt, ...)
   1.100 +{
   1.101 +	va_list ap;
   1.102 +	char s[10000]; /* big enough... */
   1.103 +	va_start(ap, fmt);
   1.104 +	vsnprintf(s, sizeof(s), fmt, ap);
   1.105 +	str_append(gs, s);
   1.106 +	va_end(ap);
   1.107 +}
   1.108 +
   1.109 +/* Retrieve value of growable string */
   1.110 +const char *str_get(struct gstr *gs)
   1.111 +{
   1.112 +	return gs->s;
   1.113 +}
   1.114 +