xref: /illumos-gate/usr/src/psm/promif/ieee1275/common/prom_string.c (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1991-1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * The routines are NOT part of the interface, merely internal
31  * utilities which assist in making the interface standalone.
32  */
33 
34 #include <sys/promif.h>
35 #include <sys/promimpl.h>
36 
37 /*
38  * a version of string copy that is bounded
39  */
40 char *
41 prom_strncpy(register char *s1, register char *s2, size_t n)
42 {
43 	register char *os1 = s1;
44 
45 	n++;
46 	while (--n != 0 && (*s1++ = *s2++) != '\0')
47 		;
48 	if (n != 0)
49 		while (--n != 0)
50 			*s1++ = '\0';
51 	return (os1);
52 }
53 
54 /*
55  * and one that knows no bounds
56  */
57 char *
58 prom_strcpy(register char *s1, register char *s2)
59 {
60 	register char *os1;
61 
62 	os1 = s1;
63 	while (*s1++ = *s2++)
64 		;
65 	return (os1);
66 }
67 
68 /*
69  * a copy of string compare that is bounded
70  */
71 int
72 prom_strncmp(register char *s1, register char *s2, register size_t n)
73 {
74 	n++;
75 	if (s1 == s2)
76 		return (0);
77 	while (--n != 0 && *s1 == *s2++)
78 		if (*s1++ == '\0')
79 			return (0);
80 	return ((n == 0) ? 0: (*s1 - s2[-1]));
81 }
82 
83 /*
84  * and one that knows no bounds
85  */
86 int
87 prom_strcmp(register char *s1, register char *s2)
88 {
89 	while (*s1 == *s2++)
90 		if (*s1++ == '\0')
91 			return (0);
92 	return (*s1 - *--s2);
93 }
94 
95 /*
96  * finds the length of a succession of non-NULL chars
97  */
98 int
99 prom_strlen(register char *s)
100 {
101 	register int n = 0;
102 
103 	while (*s++)
104 		n++;
105 
106 	return (n);
107 }
108 
109 /*
110  * return the ptr in sp at which the character c last
111  * appears; 0 if not found
112  */
113 char *
114 prom_strrchr(register char *sp, register char c)
115 {
116 	register char *r;
117 
118 	for (r = (char *)0; *sp != (char)0; ++sp)
119 		if (*sp == c)
120 			r = sp;
121 	return (r);
122 }
123 
124 /*
125  * Concatenate string s2 to string s1
126  */
127 char *
128 prom_strcat(register char *s1, register char *s2)
129 {
130 	char *os1 = s1;
131 
132 	while ((*s1) != ((char)0))
133 		s1++;		/* find the end of string s1 */
134 
135 	while (*s1++ = *s2++)	/* Concatenate s2 */
136 		;
137 	return (os1);
138 }
139 
140 /*
141  * Return the ptr in sp at which the character c first
142  * appears; NULL if not found
143  */
144 char *
145 prom_strchr(register const char *sp, register int c)
146 {
147 
148 	do {
149 		if (*sp == (char)c)
150 			return ((char *)sp);
151 	} while (*sp++);
152 	return (NULL);
153 }
154