|
|
1.1 root 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 [email protected]
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: * File: strings.c
28: * Author: Robert V. Baron, Carnegie Mellon University
29: * Date: ??/92
30: *
31: * String functions.
32: */
33:
1.1.1.2 ! root 34: #include <string.h>
1.1 root 35:
36: #ifdef strcpy
37: #undef strcmp
38: #undef strncmp
39: #undef strcpy
40: #undef strncpy
41: #undef strlen
42: #endif
43:
44: /*
45: * Abstract:
46: * strcmp (s1, s2) compares the strings "s1" and "s2".
47: * It returns 0 if the strings are identical. It returns
48: * > 0 if the first character that differs in the two strings
49: * is larger in s1 than in s2 or if s1 is longer than s2 and
50: * the contents are identical up to the length of s2.
51: * It returns < 0 if the first differing character is smaller
52: * in s1 than in s2 or if s1 is shorter than s2 and the
53: * contents are identical upto the length of s1.
54: */
55:
56: int
57: strcmp(
58: register const char *s1,
59: register const char *s2)
60: {
61: register unsigned int a, b;
62:
63: do {
64: a = *s1++;
65: b = *s2++;
66: if (a != b)
67: return a-b; /* includes case when
68: 'a' is zero and 'b' is not zero
69: or vice versa */
70: } while (a != '\0');
71:
72: return 0; /* both are zero */
73: }
74:
75:
76: /*
77: * Abstract:
78: * strncmp (s1, s2, n) compares the strings "s1" and "s2"
79: * in exactly the same way as strcmp does. Except the
80: * comparison runs for at most "n" characters.
81: */
82:
83: int
84: strncmp(
85: register const char *s1,
86: register const char *s2,
1.1.1.2 ! root 87: size_t n)
1.1 root 88: {
89: register unsigned int a, b;
90:
91: while (n != 0) {
92: a = *s1++;
93: b = *s2++;
94: if (a != b)
95: return a-b; /* includes case when
96: 'a' is zero and 'b' is not zero
97: or vice versa */
98: if (a == '\0')
99: return 0; /* both are zero */
100: n--;
101: }
102:
103: return 0;
104: }
105:
106:
107: /*
108: * Abstract:
109: * strcpy copies the contents of the string "from" including
110: * the null terminator to the string "to". A pointer to "to"
111: * is returned.
112: */
113:
114: char *
115: strcpy(
116: register char *to,
117: register const char *from)
118: {
119: register char *ret = to;
120:
121: while ((*to++ = *from++) != '\0')
122: continue;
123:
124: return ret;
125: }
126:
127: /*
128: * Abstract:
129: * strncpy copies "count" characters from the "from" string to
130: * the "to" string. If "from" contains less than "count" characters
131: * "to" will be padded with null characters until exactly "count"
132: * characters have been written. The return value is a pointer
133: * to the "to" string.
134: */
135:
136: char *
137: strncpy(
138: register char *to,
139: register const char *from,
1.1.1.2 ! root 140: register size_t count)
1.1 root 141: {
142: register char *ret = to;
143:
144: while (count != 0) {
145: count--;
146: if ((*to++ = *from++) == '\0')
147: break;
148: }
149:
150: while (count != 0) {
151: *to++ = '\0';
152: count--;
153: }
154:
155: return ret;
156: }
157:
158: /*
159: * Abstract:
160: * strlen returns the number of characters in "string" preceeding
161: * the terminating null character.
162: */
163:
1.1.1.2 ! root 164: size_t
1.1 root 165: strlen(
166: register const char *string)
167: {
168: register const char *ret = string;
169:
170: while (*string++ != '\0')
171: continue;
172:
173: return string - 1 - ret;
174: }
1.1.1.2 ! root 175:
! 176: /*
! 177: * Abstract:
! 178: * memset writes value "c" in the "n" bytes starting at address "s".
! 179: * The return value is a pointer to the "s" string.
! 180: */
! 181:
! 182: void *
! 183: memset(
! 184: void *_s, int c, size_t n)
! 185: {
! 186: char *s = _s;
! 187: int i;
! 188:
! 189: for (i = 0; i < n ; i++)
! 190: s[i] = c;
! 191:
! 192: return _s;
! 193: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.