]> git.saurik.com Git - apple/boot.git/blob - gen/libsa/string1.c
0b4bb39130da758d23aac9978f9aaea59424665a
[apple/boot.git] / gen / libsa / string1.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* string operations */
26 #import "libsa.h"
27
28 /*#if DONT_USE_GCC_BUILT_IN_STRLEN*/
29
30 #define tolower(c) ((int)((c) & ~0x20))
31 #define toupper(c) ((int)((c) | 0x20))
32
33 int strlen(const char *s)
34 {
35 int n;
36
37 n = 0;
38 while (*s++) n++;
39 return(n);
40 }
41
42 /*#endif*/
43
44 int
45 strcmp(const char *s1, const char *s2)
46 {
47 while (*s1 && (*s1 == *s2)) {
48 s1++;
49 s2++;
50 }
51 return (*s1 - *s2);
52 }
53
54 int strncmp(const char *s1, const char *s2, size_t len)
55 {
56 register int n = len;
57 while (--n >= 0 && *s1 == *s2++)
58 if (*s1++ == '\0')
59 return(0);
60 return(n<0 ? 0 : *s1 - *--s2);
61 }
62
63 char *
64 strcpy(char *s1, const char *s2)
65 {
66 register char *ret = s1;
67 while (*s1++ = *s2++)
68 continue;
69 return ret;
70 }
71
72 char *
73 strncpy(char *s1, const char *s2, size_t n)
74 {
75 register char *ret = s1;
76 while (n && (*s1++ = *s2++))
77 n--;
78 if (!n) *s1=0;
79 return ret;
80 }
81
82 int
83 ptol(char *str)
84 {
85 register int c = *str;
86
87 if (c <= '7' && c >= '0')
88 c -= '0';
89 else if (c <= 'h' && c >= 'a')
90 c -= 'a';
91 else c = 0;
92 return c;
93 }
94
95 int
96 atoi(const char *str)
97 {
98 register int sum = 0;
99 while (*str == ' ' || *str == '\t')
100 str++;
101 while (*str >= '0' && *str <= '9') {
102 sum *= 10;
103 sum += *str++ - '0';
104 }
105 return sum;
106 }
107
108
109 char *strncat(char *s1, const char *s2, size_t n)
110 {
111 register char *ret = s1;
112 while (*s1)
113 s1++;
114 while (n-- && *s2)
115 *s1++ = *s2++;
116 *s1 = '\0';
117 return ret;
118 }
119
120 char *strcat(char *s1, const char *s2)
121 {
122 return(strncat(s1, s2, strlen(s2)));
123 }
124
125 #if STRNCASECMP
126 int strncasecmp(const char *s1, const char *s2, size_t len)
127 {
128 register int n = len;
129 while (--n >= 0 && tolower(*s1) == tolower(*s2++))
130 if (*s1++ == '\0')
131 return(0);
132 return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
133 }
134 #endif
135