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 /* Allocate initial growable string */
33 struct gstr str_new(void)
36 gs.s = xmalloc(sizeof(char) * 64);
43 /* Free storage for growable string */
44 void str_free(struct gstr *gs)
52 /* Append to growable string */
53 void str_append(struct gstr *gs, const char *s)
57 l = strlen(gs->s) + strlen(s) + 1;
59 gs->s = xrealloc(gs->s, l);
66 /* Append printf formatted string to growable string */
67 void str_printf(struct gstr *gs, const char *fmt, ...)
70 char s[10000]; /* big enough... */
72 vsnprintf(s, sizeof(s), fmt, ap);
77 /* Retrieve value of growable string */
78 const char *str_get(struct gstr *gs)
83 void *xmalloc(size_t size)
85 void *p = malloc(size);
88 fprintf(stderr, "Out of memory.\n");
92 void *xcalloc(size_t nmemb, size_t size)
94 void *p = calloc(nmemb, size);
97 fprintf(stderr, "Out of memory.\n");
101 void *xrealloc(void *p, size_t size)
103 p = realloc(p, size);
106 fprintf(stderr, "Out of memory.\n");
110 char *xstrdup(const char *s)
117 fprintf(stderr, "Out of memory.\n");
121 char *xstrndup(const char *s, size_t n)
128 fprintf(stderr, "Out of memory.\n");