Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / promif / common / prom_string.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: prom_string.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * Copyright (c) 2000-2003 Sun Microsystems, Inc.
46 * All rights reserved.
47 * Use is subject to license terms.
48 */
49
50#pragma ident "@(#)prom_string.c 1.1 00/08/07 SMI"
51
52#include <sys/promif.h>
53#include <sys/promimpl.h>
54
55/*
56 * a version of string copy that is bounded
57 */
58char *
59prom_strncpy(register char *s1, register char *s2, size_t n)
60{
61 register char *os1 = s1;
62
63 n++;
64 while (--n != 0 && (*s1++ = *s2++) != '\0')
65 ;
66 if (n != 0)
67 while (--n != 0)
68 *s1++ = '\0';
69 return (os1);
70}
71
72void *
73prom_memccpy(register caddr_t s1, register caddr_t s2, int32_t c, size_t n)
74{
75 n++;
76 while (--n != 0 && (*s1++ = *s2++) != (uchar_t)c)
77 ;
78 if (n == 0)
79 return (NULL);
80 return ((void *)s1);
81}
82
83/*
84 * and one that knows no bounds
85 */
86char *
87prom_strcpy(register char *s1, register char *s2)
88{
89 register char *os1;
90
91 os1 = s1;
92 while (*s1++ = *s2++)
93 ;
94 return (os1);
95}
96
97void *
98prom_memcpy(register caddr_t s1, register caddr_t s2, size_t n)
99{
100 register caddr_t os1;
101
102 os1 = s1;
103 while (n--)
104 *s1++ = *s2++;
105 return ((void *)os1);
106}
107
108/*
109 * a copy of string compare that is bounded
110 */
111int
112prom_strncmp(register char *s1, register char *s2, register size_t n)
113{
114 n++;
115 if (s1 == s2)
116 return (0);
117 while (--n != 0 && *s1 == *s2++)
118 if (*s1++ == '\0')
119 return (0);
120 return ((n == 0) ? 0: (*s1 - s2[-1]));
121}
122
123/*
124 * and one that knows no bounds
125 */
126int
127prom_strcmp(register char *s1, register char *s2)
128{
129 while (*s1 == *s2++)
130 if (*s1++ == '\0')
131 return (0);
132 return (*s1 - *--s2);
133}
134
135int
136prom_memcmp(register caddr_t s1, register caddr_t s2, size_t n)
137{
138 while (*s1 == *s2++)
139 if (n-- == 0)
140 return (0);
141 return (*s1 - *--s2);
142}
143
144/*
145 * finds the length of a succession of non-NULL chars
146 */
147int
148prom_strlen(register char *s)
149{
150 register int32_t n = 0;
151
152 while (*s++)
153 n++;
154
155 return (n);
156}
157
158/*
159 * return the ptr in sp at which the character c last
160 * appears; 0 if not found
161 */
162char *
163prom_strrchr(register char *sp, register int32_t c)
164{
165 register char *r;
166
167 for (r = (char *)0; *sp != (char)0; ++sp)
168 if (*sp == c)
169 r = sp;
170 return (r);
171}
172
173/*
174 * Concatenate string s2 to string s1
175 */
176char *
177prom_strcat(register char *s1, register char *s2)
178{
179 char *os1 = s1;
180
181 while ((*s1) != ((char)0))
182 s1++; /* find the end of string s1 */
183
184 while (*s1++ = *s2++) /* Concatenate s2 */
185 ;
186 return (os1);
187}
188
189/*
190 * Return the ptr in sp at which the character c first
191 * appears; NULL if not found
192 */
193char *
194prom_strchr(register const char *sp, register int32_t c)
195{
196 do {
197 if (*sp == (char)c)
198 return ((char *)sp);
199 } while (*sp++);
200 return (NULL);
201}
202
203void *
204prom_memchr(register caddr_t sp, register int32_t c, register size_t n)
205{
206 do {
207 if (*sp == (char)c)
208 return ((void *)sp);
209 } while (n--);
210 return (NULL);
211
212}
213
214/*
215 * strstr() locates the first occurrence in the string s1 of
216 * the sequence of characters (excluding the terminating null
217 * character) in the string s2. strstr() returns a pointer
218 * to the located string, or a null pointer if the string is
219 * not found. If s2 is "", the function returns s1.
220 */
221char *
222prom_strstr(register char *s1, register char *s2)
223{
224 register char *p = s1;
225 register int32_t len = prom_strlen(s2);
226
227 if ((s2 == NULL) || (*s2 == '\0'))
228 return ((char *)s1);
229
230 for (; (p = (char *)prom_strchr(p, *s2)) != 0; p++) {
231 if (prom_strncmp(p, s2, len) == 0) {
232 return (p);
233 }
234 }
235 return (NULL);
236}
237
238void *
239prom_memset(register caddr_t s, register int c, register size_t n)
240{
241 register void *os = s;
242
243 while (n--)
244 *s++ = (uchar_t)c;
245 return ((void *)os);
246}