xref: /illumos-gate/usr/src/common/smbsrv/smb_string.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Implementation of some of the string functions.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef _KERNEL
33 #include <sys/types.h>
34 #include <sys/sunddi.h>
35 #else
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
39 #endif
40 #include <smbsrv/string.h>
41 #include <smbsrv/ctype.h>
42 
43 
44 /*
45  * strsubst
46  *
47  * Scan a string replacing all occurrences of orgchar with newchar.
48  * Returns a pointer to s, or null of s is null.
49  */
50 char *
51 strsubst(char *s, char orgchar, char newchar)
52 {
53 	char *p = s;
54 
55 	if (p == 0)
56 		return (0);
57 
58 	while (*p) {
59 		if (*p == orgchar)
60 			*p = newchar;
61 		++p;
62 	}
63 
64 	return (s);
65 }
66 
67 /*
68  * strcanon
69  *
70  * Normalize a string by reducing all the repeated characters in
71  * buf as defined by class. For example;
72  *
73  *		char *buf = strdup("/d1//d2//d3\\\\d4\\\\f1.txt");
74  *		strcanon(buf, "/\\");
75  *
76  * Would result in buf containing the following string:
77  *
78  *		/d1/d2/d3\d4\f1.txt
79  *
80  * This function modifies the contents of buf in place and returns
81  * a pointer to buf.
82  */
83 char *
84 strcanon(char *buf, const char *class)
85 {
86 	char *p = buf;
87 	char *q = buf;
88 	char *r;
89 
90 	while (*p) {
91 		*q++ = *p;
92 
93 		if ((r = strchr(class, *p)) != 0) {
94 			while (*p == *r)
95 				++p;
96 		} else
97 			++p;
98 	}
99 
100 	*q = '\0';
101 	return (buf);
102 }
103