xref: /illumos-gate/usr/src/common/smbsrv/smb_string.c (revision 4a7ceb24cfcc0a97f96d86cfe5852ae445b50e57)
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 2007 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/alloc.h>
41 #include <smbsrv/string.h>
42 #include <smbsrv/ctype.h>
43 
44 
45 /*
46  * strsubst
47  *
48  * Scan a string replacing all occurrences of orgchar with newchar.
49  * Returns a pointer to s, or null of s is null.
50  */
51 char *
52 strsubst(char *s, char orgchar, char newchar)
53 {
54 	char *p = s;
55 
56 	if (p == 0)
57 		return (0);
58 
59 	while (*p) {
60 		if (*p == orgchar)
61 			*p = newchar;
62 		++p;
63 	}
64 
65 	return (s);
66 }
67 
68 /*
69  * strcanon
70  *
71  * Normalize a string by reducing all the repeated characters in
72  * buf as defined by class. For example;
73  *
74  *		char *buf = strdup("/d1//d2//d3\\\\d4\\\\f1.txt");
75  *		strcanon(buf, "/\\");
76  *
77  * Would result in buf containing the following string:
78  *
79  *		/d1/d2/d3\d4\f1.txt
80  *
81  * This function modifies the contents of buf in place and returns
82  * a pointer to buf.
83  */
84 char *
85 strcanon(char *buf, const char *class)
86 {
87 	char *p = buf;
88 	char *q = buf;
89 	char *r;
90 
91 	while (*p) {
92 		*q++ = *p;
93 
94 		if ((r = strchr(class, *p)) != 0) {
95 			while (*p == *r)
96 				++p;
97 		} else
98 			++p;
99 	}
100 
101 	*q = '\0';
102 	return (buf);
103 }
104