Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate utils.c @ 38:b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
author | David A. Holland |
---|---|
date | Sat, 30 Mar 2013 21:02:25 -0400 (2013-03-31) |
parents | 40748b097655 |
children | 337110e7240a |
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 * | |
60 dorealloc(void *ptr, size_t len) | |
61 { | |
62 void *ret; | |
63 | |
64 ret = realloc(ptr, len); | |
65 if (ret == NULL) { | |
66 warnx("Out of memory"); | |
67 die(); | |
68 } | |
69 return ret; | |
70 } | |
71 | |
38
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
72 void |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
73 dofree(void *ptr) |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
74 { |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
75 free(ptr); |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
76 } |
b156910b59b2
Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents:
20
diff
changeset
|
77 |
3 | 78 char * |
79 dostrdup(const char *s) | |
80 { | |
81 char *ret; | |
82 size_t len; | |
83 | |
84 len = strlen(s); | |
85 ret = domalloc(len+1); | |
86 strcpy(ret, s); | |
87 return ret; | |
88 } | |
89 | |
90 char * | |
91 dostrdup2(const char *s, const char *t) | |
92 { | |
93 char *ret; | |
94 size_t len; | |
95 | |
96 len = strlen(s) + strlen(t); | |
97 ret = domalloc(len+1); | |
98 strcpy(ret, s); | |
99 strcat(ret, t); | |
100 return ret; | |
101 } | |
102 | |
103 char * | |
104 dostrdup3(const char *s, const char *t, const char *u) | |
105 { | |
106 char *ret; | |
107 size_t len; | |
108 | |
109 len = strlen(s) + strlen(t) + strlen(u); | |
110 ret = domalloc(len+1); | |
111 strcpy(ret, s); | |
112 strcat(ret, t); | |
113 strcat(ret, u); | |
114 return ret; | |
115 } | |
116 | |
20 | 117 char * |
118 dostrndup(const char *s, size_t len) | |
119 { | |
120 char *ret; | |
121 | |
122 ret = domalloc(len+1); | |
123 memcpy(ret, s, len); | |
124 ret[len] = '\0'; | |
125 return ret; | |
126 } | |
127 | |
18 | 128 size_t |
129 notrailingws(char *buf, size_t len) | |
130 { | |
131 while (len > 0 && strchr(ws, buf[len-1])) { | |
132 buf[--len] = '\0'; | |
133 } | |
134 return len; | |
135 } | |
136 | |
137 bool | |
138 is_identifier(const char *str) | |
139 { | |
140 size_t len; | |
141 | |
142 len = strlen(str); | |
143 if (len != strspn(str, alnum)) { | |
144 return false; | |
145 } | |
146 if (str[0] >= '0' && str[0] <= '9') { | |
147 return false; | |
148 } | |
149 return true; | |
150 } |