xref: /titanic_44/usr/src/cmd/fm/fmd/common/fmd_string.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <strings.h>
30*7c478bd9Sstevel@tonic-gate #include <ctype.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <fmd_string.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate char *
fmd_strdup(const char * s,int flags)35*7c478bd9Sstevel@tonic-gate fmd_strdup(const char *s, int flags)
36*7c478bd9Sstevel@tonic-gate {
37*7c478bd9Sstevel@tonic-gate 	char *p;
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate 	if (s != NULL)
40*7c478bd9Sstevel@tonic-gate 		p = fmd_alloc(strlen(s) + 1, flags);
41*7c478bd9Sstevel@tonic-gate 	else
42*7c478bd9Sstevel@tonic-gate 		p = NULL;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	if (p != NULL)
45*7c478bd9Sstevel@tonic-gate 		(void) strcpy(p, s);
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	return (p);
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate void
fmd_strfree(char * s)51*7c478bd9Sstevel@tonic-gate fmd_strfree(char *s)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	if (s != NULL)
54*7c478bd9Sstevel@tonic-gate 		fmd_free(s, strlen(s) + 1);
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate const char *
fmd_strbasename(const char * s)58*7c478bd9Sstevel@tonic-gate fmd_strbasename(const char *s)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	const char *p = strrchr(s, '/');
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
63*7c478bd9Sstevel@tonic-gate 		return (s);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	return (++p);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate char *
fmd_strdirname(char * s)69*7c478bd9Sstevel@tonic-gate fmd_strdirname(char *s)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	static char slash[] = "/";
72*7c478bd9Sstevel@tonic-gate 	static char dot[] = ".";
73*7c478bd9Sstevel@tonic-gate 	char *p;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (s == NULL || *s == '\0')
76*7c478bd9Sstevel@tonic-gate 		return (dot);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	for (p = s + strlen(s); p != s && *--p == '/'; )
79*7c478bd9Sstevel@tonic-gate 		continue;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	if (p == s && *p == '/')
82*7c478bd9Sstevel@tonic-gate 		return (slash);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	while (p != s) {
85*7c478bd9Sstevel@tonic-gate 		if (*--p == '/') {
86*7c478bd9Sstevel@tonic-gate 			while (*p == '/' && p != s)
87*7c478bd9Sstevel@tonic-gate 				p--;
88*7c478bd9Sstevel@tonic-gate 			*++p = '\0';
89*7c478bd9Sstevel@tonic-gate 			return (s);
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	return (dot);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate ulong_t
fmd_strhash(const char * key)97*7c478bd9Sstevel@tonic-gate fmd_strhash(const char *key)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	ulong_t g, h = 0;
100*7c478bd9Sstevel@tonic-gate 	const char *p;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	for (p = key; *p != '\0'; p++) {
103*7c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
106*7c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
107*7c478bd9Sstevel@tonic-gate 			h ^= g;
108*7c478bd9Sstevel@tonic-gate 		}
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	return (h);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /*
115*7c478bd9Sstevel@tonic-gate  * Transform string s inline, converting each embedded C escape sequence string
116*7c478bd9Sstevel@tonic-gate  * to the corresponding character.  For example, the substring "\n" is replaced
117*7c478bd9Sstevel@tonic-gate  * by an inline '\n' character.  The length of the resulting string is returned.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate size_t
fmd_stresc2chr(char * s)120*7c478bd9Sstevel@tonic-gate fmd_stresc2chr(char *s)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	char *p, *q, c;
123*7c478bd9Sstevel@tonic-gate 	int esc = 0;
124*7c478bd9Sstevel@tonic-gate 	int x;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	for (p = q = s; (c = *p) != '\0'; p++) {
127*7c478bd9Sstevel@tonic-gate 		if (esc) {
128*7c478bd9Sstevel@tonic-gate 			switch (c) {
129*7c478bd9Sstevel@tonic-gate 			case '0':
130*7c478bd9Sstevel@tonic-gate 			case '1':
131*7c478bd9Sstevel@tonic-gate 			case '2':
132*7c478bd9Sstevel@tonic-gate 			case '3':
133*7c478bd9Sstevel@tonic-gate 			case '4':
134*7c478bd9Sstevel@tonic-gate 			case '5':
135*7c478bd9Sstevel@tonic-gate 			case '6':
136*7c478bd9Sstevel@tonic-gate 			case '7':
137*7c478bd9Sstevel@tonic-gate 				c -= '0';
138*7c478bd9Sstevel@tonic-gate 				p++;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 				if (*p >= '0' && *p <= '7') {
141*7c478bd9Sstevel@tonic-gate 					c = c * 8 + *p++ - '0';
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 					if (*p >= '0' && *p <= '7')
144*7c478bd9Sstevel@tonic-gate 						c = c * 8 + *p - '0';
145*7c478bd9Sstevel@tonic-gate 					else
146*7c478bd9Sstevel@tonic-gate 						p--;
147*7c478bd9Sstevel@tonic-gate 				} else
148*7c478bd9Sstevel@tonic-gate 					p--;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 				*q++ = c;
151*7c478bd9Sstevel@tonic-gate 				break;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 			case 'a':
154*7c478bd9Sstevel@tonic-gate 				*q++ = '\a';
155*7c478bd9Sstevel@tonic-gate 				break;
156*7c478bd9Sstevel@tonic-gate 			case 'b':
157*7c478bd9Sstevel@tonic-gate 				*q++ = '\b';
158*7c478bd9Sstevel@tonic-gate 				break;
159*7c478bd9Sstevel@tonic-gate 			case 'f':
160*7c478bd9Sstevel@tonic-gate 				*q++ = '\f';
161*7c478bd9Sstevel@tonic-gate 				break;
162*7c478bd9Sstevel@tonic-gate 			case 'n':
163*7c478bd9Sstevel@tonic-gate 				*q++ = '\n';
164*7c478bd9Sstevel@tonic-gate 				break;
165*7c478bd9Sstevel@tonic-gate 			case 'r':
166*7c478bd9Sstevel@tonic-gate 				*q++ = '\r';
167*7c478bd9Sstevel@tonic-gate 				break;
168*7c478bd9Sstevel@tonic-gate 			case 't':
169*7c478bd9Sstevel@tonic-gate 				*q++ = '\t';
170*7c478bd9Sstevel@tonic-gate 				break;
171*7c478bd9Sstevel@tonic-gate 			case 'v':
172*7c478bd9Sstevel@tonic-gate 				*q++ = '\v';
173*7c478bd9Sstevel@tonic-gate 				break;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 			case 'x':
176*7c478bd9Sstevel@tonic-gate 				for (x = 0; (c = *++p) != '\0'; ) {
177*7c478bd9Sstevel@tonic-gate 					if (c >= '0' && c <= '9')
178*7c478bd9Sstevel@tonic-gate 						x = x * 16 + c - '0';
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 if (c >= 'A' && c <= 'F')
182*7c478bd9Sstevel@tonic-gate 						x = x * 16 + c - 'A' + 10;
183*7c478bd9Sstevel@tonic-gate 					else
184*7c478bd9Sstevel@tonic-gate 						break;
185*7c478bd9Sstevel@tonic-gate 				}
186*7c478bd9Sstevel@tonic-gate 				*q++ = (char)x;
187*7c478bd9Sstevel@tonic-gate 				p--;
188*7c478bd9Sstevel@tonic-gate 				break;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 			case '"':
191*7c478bd9Sstevel@tonic-gate 			case '\\':
192*7c478bd9Sstevel@tonic-gate 				*q++ = c;
193*7c478bd9Sstevel@tonic-gate 				break;
194*7c478bd9Sstevel@tonic-gate 			default:
195*7c478bd9Sstevel@tonic-gate 				*q++ = '\\';
196*7c478bd9Sstevel@tonic-gate 				*q++ = c;
197*7c478bd9Sstevel@tonic-gate 			}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 			esc = 0;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 		} else {
202*7c478bd9Sstevel@tonic-gate 			if ((esc = c == '\\') == 0)
203*7c478bd9Sstevel@tonic-gate 				*q++ = c;
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	*q = '\0';
208*7c478bd9Sstevel@tonic-gate 	return ((size_t)(q - s));
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  * We require that identifiers for buffers, statistics, and properties conform
213*7c478bd9Sstevel@tonic-gate  * to the regular expression [a-zA-Z0-9\-_.].  If check_prefixes is set, we
214*7c478bd9Sstevel@tonic-gate  * also flag strings that begin with a set of prefixes reserved for use by fmd.
215*7c478bd9Sstevel@tonic-gate  */
216*7c478bd9Sstevel@tonic-gate const char *
fmd_strbadid(const char * s,int check_prefixes)217*7c478bd9Sstevel@tonic-gate fmd_strbadid(const char *s, int check_prefixes)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate 	const char *s0 = s;
220*7c478bd9Sstevel@tonic-gate 	int c = *s++;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0') {
223*7c478bd9Sstevel@tonic-gate 		if (!isupper(c) && !islower(c) &&
224*7c478bd9Sstevel@tonic-gate 		    !isdigit(c) && c != '-' && c != '_' && c != '.')
225*7c478bd9Sstevel@tonic-gate 			return (s - 1);
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	if (check_prefixes && (s0[0] == '_' || s0[0] == '.' ||
229*7c478bd9Sstevel@tonic-gate 	    strncmp(s0, "fmd_", 4) == 0 || strncmp(s0, "FMD_", 4) == 0 ||
230*7c478bd9Sstevel@tonic-gate 	    strncmp(s0, "fmd.", 4) == 0 || strncmp(s0, "FMD.", 4) == 0))
231*7c478bd9Sstevel@tonic-gate 		return (s0);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	return (NULL);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate int
fmd_strmatch(const char * s,const char * p)237*7c478bd9Sstevel@tonic-gate fmd_strmatch(const char *s, const char *p)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	char c;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
242*7c478bd9Sstevel@tonic-gate 		return (0);
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
245*7c478bd9Sstevel@tonic-gate 		s = ""; /* treat NULL string as the empty string */
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	do {
248*7c478bd9Sstevel@tonic-gate 		if ((c = *p++) == '\0')
249*7c478bd9Sstevel@tonic-gate 			return (*s == '\0');
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 		if (c == '*') {
252*7c478bd9Sstevel@tonic-gate 			while (*p == '*')
253*7c478bd9Sstevel@tonic-gate 				p++; /* consecutive *'s can be collapsed */
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 			if (*p == '\0')
256*7c478bd9Sstevel@tonic-gate 				return (1);
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 			while (*s != '\0') {
259*7c478bd9Sstevel@tonic-gate 				if (fmd_strmatch(s++, p) != 0)
260*7c478bd9Sstevel@tonic-gate 					return (1);
261*7c478bd9Sstevel@tonic-gate 			}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 			return (0);
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 	} while (c == *s++);
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	return (0);
268*7c478bd9Sstevel@tonic-gate }
269