Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate utils.c @ 40:291fefe664f2
Oops, fix previous.
author | David A. Holland |
---|---|
date | Sat, 30 Mar 2013 21:48:57 -0400 |
parents | 337110e7240a |
children | ef8bedab8a4b |
rev | line source |
---|---|
3 | 1 /*- |
2 * Copyright (c) 2010 The NetBSD Foundation, Inc. | |
3 * All rights reserved. | |
4 * | |
5 * This code is derived from software contributed to The NetBSD Foundation | |
6 * by David A. Holland. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 * POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
30 #include <stdlib.h> | |
31 #include <string.h> | |
32 #include <err.h> | |
33 | |
34 #include "utils.h" | |
35 | |
16 | 36 const char ws[] = |
37 " \t\f\v" | |
38 ; | |
39 const char alnum[] = | |
40 "0123456789" | |
41 "abcdefghijklmnopqrstuvwxyz" | |
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
43 "_" | |
44 ; | |
45 | |
3 | 46 void * |
47 domalloc(size_t len) | |
48 { | |
49 void *ret; | |
50 | |
51 ret = malloc(len); | |
52 if (ret == NULL) { | |
53 warnx("Out of memory"); | |
54 die(); | |
55 } | |
56 return ret; | |
57 } | |
58 | |
59 void * | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
60 dorealloc(void *ptr, size_t oldlen, size_t newlen) |
3 | 61 { |
62 void *ret; | |
63 | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
64 (void)oldlen; |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
65 ret = realloc(ptr, newlen); |
3 | 66 if (ret == NULL) { |
67 warnx("Out of memory"); | |
68 die(); | |
69 } | |
70 return ret; | |
71 } | |
72 | |
38
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
73 void |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
74 dofree(void *ptr, size_t len) |
38
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
75 { |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
76 (void)len; |
38
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
77 free(ptr); |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
78 } |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
79 |
3 | 80 char * |
81 dostrdup(const char *s) | |
82 { | |
83 char *ret; | |
84 size_t len; | |
85 | |
86 len = strlen(s); | |
87 ret = domalloc(len+1); | |
88 strcpy(ret, s); | |
89 return ret; | |
90 } | |
91 | |
92 char * | |
93 dostrdup2(const char *s, const char *t) | |
94 { | |
95 char *ret; | |
96 size_t len; | |
97 | |
98 len = strlen(s) + strlen(t); | |
99 ret = domalloc(len+1); | |
100 strcpy(ret, s); | |
101 strcat(ret, t); | |
102 return ret; | |
103 } | |
104 | |
105 char * | |
106 dostrdup3(const char *s, const char *t, const char *u) | |
107 { | |
108 char *ret; | |
109 size_t len; | |
110 | |
111 len = strlen(s) + strlen(t) + strlen(u); | |
112 ret = domalloc(len+1); | |
113 strcpy(ret, s); | |
114 strcat(ret, t); | |
115 strcat(ret, u); | |
116 return ret; | |
117 } | |
118 | |
20 | 119 char * |
120 dostrndup(const char *s, size_t len) | |
121 { | |
122 char *ret; | |
123 | |
124 ret = domalloc(len+1); | |
125 memcpy(ret, s, len); | |
126 ret[len] = '\0'; | |
127 return ret; | |
128 } | |
129 | |
39
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
130 void |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
131 dostrfree(char *s) |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
132 { |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
133 dofree(s, strlen(s)+1); |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
134 } |
337110e7240a
Pass the size to free; it makes debug checking easier.
David A. Holland
parents:
38
diff
changeset
|
135 |
18 | 136 size_t |
137 notrailingws(char *buf, size_t len) | |
138 { | |
139 while (len > 0 && strchr(ws, buf[len-1])) { | |
140 buf[--len] = '\0'; | |
141 } | |
142 return len; | |
143 } | |
144 | |
145 bool | |
146 is_identifier(const char *str) | |
147 { | |
148 size_t len; | |
149 | |
150 len = strlen(str); | |
151 if (len != strspn(str, alnum)) { | |
152 return false; | |
153 } | |
154 if (str[0] >= '0' && str[0] <= '9') { | |
155 return false; | |
156 } | |
157 return true; | |
158 } |