Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
0004  * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
0005  */
0006 
0007 #include <stdarg.h>
0008 #include <stdlib.h>
0009 #include <string.h>
0010 #include "lkc.h"
0011 
0012 /* file already present in list? If not add it */
0013 struct file *file_lookup(const char *name)
0014 {
0015     struct file *file;
0016 
0017     for (file = file_list; file; file = file->next) {
0018         if (!strcmp(name, file->name)) {
0019             return file;
0020         }
0021     }
0022 
0023     file = xmalloc(sizeof(*file));
0024     memset(file, 0, sizeof(*file));
0025     file->name = xstrdup(name);
0026     file->next = file_list;
0027     file_list = file;
0028     return file;
0029 }
0030 
0031 /* Allocate initial growable string */
0032 struct gstr str_new(void)
0033 {
0034     struct gstr gs;
0035     gs.s = xmalloc(sizeof(char) * 64);
0036     gs.len = 64;
0037     gs.max_width = 0;
0038     strcpy(gs.s, "\0");
0039     return gs;
0040 }
0041 
0042 /* Free storage for growable string */
0043 void str_free(struct gstr *gs)
0044 {
0045     if (gs->s)
0046         free(gs->s);
0047     gs->s = NULL;
0048     gs->len = 0;
0049 }
0050 
0051 /* Append to growable string */
0052 void str_append(struct gstr *gs, const char *s)
0053 {
0054     size_t l;
0055     if (s) {
0056         l = strlen(gs->s) + strlen(s) + 1;
0057         if (l > gs->len) {
0058             gs->s = xrealloc(gs->s, l);
0059             gs->len = l;
0060         }
0061         strcat(gs->s, s);
0062     }
0063 }
0064 
0065 /* Append printf formatted string to growable string */
0066 void str_printf(struct gstr *gs, const char *fmt, ...)
0067 {
0068     va_list ap;
0069     char s[10000]; /* big enough... */
0070     va_start(ap, fmt);
0071     vsnprintf(s, sizeof(s), fmt, ap);
0072     str_append(gs, s);
0073     va_end(ap);
0074 }
0075 
0076 /* Retrieve value of growable string */
0077 const char *str_get(struct gstr *gs)
0078 {
0079     return gs->s;
0080 }
0081 
0082 void *xmalloc(size_t size)
0083 {
0084     void *p = malloc(size);
0085     if (p)
0086         return p;
0087     fprintf(stderr, "Out of memory.\n");
0088     exit(1);
0089 }
0090 
0091 void *xcalloc(size_t nmemb, size_t size)
0092 {
0093     void *p = calloc(nmemb, size);
0094     if (p)
0095         return p;
0096     fprintf(stderr, "Out of memory.\n");
0097     exit(1);
0098 }
0099 
0100 void *xrealloc(void *p, size_t size)
0101 {
0102     p = realloc(p, size);
0103     if (p)
0104         return p;
0105     fprintf(stderr, "Out of memory.\n");
0106     exit(1);
0107 }
0108 
0109 char *xstrdup(const char *s)
0110 {
0111     char *p;
0112 
0113     p = strdup(s);
0114     if (p)
0115         return p;
0116     fprintf(stderr, "Out of memory.\n");
0117     exit(1);
0118 }
0119 
0120 char *xstrndup(const char *s, size_t n)
0121 {
0122     char *p;
0123 
0124     p = strndup(s, n);
0125     if (p)
0126         return p;
0127     fprintf(stderr, "Out of memory.\n");
0128     exit(1);
0129 }