"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2 | /* |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org> |
| 4 | * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org> |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 6 | * Released under the terms of the GNU GPL v2.0. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include "lkc.h" |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 11 | |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 12 | /* file already present in list? If not add it */ |
| 13 | struct file *file_lookup(const char *name) |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 14 | { |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 15 | struct file *file; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 16 | |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 17 | for (file = file_list; file; file = file->next) { |
| 18 | if (!strcmp(name, file->name)) |
| 19 | return file; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 22 | 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 */ |
| 31 | int 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 Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
Eric Andersen | c9f20d9 | 2002-12-05 08:41:41 +0000 | [diff] [blame] | 53 | |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 54 | |
| 55 | /* Allocate initial growable sting */ |
| 56 | struct gstr str_new(void) |
Eric Andersen | c9f20d9 | 2002-12-05 08:41:41 +0000 | [diff] [blame] | 57 | { |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 58 | struct gstr gs; |
| 59 | gs.s = malloc(sizeof(char) * 64); |
| 60 | gs.len = 16; |
| 61 | strcpy(gs.s, "\0"); |
| 62 | return gs; |
Eric Andersen | c9f20d9 | 2002-12-05 08:41:41 +0000 | [diff] [blame] | 63 | } |
Peter Kjellerstedt | bae38db | 2005-04-19 09:55:06 +0000 | [diff] [blame] | 64 | |
| 65 | /* Allocate and assign growable string */ |
| 66 | struct 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 */ |
| 75 | void 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 */ |
| 84 | void 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 */ |
| 95 | void 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 */ |
| 106 | const char *str_get(struct gstr *gs) |
| 107 | { |
| 108 | return gs->s; |
| 109 | } |
| 110 | |