4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <fmd_string.h>
35 fmd_strdup(const char *s
, int flags
)
40 p
= fmd_alloc(strlen(s
) + 1, flags
);
54 fmd_free(s
, strlen(s
) + 1);
58 fmd_strbasename(const char *s
)
60 const char *p
= strrchr(s
, '/');
69 fmd_strdirname(char *s
)
71 static char slash
[] = "/";
72 static char dot
[] = ".";
75 if (s
== NULL
|| *s
== '\0')
78 for (p
= s
+ strlen(s
); p
!= s
&& *--p
== '/'; )
81 if (p
== s
&& *p
== '/')
86 while (*p
== '/' && p
!= s
)
97 fmd_strhash(const char *key
)
102 for (p
= key
; *p
!= '\0'; p
++) {
105 if ((g
= (h
& 0xf0000000)) != 0) {
115 * Transform string s inline, converting each embedded C escape sequence string
116 * to the corresponding character. For example, the substring "\n" is replaced
117 * by an inline '\n' character. The length of the resulting string is returned.
120 fmd_stresc2chr(char *s
)
126 for (p
= q
= s
; (c
= *p
) != '\0'; p
++) {
140 if (*p
>= '0' && *p
<= '7') {
141 c
= c
* 8 + *p
++ - '0';
143 if (*p
>= '0' && *p
<= '7')
144 c
= c
* 8 + *p
- '0';
176 for (x
= 0; (c
= *++p
) != '\0'; ) {
177 if (c
>= '0' && c
<= '9')
178 x
= x
* 16 + c
- '0';
179 else if (c
>= 'a' && c
<= 'f')
180 x
= x
* 16 + c
- 'a' + 10;
181 else if (c
>= 'A' && c
<= 'F')
182 x
= x
* 16 + c
- 'A' + 10;
202 if ((esc
= c
== '\\') == 0)
208 return ((size_t)(q
- s
));
212 * We require that identifiers for buffers, statistics, and properties conform
213 * to the regular expression [a-zA-Z0-9\-_.]. If check_prefixes is set, we
214 * also flag strings that begin with a set of prefixes reserved for use by fmd.
217 fmd_strbadid(const char *s
, int check_prefixes
)
222 while ((c
= *s
++) != '\0') {
223 if (!isupper(c
) && !islower(c
) &&
224 !isdigit(c
) && c
!= '-' && c
!= '_' && c
!= '.')
228 if (check_prefixes
&& (s0
[0] == '_' || s0
[0] == '.' ||
229 strncmp(s0
, "fmd_", 4) == 0 || strncmp(s0
, "FMD_", 4) == 0 ||
230 strncmp(s0
, "fmd.", 4) == 0 || strncmp(s0
, "FMD.", 4) == 0))
237 fmd_strmatch(const char *s
, const char *p
)
245 s
= ""; /* treat NULL string as the empty string */
248 if ((c
= *p
++) == '\0')
253 p
++; /* consecutive *'s can be collapsed */
259 if (fmd_strmatch(s
++, p
) != 0)