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
|
|
72 char *
|
|
73 dostrdup(const char *s)
|
|
74 {
|
|
75 char *ret;
|
|
76 size_t len;
|
|
77
|
|
78 len = strlen(s);
|
|
79 ret = domalloc(len+1);
|
|
80 strcpy(ret, s);
|
|
81 return ret;
|
|
82 }
|
|
83
|
|
84 char *
|
|
85 dostrdup2(const char *s, const char *t)
|
|
86 {
|
|
87 char *ret;
|
|
88 size_t len;
|
|
89
|
|
90 len = strlen(s) + strlen(t);
|
|
91 ret = domalloc(len+1);
|
|
92 strcpy(ret, s);
|
|
93 strcat(ret, t);
|
|
94 return ret;
|
|
95 }
|
|
96
|
|
97 char *
|
|
98 dostrdup3(const char *s, const char *t, const char *u)
|
|
99 {
|
|
100 char *ret;
|
|
101 size_t len;
|
|
102
|
|
103 len = strlen(s) + strlen(t) + strlen(u);
|
|
104 ret = domalloc(len+1);
|
|
105 strcpy(ret, s);
|
|
106 strcat(ret, t);
|
|
107 strcat(ret, u);
|
|
108 return ret;
|
|
109 }
|
|
110
|
20
|
111 char *
|
|
112 dostrndup(const char *s, size_t len)
|
|
113 {
|
|
114 char *ret;
|
|
115
|
|
116 ret = domalloc(len+1);
|
|
117 memcpy(ret, s, len);
|
|
118 ret[len] = '\0';
|
|
119 return ret;
|
|
120 }
|
|
121
|
18
|
122 size_t
|
|
123 notrailingws(char *buf, size_t len)
|
|
124 {
|
|
125 while (len > 0 && strchr(ws, buf[len-1])) {
|
|
126 buf[--len] = '\0';
|
|
127 }
|
|
128 return len;
|
|
129 }
|
|
130
|
|
131 bool
|
|
132 is_identifier(const char *str)
|
|
133 {
|
|
134 size_t len;
|
|
135
|
|
136 len = strlen(str);
|
|
137 if (len != strspn(str, alnum)) {
|
|
138 return false;
|
|
139 }
|
|
140 if (str[0] >= '0' && str[0] <= '9') {
|
|
141 return false;
|
|
142 }
|
|
143 return true;
|
|
144 }
|