diff options
author | Richard Braun <rbraun@sceen.net> | 2016-08-16 18:35:51 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2016-08-16 18:41:34 +0200 |
commit | 05045bd8a898da23ebdad07c4a34e21c433b1b88 (patch) | |
tree | 9808571c05e1b3d99a037a9ed39e671cd02ea473 /kern | |
parent | e26f13d83a560684e1aa6b11b1a43f8764eaa032 (diff) | |
download | gnumach-05045bd8a898da23ebdad07c4a34e21c433b1b88.tar.gz gnumach-05045bd8a898da23ebdad07c4a34e21c433b1b88.tar.bz2 gnumach-05045bd8a898da23ebdad07c4a34e21c433b1b88.zip |
Replace libc string functions with internal implementations
* Makefile.am (clib_routines): Remove memcmp, memcpy, memmove,
strchr, strstr and strsep.
* kern/strings.c (memset): Comment out.
(strchr, strsep, strstr): New functions.
Diffstat (limited to 'kern')
-rw-r--r-- | kern/strings.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/kern/strings.c b/kern/strings.c index 64410d91..71c99050 100644 --- a/kern/strings.c +++ b/kern/strings.c @@ -179,6 +179,7 @@ strlen( * The return value is a pointer to the "s" string. */ +#if 0 void * memset( void *_s, int c, size_t n) @@ -191,3 +192,105 @@ memset( 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++; + } +} |