|
|
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
1.1.1.3 root 53: * contents are identical up to the length of s1.
1.1 root 54: */
55:
1.1.1.3 root 56: int __attribute__ ((pure))
1.1 root 57: strcmp(
1.1.1.3 root 58: const char *s1,
59: const char *s2)
1.1 root 60: {
1.1.1.3 root 61: unsigned int a, b;
1.1 root 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:
1.1.1.3 root 83: int __attribute__ ((pure))
1.1 root 84: strncmp(
1.1.1.3 root 85: const char *s1,
86: const char *s2,
1.1.1.2 root 87: size_t n)
1.1 root 88: {
1.1.1.3 root 89: unsigned int a, b;
1.1 root 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(
1.1.1.3 root 116: char *to,
117: const char *from)
1.1 root 118: {
1.1.1.3 root 119: char *ret = to;
1.1 root 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(
1.1.1.3 root 138: char *to,
139: const char *from,
140: size_t count)
1.1 root 141: {
1.1.1.3 root 142: char *ret = to;
1.1 root 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:
1.1.1.3 root 160: * strlen returns the number of characters in "string" preceding
1.1 root 161: * the terminating null character.
162: */
163:
1.1.1.3 root 164: size_t __attribute__ ((pure))
1.1 root 165: strlen(
1.1.1.3 root 166: const char *string)
1.1 root 167: {
1.1.1.3 root 168: const char *ret = string;
1.1 root 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:
1.1.1.5 ! root 182: #if 0
1.1.1.2 root 183: void *
184: memset(
185: void *_s, int c, size_t n)
186: {
187: char *s = _s;
1.1.1.4 root 188: size_t i;
1.1.1.2 root 189:
190: for (i = 0; i < n ; i++)
191: s[i] = c;
192:
193: return _s;
194: }
1.1.1.5 ! root 195: #endif
! 196:
! 197: /*
! 198: * Abstract:
! 199: * strchr returns a pointer to the first occurrence of the character
! 200: * "c" in the string "s". If "c" is not found, return NULL.
! 201: */
! 202: char *
! 203: strchr(
! 204: const char *s,
! 205: int c)
! 206: {
! 207: while (*s != c) {
! 208: if (*s == '\0') {
! 209: return NULL;
! 210: }
! 211:
! 212: s++;
! 213: }
! 214:
! 215: return (char *)s;
! 216: }
! 217:
! 218: /*
! 219: * Abstract:
! 220: * strsep extracts tokens from strings. If "*sp" is NULL, return NULL
! 221: * and do nothing. Otherwise, find the first token in string "*sp".
! 222: * Tokens are delimited by characters in the string "delim". If no
! 223: * delimiter is found, the token is the entire string "*sp", and "*sp"
! 224: * is made NULL. Otherwise, overwrite the delimiter with a null byte,
! 225: * and make "*sp" point past it.
! 226: */
! 227: char *
! 228: strsep(
! 229: char **sp,
! 230: const char *delim)
! 231: {
! 232: const char *d;
! 233: char *s, *t;
! 234:
! 235: s = t = *sp;
! 236:
! 237: if (s == NULL) {
! 238: return NULL;
! 239: }
! 240:
! 241: for (;;) {
! 242: if (*s == '\0') {
! 243: *sp = NULL;
! 244: return t;
! 245: }
! 246:
! 247: d = delim;
! 248:
! 249: for (;;) {
! 250: if (*d == '\0') {
! 251: break;
! 252: }
! 253:
! 254: if (*d == *s) {
! 255: *s = '\0';
! 256: *sp = s + 1;
! 257: return t;
! 258: }
! 259:
! 260: d++;
! 261: }
! 262:
! 263: s++;
! 264: }
! 265: }
! 266:
! 267: /*
! 268: * Abstract:
! 269: * strstr returns a pointer to the first occurrence of the substring
! 270: * "find" in the string "s". If no substring was found, return NULL.
! 271: */
! 272: char *
! 273: strstr(
! 274: const char *s,
! 275: const char *find)
! 276: {
! 277: size_t len;
! 278:
! 279: len = strlen(find);
! 280:
! 281: if (len == 0) {
! 282: return (char *)s;
! 283: }
! 284:
! 285: for (;;) {
! 286: if (*s == '\0') {
! 287: return NULL;
! 288: }
! 289:
! 290: if (strncmp(s, find, len) == 0) {
! 291: return (char *)s;
! 292: }
! 293:
! 294: s++;
! 295: }
! 296: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.