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.2 93/03/09 10:55:36 danner
30 * Created from Bob's new code in libmach. Added strncmp().
31 * ANSI-fied, prototypes the way GCC wants them (signed char* ??).
32 * [93/03/06 af]
33 *
34 */
35 /*
36 * File: strings.c
37 * Author: Robert V. Baron, Carnegie Mellon University
38 * Date: ??/92
39 *
40 * String functions.
41 */
42
43 #include <kern/strings.h> /* make sure we sell the truth */
44
45 #ifdef strcpy
46 #undef strcmp
47 #undef strncmp
48 #undef strcpy
49 #undef strncpy
50 #undef strlen
51 #endif
52
53 /*
54 * Abstract:
55 * strcmp (s1, s2) compares the strings "s1" and "s2".
56 * It returns 0 if the strings are identical. It returns
57 * > 0 if the first character that differs into two strings
58 * is larger in s1 than in s2 or if s1 is longer than s2 and
59 * the contents are identical up to the length of s2.
60 * It returns < 0 if the first differing character is smaller
61 * in s1 than in s2 or if s1 is shorter than s2 and the
62 * contents are identical upto the length of s1.
63 */
64
65 int
66 strcmp(
67 register const char *s1,
68 register const char *s2)
69 {
70 register unsigned int a, b;
71
72 while ( (a = *s1++), (b = *s2++), a && b) {
73 if (a != b)
74 return (a-b);
75 }
76
77 return a-b;
78 }
79
80
81 /*
82 * Abstract:
83 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
84 * in exactly the same way as strcmp does. Except the
85 * comparison runs for at most "n" characters.
86 */
87
88 int
89 strncmp(
90 register const char *s1,
91 register const char *s2,
92 unsigned long n)
93 {
94 register unsigned int a, b;
95
96 while ( n, (a = *s1++), (b = *s2++), a && b) {
97 if (a != b)
98 return (a-b);
99 n--;
100 }
101
102 return n ? a-b : 0;
103 }
104
105
106 /*
107 * Abstract:
108 * strcpy copies the contents of the string "from" including
109 * the null terminator to the string "to". A pointer to "to"
110 * is returned.
111 */
112
113 char *
114 strcpy(
115 register char *to,
116 register const char *from)
117 {
118 register char *ret = to;
119
120 while(*to++ = *from++);
121
122 return ret;
123 }
124
125 /*
126 * Abstract:
127 * strncpy copies "count" characters from the "from" string to
128 * the "to" string. If "from" contains less than "count" characters
129 * "to" will be padded with null characters until exactly "count"
130 * characters have been written. The return value is a pointer
131 * to the "to" string.
132 */
133
134 char *
135 strncpy(
136 register char *to,
137 register const char *from,
138 register unsigned long count)
139 {
140 register char *ret = to;
141
142 while (count-- > 0 && (*to++ = *from++));
143
144 while (count-- > 0)
145 *to++ = '\0';
146
147 return ret;
148 }
149
150 /*
151 * Abstract:
152 * strlen returns the number of characters in "string" preceeding
153 * the terminating null character.
154 */
155
156 unsigned long
157 strlen(
158 register const char *string)
159 {
160 register const char *ret = string;
161
162 while (*string++);
163
164 return string - 1 - ret;
165
166 }
Cache object: 7e7d5bde2c36b0c12f777dadf5eebf07
|