blob: d999683bb2a778ae05841531abad3fc3e323d4a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Arnaud Lacombe10a4b272011-06-01 16:00:46 -04008#include <stdarg.h>
Arnaud Lacombe02d95c92011-06-01 16:08:14 -04009#include <stdlib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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) {
Arnaud Lacombec7abe862010-09-04 16:11:26 -040019 if (!strcmp(name, file->name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 return file;
Arnaud Lacombec7abe862010-09-04 16:11:26 -040021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 }
23
Alan Cox177acf72012-11-06 14:32:08 +000024 file = xmalloc(sizeof(*file));
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 memset(file, 0, sizeof(*file));
Masahiro Yamadabb222ce2018-05-28 18:21:41 +090026 file->name = xstrdup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 file->next = file_list;
28 file_list = file;
29 return file;
30}
31
Thomas Weber31a2d312010-02-19 12:43:44 +010032/* Allocate initial growable string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct gstr str_new(void)
34{
35 struct gstr gs;
Alan Cox177acf72012-11-06 14:32:08 +000036 gs.s = xmalloc(sizeof(char) * 64);
Christophe Jaillet107f43a2008-05-18 23:10:24 +020037 gs.len = 64;
Vadim Bendebury (вб)da60fbb2009-12-20 00:29:49 -080038 gs.max_width = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 strcpy(gs.s, "\0");
40 return gs;
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* 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{
Sam Ravnborga67cb132007-09-19 21:23:09 +020055 size_t l;
56 if (s) {
57 l = strlen(gs->s) + strlen(s) + 1;
58 if (l > gs->len) {
Masahiro Yamadad717f242018-02-09 01:19:07 +090059 gs->s = xrealloc(gs->s, l);
Sam Ravnborga67cb132007-09-19 21:23:09 +020060 gs->len = l;
61 }
62 strcat(gs->s, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
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
Matt Mackall4a4efbd2006-01-03 13:27:11 +010077/* Retrieve value of growable string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078const char *str_get(struct gstr *gs)
79{
80 return gs->s;
81}
82
Alan Cox177acf72012-11-06 14:32:08 +000083void *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}
Masahiro Yamadad717f242018-02-09 01:19:07 +0900100
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}
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900109
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}
Masahiro Yamada104daea2018-05-28 18:21:40 +0900120
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}