]> git.saurik.com Git - apple/boot.git/blame - i386/libsa/string.c
boot-132.tar.gz
[apple/boot.git] / i386 / libsa / string.c
CommitLineData
14c7c974 1/*
57c72a9a 2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
14c7c974
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
57c72a9a 6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
4f6e3300
A
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
57c72a9a 9 * Source License Version 2.0 (the "License"). You may not use this file
4f6e3300
A
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
14c7c974
A
13 *
14 * The Original Code and all software distributed under the License are
4f6e3300 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14c7c974
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
4f6e3300
A
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
14c7c974
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/* string operations */
25
26#include "libsa.h"
27
28void * memset(void * dst, int val, size_t len)
29{
bba600dd 30 asm volatile ( "rep; stosb"
14c7c974
A
31 : "=c" (len), "=D" (dst)
32 : "0" (len), "1" (dst), "a" (val)
33 : "memory" );
34
35 return dst;
36}
37
57c72a9a 38#if 0
14c7c974
A
39void * memcpy(void * dst, const void * src, size_t len)
40{
bba600dd 41 asm volatile ( "rep; movsb"
14c7c974
A
42 : "=c" (len), "=D" (dst), "=S" (src)
43 : "0" (len), "1" (dst), "2" (src)
44 : "memory" );
45
46 return dst;
47}
48
75b89a82 49void bcopy(const void * src, void * dst, size_t len)
14c7c974
A
50{
51 memcpy(dst, src, len);
52}
53
75b89a82 54void bzero(void * dst, size_t len)
14c7c974
A
55{
56 memset(dst, 0, len);
57}
58
57c72a9a
A
59#else
60void * memcpy(void * dst, const void * src, size_t len)
61{
bba600dd 62 asm volatile ( "cld \n\t"
57c72a9a
A
63 "movl %%ecx, %%edx \n\t"
64 "shrl $2, %%ecx \n\t"
65 "rep; movsl \n\t"
66 "movl %%edx, %%ecx \n\t"
67 "andl $3, %%ecx \n\t"
68 "rep; movsb \n\t"
69 : "=D" (dst)
70 : "c" (len), "D" (dst), "S" (src)
71 : "memory", "%edx" );
72
73 return dst;
74}
75
76void bcopy(const void * src, void * dst, size_t len)
77{
bba600dd 78 asm volatile ( "cld \n\t"
57c72a9a
A
79 "movl %%ecx, %%edx \n\t"
80 "shrl $2, %%ecx \n\t"
81 "rep; movsl \n\t"
82 "movl %%edx, %%ecx \n\t"
83 "andl $3, %%ecx \n\t"
84 "rep; movsb \n\t"
85 :
86 : "c" (len), "D" (dst), "S" (src)
87 : "memory", "%edx" );
88}
89
90void bzero(void * dst, size_t len)
91{
bba600dd 92 asm volatile ( "xorl %%eax, %%eax \n\t"
57c72a9a
A
93 "cld \n\t"
94 "movl %%ecx, %%edx \n\t"
95 "shrl $2, %%ecx \n\t"
96 "rep; stosl \n\t"
97 "movl %%edx, %%ecx \n\t"
98 "andl $3, %%ecx \n\t"
99 "rep; stosb \n\t"
100 :
101 : "c" (len), "D" (dst)
102 : "memory", "%eax" );
103}
104#endif
105
14c7c974
A
106/* #if DONT_USE_GCC_BUILT_IN_STRLEN */
107
108#define tolower(c) ((int)((c) & ~0x20))
109#define toupper(c) ((int)((c) | 0x20))
110
111int strlen(const char * s)
112{
113 int n = 0;
114 while (*s++) n++;
115 return(n);
116}
117
118/*#endif*/
119
120int
121strcmp(const char * s1, const char * s2)
122{
123 while (*s1 && (*s1 == *s2)) {
124 s1++;
125 s2++;
126 }
127 return (*s1 - *s2);
128}
129
130int strncmp(const char * s1, const char * s2, size_t len)
131{
132 register int n = len;
133 while (--n >= 0 && *s1 == *s2++)
134 if (*s1++ == '\0')
135 return(0);
136 return(n<0 ? 0 : *s1 - *--s2);
137}
138
139char *
140strcpy(char * s1, const char * s2)
141{
142 register char *ret = s1;
143 while (*s1++ = *s2++)
144 continue;
145 return ret;
146}
147
148char *
149strncpy(char * s1, const char * s2, size_t n)
150{
151 register char *ret = s1;
152 while (n && (*s1++ = *s2++))
153 n--;
f083c6c3
A
154 return ret;
155}
156
157char *
158strlcpy(char * s1, const char * s2, size_t n)
159{
160 register char *ret = s1;
161 while (n && (*s1++ = *s2++))
162 n--;
163 if (!n) *--s1=0;
14c7c974
A
164 return ret;
165}
166
167int
75b89a82 168ptol(const char *str)
14c7c974
A
169{
170 register int c = *str;
171
172 if (c <= '7' && c >= '0')
173 c -= '0';
174 else if (c <= 'h' && c >= 'a')
175 c -= 'a';
176 else c = 0;
177 return c;
178}
179
180int
181atoi(const char *str)
182{
183 register int sum = 0;
184 while (*str == ' ' || *str == '\t')
185 str++;
186 while (*str >= '0' && *str <= '9') {
187 sum *= 10;
188 sum += *str++ - '0';
189 }
190 return sum;
191}
192
193char *strncat(char *s1, const char *s2, size_t n)
194{
195 register char *ret = s1;
196 while (*s1)
197 s1++;
198 while (n-- && *s2)
199 *s1++ = *s2++;
200 *s1 = '\0';
201 return ret;
202}
203
204char *strcat(char *s1, const char *s2)
205{
206 return(strncat(s1, s2, strlen(s2)));
207}
208
209#if STRNCASECMP
210int strncasecmp(const char *s1, const char *s2, size_t len)
211{
212 register int n = len;
213 while (--n >= 0 && tolower(*s1) == tolower(*s2++))
214 if (*s1++ == '\0')
215 return(0);
216 return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
217}
218#endif