]> git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/libclite.subproj/string.c
BootX-59.1.1.tar.gz
[apple/bootx.git] / bootx.tproj / libclite.subproj / string.c
1 /*
2 * Copyright (c) 2000 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 /*
26 * string.c - string operations.
27 *
28 * Copyright (c) 1998-2000 Apple Computer, Inc.
29 *
30 * DRI: Josh de Cesare
31 */
32
33 #include "libclite.h"
34
35 /*#if DONT_USE_GCC_BUILT_IN_STRLEN*/
36
37 #define tolower(c) ((int)((c) & ~0x20))
38 #define toupper(c) ((int)((c) | 0x20))
39
40 int strlen(const char *s)
41 {
42 int n;
43
44 n = 0;
45 while (*s++) n++;
46 return(n);
47 }
48
49 /*#endif*/
50
51 int
52 strcmp(const char *s1, const char *s2)
53 {
54 while (*s1 && (*s1 == *s2)) {
55 s1++;
56 s2++;
57 }
58 return (*s1 - *s2);
59 }
60
61 int strncmp(const char *s1, const char *s2, size_t len)
62 {
63 register int n = len;
64 while (--n >= 0 && *s1 == *s2++)
65 if (*s1++ == '\0')
66 return(0);
67 return(n<0 ? 0 : *s1 - *--s2);
68 }
69
70 char *
71 strcpy(char *s1, const char *s2)
72 {
73 register char *ret = s1;
74 while (*s1++ = *s2++)
75 continue;
76 return ret;
77 }
78
79 char *
80 strncpy(char *s1, const char *s2, size_t n)
81 {
82 register char *ret = s1;
83 while (n && (*s1++ = *s2++))
84 n--;
85 if (!n) *s1=0;
86 return ret;
87 }
88
89 int
90 ptol(char *str)
91 {
92 register int c = *str;
93
94 if (c <= '7' && c >= '0')
95 c -= '0';
96 else if (c <= 'h' && c >= 'a')
97 c -= 'a';
98 else c = 0;
99 return c;
100 }
101
102 int
103 atoi(const char *str)
104 {
105 register int sum = 0;
106 while (*str == ' ' || *str == '\t')
107 str++;
108 while (*str >= '0' && *str <= '9') {
109 sum *= 10;
110 sum += *str++ - '0';
111 }
112 return sum;
113 }
114
115
116 char *strncat(char *s1, const char *s2, size_t n)
117 {
118 register char *ret = s1;
119 while (*s1)
120 s1++;
121 while (n-- && *s2)
122 *s1++ = *s2++;
123 *s1 = '\0';
124 return ret;
125 }
126
127 char *strcat(char *s1, const char *s2)
128 {
129 return(strncat(s1, s2, strlen(s2)));
130 }
131
132 /*#if STRNCASECMP*/
133 int strncasecmp(const char *s1, const char *s2, size_t len)
134 {
135 register int n = len;
136 while (--n >= 0 && tolower(*s1) == tolower(*s2++))
137 if (*s1++ == '\0')
138 return(0);
139 return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
140 }
141 /*#endif*/
142