1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* Mach Operating System
* Copyright (c) 1993 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* File: strings.c
* Author: Robert V. Baron, Carnegie Mellon University
* Date: ??/92
*
* String functions.
*/
#include <string.h>
#ifdef strcpy
#undef strcmp
#undef strncmp
#undef strcpy
#undef strncpy
#undef strlen
#endif
/*
* Abstract:
* strcmp (s1, s2) compares the strings "s1" and "s2".
* It returns 0 if the strings are identical. It returns
* > 0 if the first character that differs in the two strings
* is larger in s1 than in s2 or if s1 is longer than s2 and
* the contents are identical up to the length of s2.
* It returns < 0 if the first differing character is smaller
* in s1 than in s2 or if s1 is shorter than s2 and the
* contents are identical up to the length of s1.
*/
int __attribute__ ((pure))
strcmp(
const char *s1,
const char *s2)
{
unsigned int a, b;
do {
a = *s1++;
b = *s2++;
if (a != b)
return a-b; /* includes case when
'a' is zero and 'b' is not zero
or vice versa */
} while (a != '\0');
return 0; /* both are zero */
}
/*
* Abstract:
* strncmp (s1, s2, n) compares the strings "s1" and "s2"
* in exactly the same way as strcmp does. Except the
* comparison runs for at most "n" characters.
*/
int __attribute__ ((pure))
strncmp(
const char *s1,
const char *s2,
size_t n)
{
unsigned int a, b;
while (n != 0) {
a = *s1++;
b = *s2++;
if (a != b)
return a-b; /* includes case when
'a' is zero and 'b' is not zero
or vice versa */
if (a == '\0')
return 0; /* both are zero */
n--;
}
return 0;
}
/*
* Abstract:
* strcpy copies the contents of the string "from" including
* the null terminator to the string "to". A pointer to "to"
* is returned.
*/
char *
strcpy(
char *to,
const char *from)
{
char *ret = to;
while ((*to++ = *from++) != '\0')
continue;
return ret;
}
/*
* Abstract:
* strncpy copies "count" characters from the "from" string to
* the "to" string. If "from" contains less than "count" characters
* "to" will be padded with null characters until exactly "count"
* characters have been written. The return value is a pointer
* to the "to" string.
*/
char *
strncpy(
char *to,
const char *from,
size_t count)
{
char *ret = to;
while (count != 0) {
count--;
if ((*to++ = *from++) == '\0')
break;
}
while (count != 0) {
*to++ = '\0';
count--;
}
return ret;
}
/*
* Abstract:
* strlen returns the number of characters in "string" preceding
* the terminating null character.
*/
size_t __attribute__ ((pure))
strlen(
const char *string)
{
const char *ret = string;
while (*string++ != '\0')
continue;
return string - 1 - ret;
}
/*
* Abstract:
* memset writes value "c" in the "n" bytes starting at address "s".
* The return value is a pointer to the "s" string.
*/
#if 0
void *
memset(
void *_s, int c, size_t n)
{
char *s = _s;
size_t i;
for (i = 0; i < n ; i++)
s[i] = c;
return _s;
}
#endif
/*
* Abstract:
* strchr returns a pointer to the first occurrence of the character
* "c" in the string "s". If "c" is not found, return NULL.
*/
char *
strchr(
const char *s,
int c)
{
while (*s != c) {
if (*s == '\0') {
return NULL;
}
s++;
}
return (char *)s;
}
/*
* Abstract:
* strsep extracts tokens from strings. If "*sp" is NULL, return NULL
* and do nothing. Otherwise, find the first token in string "*sp".
* Tokens are delimited by characters in the string "delim". If no
* delimiter is found, the token is the entire string "*sp", and "*sp"
* is made NULL. Otherwise, overwrite the delimiter with a null byte,
* and make "*sp" point past it.
*/
char *
strsep(
char **sp,
const char *delim)
{
const char *d;
char *s, *t;
s = t = *sp;
if (s == NULL) {
return NULL;
}
for (;;) {
if (*s == '\0') {
*sp = NULL;
return t;
}
d = delim;
for (;;) {
if (*d == '\0') {
break;
}
if (*d == *s) {
*s = '\0';
*sp = s + 1;
return t;
}
d++;
}
s++;
}
}
/*
* Abstract:
* strstr returns a pointer to the first occurrence of the substring
* "find" in the string "s". If no substring was found, return NULL.
*/
char *
strstr(
const char *s,
const char *find)
{
size_t len;
len = strlen(find);
if (len == 0) {
return (char *)s;
}
for (;;) {
if (*s == '\0') {
return NULL;
}
if (strncmp(s, find, len) == 0) {
return (char *)s;
}
s++;
}
}
|