1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #include <strings.h>
28*7c478bd9Sstevel@tonic-gate #include <ctype.h>
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #include <fmd_string.h>
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate char *
fmd_strdup(const char * s,int flags)33*7c478bd9Sstevel@tonic-gate fmd_strdup(const char *s, int flags)
34*7c478bd9Sstevel@tonic-gate {
35*7c478bd9Sstevel@tonic-gate char *p;
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate if (s != NULL)
38*7c478bd9Sstevel@tonic-gate p = fmd_alloc(strlen(s) + 1, flags);
39*7c478bd9Sstevel@tonic-gate else
40*7c478bd9Sstevel@tonic-gate p = NULL;
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate if (p != NULL)
43*7c478bd9Sstevel@tonic-gate (void) strcpy(p, s);
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate return (p);
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate void
fmd_strfree(char * s)49*7c478bd9Sstevel@tonic-gate fmd_strfree(char *s)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate if (s != NULL)
52*7c478bd9Sstevel@tonic-gate fmd_free(s, strlen(s) + 1);
53*7c478bd9Sstevel@tonic-gate }
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate const char *
fmd_strbasename(const char * s)56*7c478bd9Sstevel@tonic-gate fmd_strbasename(const char *s)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate const char *p = strrchr(s, '/');
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate if (p == NULL)
61*7c478bd9Sstevel@tonic-gate return (s);
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate return (++p);
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate char *
fmd_strdirname(char * s)67*7c478bd9Sstevel@tonic-gate fmd_strdirname(char *s)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate static char slash[] = "/";
70*7c478bd9Sstevel@tonic-gate static char dot[] = ".";
71*7c478bd9Sstevel@tonic-gate char *p;
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate if (s == NULL || *s == '\0')
74*7c478bd9Sstevel@tonic-gate return (dot);
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate for (p = s + strlen(s); p != s && *--p == '/'; )
77*7c478bd9Sstevel@tonic-gate continue;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate if (p == s && *p == '/')
80*7c478bd9Sstevel@tonic-gate return (slash);
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate while (p != s) {
83*7c478bd9Sstevel@tonic-gate if (*--p == '/') {
84*7c478bd9Sstevel@tonic-gate while (*p == '/' && p != s)
85*7c478bd9Sstevel@tonic-gate p--;
86*7c478bd9Sstevel@tonic-gate *++p = '\0';
87*7c478bd9Sstevel@tonic-gate return (s);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate return (dot);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate ulong_t
fmd_strhash(const char * key)95*7c478bd9Sstevel@tonic-gate fmd_strhash(const char *key)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate ulong_t g, h = 0;
98*7c478bd9Sstevel@tonic-gate const char *p;
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate for (p = key; *p != '\0'; p++) {
101*7c478bd9Sstevel@tonic-gate h = (h << 4) + *p;
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate if ((g = (h & 0xf0000000)) != 0) {
104*7c478bd9Sstevel@tonic-gate h ^= (g >> 24);
105*7c478bd9Sstevel@tonic-gate h ^= g;
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate return (h);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate * Transform string s inline, converting each embedded C escape sequence string
114*7c478bd9Sstevel@tonic-gate * to the corresponding character. For example, the substring "\n" is replaced
115*7c478bd9Sstevel@tonic-gate * by an inline '\n' character. The length of the resulting string is returned.
116*7c478bd9Sstevel@tonic-gate */
117*7c478bd9Sstevel@tonic-gate size_t
fmd_stresc2chr(char * s)118*7c478bd9Sstevel@tonic-gate fmd_stresc2chr(char *s)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate char *p, *q, c;
121*7c478bd9Sstevel@tonic-gate int esc = 0;
122*7c478bd9Sstevel@tonic-gate int x;
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate for (p = q = s; (c = *p) != '\0'; p++) {
125*7c478bd9Sstevel@tonic-gate if (esc) {
126*7c478bd9Sstevel@tonic-gate switch (c) {
127*7c478bd9Sstevel@tonic-gate case '0':
128*7c478bd9Sstevel@tonic-gate case '1':
129*7c478bd9Sstevel@tonic-gate case '2':
130*7c478bd9Sstevel@tonic-gate case '3':
131*7c478bd9Sstevel@tonic-gate case '4':
132*7c478bd9Sstevel@tonic-gate case '5':
133*7c478bd9Sstevel@tonic-gate case '6':
134*7c478bd9Sstevel@tonic-gate case '7':
135*7c478bd9Sstevel@tonic-gate c -= '0';
136*7c478bd9Sstevel@tonic-gate p++;
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate if (*p >= '0' && *p <= '7') {
139*7c478bd9Sstevel@tonic-gate c = c * 8 + *p++ - '0';
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate if (*p >= '0' && *p <= '7')
142*7c478bd9Sstevel@tonic-gate c = c * 8 + *p - '0';
143*7c478bd9Sstevel@tonic-gate else
144*7c478bd9Sstevel@tonic-gate p--;
145*7c478bd9Sstevel@tonic-gate } else
146*7c478bd9Sstevel@tonic-gate p--;
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate *q++ = c;
149*7c478bd9Sstevel@tonic-gate break;
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate case 'a':
152*7c478bd9Sstevel@tonic-gate *q++ = '\a';
153*7c478bd9Sstevel@tonic-gate break;
154*7c478bd9Sstevel@tonic-gate case 'b':
155*7c478bd9Sstevel@tonic-gate *q++ = '\b';
156*7c478bd9Sstevel@tonic-gate break;
157*7c478bd9Sstevel@tonic-gate case 'f':
158*7c478bd9Sstevel@tonic-gate *q++ = '\f';
159*7c478bd9Sstevel@tonic-gate break;
160*7c478bd9Sstevel@tonic-gate case 'n':
161*7c478bd9Sstevel@tonic-gate *q++ = '\n';
162*7c478bd9Sstevel@tonic-gate break;
163*7c478bd9Sstevel@tonic-gate case 'r':
164*7c478bd9Sstevel@tonic-gate *q++ = '\r';
165*7c478bd9Sstevel@tonic-gate break;
166*7c478bd9Sstevel@tonic-gate case 't':
167*7c478bd9Sstevel@tonic-gate *q++ = '\t';
168*7c478bd9Sstevel@tonic-gate break;
169*7c478bd9Sstevel@tonic-gate case 'v':
170*7c478bd9Sstevel@tonic-gate *q++ = '\v';
171*7c478bd9Sstevel@tonic-gate break;
172*7c478bd9Sstevel@tonic-gate
173*7c478bd9Sstevel@tonic-gate case 'x':
174*7c478bd9Sstevel@tonic-gate for (x = 0; (c = *++p) != '\0'; ) {
175*7c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9')
176*7c478bd9Sstevel@tonic-gate x = x * 16 + c - '0';
177*7c478bd9Sstevel@tonic-gate else if (c >= 'a' && c <= 'f')
178*7c478bd9Sstevel@tonic-gate x = x * 16 + c - 'a' + 10;
179*7c478bd9Sstevel@tonic-gate else if (c >= 'A' && c <= 'F')
180*7c478bd9Sstevel@tonic-gate x = x * 16 + c - 'A' + 10;
181*7c478bd9Sstevel@tonic-gate else
182*7c478bd9Sstevel@tonic-gate break;
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate *q++ = (char)x;
185*7c478bd9Sstevel@tonic-gate p--;
186*7c478bd9Sstevel@tonic-gate break;
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate case '"':
189*7c478bd9Sstevel@tonic-gate case '\\':
190*7c478bd9Sstevel@tonic-gate *q++ = c;
191*7c478bd9Sstevel@tonic-gate break;
192*7c478bd9Sstevel@tonic-gate default:
193*7c478bd9Sstevel@tonic-gate *q++ = '\\';
194*7c478bd9Sstevel@tonic-gate *q++ = c;
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate esc = 0;
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate } else {
200*7c478bd9Sstevel@tonic-gate if ((esc = c == '\\') == 0)
201*7c478bd9Sstevel@tonic-gate *q++ = c;
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate *q = '\0';
206*7c478bd9Sstevel@tonic-gate return ((size_t)(q - s));
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate /*
210*7c478bd9Sstevel@tonic-gate * We require that identifiers for buffers, statistics, and properties conform
211*7c478bd9Sstevel@tonic-gate * to the regular expression [a-zA-Z0-9\-_.]. If check_prefixes is set, we
212*7c478bd9Sstevel@tonic-gate * also flag strings that begin with a set of prefixes reserved for use by fmd.
213*7c478bd9Sstevel@tonic-gate */
214*7c478bd9Sstevel@tonic-gate const char *
fmd_strbadid(const char * s,int check_prefixes)215*7c478bd9Sstevel@tonic-gate fmd_strbadid(const char *s, int check_prefixes)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate const char *s0 = s;
218*7c478bd9Sstevel@tonic-gate int c = *s++;
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate while ((c = *s++) != '\0') {
221*7c478bd9Sstevel@tonic-gate if (!isupper(c) && !islower(c) &&
222*7c478bd9Sstevel@tonic-gate !isdigit(c) && c != '-' && c != '_' && c != '.')
223*7c478bd9Sstevel@tonic-gate return (s - 1);
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate if (check_prefixes && (s0[0] == '_' || s0[0] == '.' ||
227*7c478bd9Sstevel@tonic-gate strncmp(s0, "fmd_", 4) == 0 || strncmp(s0, "FMD_", 4) == 0 ||
228*7c478bd9Sstevel@tonic-gate strncmp(s0, "fmd.", 4) == 0 || strncmp(s0, "FMD.", 4) == 0))
229*7c478bd9Sstevel@tonic-gate return (s0);
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate return (NULL);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate int
fmd_strmatch(const char * s,const char * p)235*7c478bd9Sstevel@tonic-gate fmd_strmatch(const char *s, const char *p)
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate char c;
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate if (p == NULL)
240*7c478bd9Sstevel@tonic-gate return (0);
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate if (s == NULL)
243*7c478bd9Sstevel@tonic-gate s = ""; /* treat NULL string as the empty string */
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate do {
246*7c478bd9Sstevel@tonic-gate if ((c = *p++) == '\0')
247*7c478bd9Sstevel@tonic-gate return (*s == '\0');
248*7c478bd9Sstevel@tonic-gate
249*7c478bd9Sstevel@tonic-gate if (c == '*') {
250*7c478bd9Sstevel@tonic-gate while (*p == '*')
251*7c478bd9Sstevel@tonic-gate p++; /* consecutive *'s can be collapsed */
252*7c478bd9Sstevel@tonic-gate
253*7c478bd9Sstevel@tonic-gate if (*p == '\0')
254*7c478bd9Sstevel@tonic-gate return (1);
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate while (*s != '\0') {
257*7c478bd9Sstevel@tonic-gate if (fmd_strmatch(s++, p) != 0)
258*7c478bd9Sstevel@tonic-gate return (1);
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate return (0);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate } while (c == *s++);
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate return (0);
266*7c478bd9Sstevel@tonic-gate }
267