uboot/scripts/kconfig/util.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
   3 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
   4 *
   5 * Released under the terms of the GNU GPL v2.0.
   6 */
   7
   8#include <stdarg.h>
   9#include <stdlib.h>
  10#include <string.h>
  11#include "lkc.h"
  12
  13/* file already present in list? If not add it */
  14struct file *file_lookup(const char *name)
  15{
  16        struct file *file;
  17
  18        for (file = file_list; file; file = file->next) {
  19                if (!strcmp(name, file->name)) {
  20                        return file;
  21                }
  22        }
  23
  24        file = xmalloc(sizeof(*file));
  25        memset(file, 0, sizeof(*file));
  26        file->name = xstrdup(name);
  27        file->next = file_list;
  28        file_list = file;
  29        return file;
  30}
  31
  32/* Allocate initial growable string */
  33struct gstr str_new(void)
  34{
  35        struct gstr gs;
  36        gs.s = xmalloc(sizeof(char) * 64);
  37        gs.len = 64;
  38        gs.max_width = 0;
  39        strcpy(gs.s, "\0");
  40        return gs;
  41}
  42
  43/* Free storage for growable string */
  44void str_free(struct gstr *gs)
  45{
  46        if (gs->s)
  47                free(gs->s);
  48        gs->s = NULL;
  49        gs->len = 0;
  50}
  51
  52/* Append to growable string */
  53void str_append(struct gstr *gs, const char *s)
  54{
  55        size_t l;
  56        if (s) {
  57                l = strlen(gs->s) + strlen(s) + 1;
  58                if (l > gs->len) {
  59                        gs->s = xrealloc(gs->s, l);
  60                        gs->len = l;
  61                }
  62                strcat(gs->s, s);
  63        }
  64}
  65
  66/* Append printf formatted string to growable string */
  67void str_printf(struct gstr *gs, const char *fmt, ...)
  68{
  69        va_list ap;
  70        char s[10000]; /* big enough... */
  71        va_start(ap, fmt);
  72        vsnprintf(s, sizeof(s), fmt, ap);
  73        str_append(gs, s);
  74        va_end(ap);
  75}
  76
  77/* Retrieve value of growable string */
  78const char *str_get(struct gstr *gs)
  79{
  80        return gs->s;
  81}
  82
  83void *xmalloc(size_t size)
  84{
  85        void *p = malloc(size);
  86        if (p)
  87                return p;
  88        fprintf(stderr, "Out of memory.\n");
  89        exit(1);
  90}
  91
  92void *xcalloc(size_t nmemb, size_t size)
  93{
  94        void *p = calloc(nmemb, size);
  95        if (p)
  96                return p;
  97        fprintf(stderr, "Out of memory.\n");
  98        exit(1);
  99}
 100
 101void *xrealloc(void *p, size_t size)
 102{
 103        p = realloc(p, size);
 104        if (p)
 105                return p;
 106        fprintf(stderr, "Out of memory.\n");
 107        exit(1);
 108}
 109
 110char *xstrdup(const char *s)
 111{
 112        char *p;
 113
 114        p = strdup(s);
 115        if (p)
 116                return p;
 117        fprintf(stderr, "Out of memory.\n");
 118        exit(1);
 119}
 120
 121char *xstrndup(const char *s, size_t n)
 122{
 123        char *p;
 124
 125        p = strndup(s, n);
 126        if (p)
 127                return p;
 128        fprintf(stderr, "Out of memory.\n");
 129        exit(1);
 130}
 131