]> git.saurik.com Git - apple/boot.git/blob - i386/libsa/string.c
4ed67cd4ca7fc23f64dbc450244678a3e37d6c70
[apple/boot.git] / i386 / libsa / string.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
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
9 * Source License Version 1.1 (the "License"). You may not use this file
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.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* string operations */
25
26 #include "libsa.h"
27
28 void * memset(void * dst, int val, size_t len)
29 {
30 asm( "rep; stosb"
31 : "=c" (len), "=D" (dst)
32 : "0" (len), "1" (dst), "a" (val)
33 : "memory" );
34
35 return dst;
36 }
37
38 void * memcpy(void * dst, const void * src, size_t len)
39 {
40 asm( "rep; movsb"
41 : "=c" (len), "=D" (dst), "=S" (src)
42 : "0" (len), "1" (dst), "2" (src)
43 : "memory" );
44
45 return dst;
46 }
47
48 void bcopy(const void * src, void * dst, size_t len)
49 {
50 memcpy(dst, src, len);
51 }
52
53 void bzero(void * dst, size_t len)
54 {
55 memset(dst, 0, len);
56 }
57
58 /* #if DONT_USE_GCC_BUILT_IN_STRLEN */
59
60 #define tolower(c) ((int)((c) & ~0x20))
61 #define toupper(c) ((int)((c) | 0x20))
62
63 int strlen(const char * s)
64 {
65 int n = 0;
66 while (*s++) n++;
67 return(n);
68 }
69
70 /*#endif*/
71
72 int
73 strcmp(const char * s1, const char * s2)
74 {
75 while (*s1 && (*s1 == *s2)) {
76 s1++;
77 s2++;
78 }
79 return (*s1 - *s2);
80 }
81
82 int strncmp(const char * s1, const char * s2, size_t len)
83 {
84 register int n = len;
85 while (--n >= 0 && *s1 == *s2++)
86 if (*s1++ == '\0')
87 return(0);
88 return(n<0 ? 0 : *s1 - *--s2);
89 }
90
91 char *
92 strcpy(char * s1, const char * s2)
93 {
94 register char *ret = s1;
95 while (*s1++ = *s2++)
96 continue;
97 return ret;
98 }
99
100 char *
101 strncpy(char * s1, const char * s2, size_t n)
102 {
103 register char *ret = s1;
104 while (n && (*s1++ = *s2++))
105 n--;
106 return ret;
107 }
108
109 char *
110 strlcpy(char * s1, const char * s2, size_t n)
111 {
112 register char *ret = s1;
113 while (n && (*s1++ = *s2++))
114 n--;
115 if (!n) *--s1=0;
116 return ret;
117 }
118
119 int
120 ptol(const char *str)
121 {
122 register int c = *str;
123
124 if (c <= '7' && c >= '0')
125 c -= '0';
126 else if (c <= 'h' && c >= 'a')
127 c -= 'a';
128 else c = 0;
129 return c;
130 }
131
132 int
133 atoi(const char *str)
134 {
135 register int sum = 0;
136 while (*str == ' ' || *str == '\t')
137 str++;
138 while (*str >= '0' && *str <= '9') {
139 sum *= 10;
140 sum += *str++ - '0';
141 }
142 return sum;
143 }
144
145 char *strncat(char *s1, const char *s2, size_t n)
146 {
147 register char *ret = s1;
148 while (*s1)
149 s1++;
150 while (n-- && *s2)
151 *s1++ = *s2++;
152 *s1 = '\0';
153 return ret;
154 }
155
156 char *strcat(char *s1, const char *s2)
157 {
158 return(strncat(s1, s2, strlen(s2)));
159 }
160
161 #if STRNCASECMP
162 int strncasecmp(const char *s1, const char *s2, size_t len)
163 {
164 register int n = len;
165 while (--n >= 0 && tolower(*s1) == tolower(*s2++))
166 if (*s1++ == '\0')
167 return(0);
168 return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
169 }
170 #endif