FreeBSD/Linux Kernel Cross Reference
sys/kern/strings.c
1 /*
2 * Mach Operating System
3 * Copyright (c) 1993 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * HISTORY
28 * $Log: strings.c,v $
29 * Revision 2.3 93/11/17 17:24:57 dbg
30 * Simplified. Fixed boundary conditions in strncpy, strncmp.
31 * [93/11/08 dbg]
32 *
33 * Revision 2.2 93/03/09 10:55:36 danner
34 * Created from Bob's new code in libmach. Added strncmp().
35 * ANSI-fied, prototypes the way GCC wants them (signed char* ??).
36 * [93/03/06 af]
37 *
38 */
39 /*
40 * File: strings.c
41 * Author: Robert V. Baron, Carnegie Mellon University
42 * Date: ??/92
43 *
44 * String functions.
45 */
46
47 #include <kern/strings.h> /* make sure we sell the truth */
48
49 #ifdef strcpy
50 #undef strcmp
51 #undef strncmp
52 #undef strcpy
53 #undef strncpy
54 #undef strlen
55 #endif
56
57 /*
58 * Abstract:
59 * strcmp (s1, s2) compares the strings "s1" and "s2".
60 * It returns 0 if the strings are identical. It returns
61 * > 0 if the first character that differs in the two strings
62 * is larger in s1 than in s2 or if s1 is longer than s2 and
63 * the contents are identical up to the length of s2.
64 * It returns < 0 if the first differing character is smaller
65 * in s1 than in s2 or if s1 is shorter than s2 and the
66 * contents are identical upto the length of s1.
67 */
68
69 int
70 strcmp(
71 register const char *s1,
72 register const char *s2)
73 {
74 register unsigned int a, b;
75
76 do {
77 a = *s1++;
78 b = *s2++;
79 if (a != b)
80 return a-b; /* includes case when
81 'a' is zero and 'b' is not zero
82 or vice versa */
83 } while (a != '\0');
84
85 return 0; /* both are zero */
86 }
87
88
89 /*
90 * Abstract:
91 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
92 * in exactly the same way as strcmp does. Except the
93 * comparison runs for at most "n" characters.
94 */
95
96 int
97 strncmp(
98 register const char *s1,
99 register const char *s2,
100 unsigned long n)
101 {
102 register unsigned int a, b;
103
104 while (n != 0) {
105 a = *s1++;
106 b = *s2++;
107 if (a != b)
108 return a-b; /* includes case when
109 'a' is zero and 'b' is not zero
110 or vice versa */
111 if (a == '\0')
112 return 0; /* both are zero */
113 n--;
114 }
115
116 return 0;
117 }
118
119
120 /*
121 * Abstract:
122 * strcpy copies the contents of the string "from" including
123 * the null terminator to the string "to". A pointer to "to"
124 * is returned.
125 */
126
127 char *
128 strcpy(
129 register char *to,
130 register const char *from)
131 {
132 register char *ret = to;
133
134 while ((*to++ = *from++) != '\0')
135 continue;
136
137 return ret;
138 }
139
140 /*
141 * Abstract:
142 * strncpy copies "count" characters from the "from" string to
143 * the "to" string. If "from" contains less than "count" characters
144 * "to" will be padded with null characters until exactly "count"
145 * characters have been written. The return value is a pointer
146 * to the "to" string.
147 */
148
149 char *
150 strncpy(
151 register char *to,
152 register const char *from,
153 register unsigned long count)
154 {
155 register char *ret = to;
156
157 while (count != 0) {
158 if ((*to++ = *from++) == '\0')
159 break;
160 --count;
161 }
162
163 while (count != 0) {
164 *to++ = '\0';
165 count--;
166 }
167
168 return ret;
169 }
170
171 /*
172 * Abstract:
173 * strlen returns the number of characters in "string" preceeding
174 * the terminating null character.
175 */
176
177 unsigned long
178 strlen(
179 register const char *string)
180 {
181 register const char *ret = string;
182
183 while (*string++ != '\0')
184 continue;
185
186 return string - 1 - ret;
187 }
Cache object: 6ab076fc4a1ba77c43b09a710846fb9a
|