Hide b_freeze_* under ZFS_DEBUG
[zfs.git] / lib / libuutil / uu_ident.c
blobd0cab0a64d0929f184e388244e7e73ef86ef9d34
1 /*
2 * CDDL HEADER START
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
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 #include "libuutil_common.h"
31 #include <string.h>
34 * We require names of the form:
35 * [provider,]identifier[/[provider,]identifier]...
37 * Where provider is either a stock symbol (SUNW) or a java-style reversed
38 * domain name (com.sun).
40 * Both providers and identifiers must start with a letter, and may
41 * only contain alphanumerics, dashes, and underlines. Providers
42 * may also contain periods.
44 * Note that we do _not_ use the macros in <ctype.h>, since they are affected
45 * by the current locale settings.
48 #define IS_ALPHA(c) \
49 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
51 #define IS_DIGIT(c) \
52 ((c) >= '0' && (c) <= '9')
54 static int
55 is_valid_ident(const char *s, const char *e, int allowdot)
57 char c;
59 if (s >= e)
60 return (0); /* name is empty */
62 c = *s++;
63 if (!IS_ALPHA(c))
64 return (0); /* does not start with letter */
66 while (s < e && (c = *s++) != 0) {
67 if (IS_ALPHA(c) || IS_DIGIT(c) || c == '-' || c == '_' ||
68 (allowdot && c == '.'))
69 continue;
70 return (0); /* invalid character */
72 return (1);
75 static int
76 is_valid_component(const char *b, const char *e, uint_t flags)
78 char *sp;
80 if (flags & UU_NAME_DOMAIN) {
81 sp = strchr(b, ',');
82 if (sp != NULL && sp < e) {
83 if (!is_valid_ident(b, sp, 1))
84 return (0);
85 b = sp + 1;
89 return (is_valid_ident(b, e, 0));
92 int
93 uu_check_name(const char *name, uint_t flags)
95 const char *end = name + strlen(name);
96 const char *p;
98 if (flags & ~(UU_NAME_DOMAIN | UU_NAME_PATH)) {
99 uu_set_error(UU_ERROR_UNKNOWN_FLAG);
100 return (-1);
103 if (!(flags & UU_NAME_PATH)) {
104 if (!is_valid_component(name, end, flags))
105 goto bad;
106 return (0);
109 while ((p = strchr(name, '/')) != NULL) {
110 if (!is_valid_component(name, p - 1, flags))
111 goto bad;
112 name = p + 1;
114 if (!is_valid_component(name, end, flags))
115 goto bad;
117 return (0);
119 bad:
120 uu_set_error(UU_ERROR_INVALID_ARGUMENT);
121 return (-1);