From 93ce4f4248999adeb85c6859dc2b5c54d7a8d7b7 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 7 Aug 2022 00:04:54 +0200 Subject: user/libc: strchr --- src/user/bootstrap/tar.c | 9 +++------ src/user/lib/include/string.h | 2 ++ src/user/lib/stdlib.c | 8 +++----- src/user/lib/string.c | 8 ++++++++ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/user/bootstrap/tar.c b/src/user/bootstrap/tar.c index 0356987..6bd4179 100644 --- a/src/user/bootstrap/tar.c +++ b/src/user/bootstrap/tar.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -94,12 +95,8 @@ static void tar_read(struct fs_wait_response *res, void *base, size_t base_len) char *suffix = base + off + meta_len; /* check if the path contains any non-trailing slashes */ - char *next = suffix; - // TODO strchr - while (*next && *next != '/') next++; - if (*next == '/') next++; - - if (*next == '\0') { + char *slash = strchr(suffix, '/'); + if (!slash || slash[1] == '\0') { if (dir_append(&db, suffix)) break; } } diff --git a/src/user/lib/include/string.h b/src/user/lib/include/string.h index 8930bee..9e08ae5 100644 --- a/src/user/lib/include/string.h +++ b/src/user/lib/include/string.h @@ -4,3 +4,5 @@ int isspace(char c); long strtol(const char *restrict s, char **restrict end, int base); + +char *strchr(const char *s, int c); diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c index 11ca7b6..857b40e 100644 --- a/src/user/lib/stdlib.c +++ b/src/user/lib/stdlib.c @@ -36,11 +36,9 @@ int execv(const char *path, char *const argv[]) { fseek(file, 2, SEEK_SET); if (fgets(buf, sizeof buf, file)) { const char *argv [] = {buf, path, NULL}; - // TODO strchr - char *s = buf; - while (*s && *s != '\n') s++; - *s = '\0'; - execv(argv[0], (void*)argv); + char *endl = strchr(buf, '\n'); + if (endl) *endl = '\0'; + execv(buf, (void*)argv); } } diff --git a/src/user/lib/string.c b/src/user/lib/string.c index 77b996c..92b2e51 100644 --- a/src/user/lib/string.c +++ b/src/user/lib/string.c @@ -49,3 +49,11 @@ long strtol(const char *restrict s, char **restrict end, int base) { if (end) *end = (void*)s; return res * sign; } + +char *strchr(const char *s, int c) { + while (*s) { + if (*s == c) return s; + s++; + } + return NULL; +} -- cgit v1.2.3