]> git.saurik.com Git - apple/boot.git/blame - i386/libsa/string.c
boot-80.1.tar.gz
[apple/boot.git] / i386 / libsa / string.c
CommitLineData
14c7c974
A
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
28void * 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
38void * 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
48void bcopy(const void * src, void * dst, int len)
49{
50 memcpy(dst, src, len);
51}
52
53void bzero(void * dst, int 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
63int strlen(const char * s)
64{
65 int n = 0;
66 while (*s++) n++;
67 return(n);
68}
69
70/*#endif*/
71
72int
73strcmp(const char * s1, const char * s2)
74{
75 while (*s1 && (*s1 == *s2)) {
76 s1++;
77 s2++;
78 }
79 return (*s1 - *s2);
80}
81
82int 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
91char *
92strcpy(char * s1, const char * s2)
93{
94 register char *ret = s1;
95 while (*s1++ = *s2++)
96 continue;
97 return ret;
98}
99
100char *
101strncpy(char * s1, const char * s2, size_t n)
102{
103 register char *ret = s1;
104 while (n && (*s1++ = *s2++))
105 n--;
106 if (!n) *s1=0;
107 return ret;
108}
109
110int
111ptol(char *str)
112{
113 register int c = *str;
114
115 if (c <= '7' && c >= '0')
116 c -= '0';
117 else if (c <= 'h' && c >= 'a')
118 c -= 'a';
119 else c = 0;
120 return c;
121}
122
123int
124atoi(const char *str)
125{
126 register int sum = 0;
127 while (*str == ' ' || *str == '\t')
128 str++;
129 while (*str >= '0' && *str <= '9') {
130 sum *= 10;
131 sum += *str++ - '0';
132 }
133 return sum;
134}
135
136char *strncat(char *s1, const char *s2, size_t n)
137{
138 register char *ret = s1;
139 while (*s1)
140 s1++;
141 while (n-- && *s2)
142 *s1++ = *s2++;
143 *s1 = '\0';
144 return ret;
145}
146
147char *strcat(char *s1, const char *s2)
148{
149 return(strncat(s1, s2, strlen(s2)));
150}
151
152#if STRNCASECMP
153int strncasecmp(const char *s1, const char *s2, size_t len)
154{
155 register int n = len;
156 while (--n >= 0 && tolower(*s1) == tolower(*s2++))
157 if (*s1++ == '\0')
158 return(0);
159 return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
160}
161#endif