diff options
author | dzwdz | 2022-08-07 00:04:54 +0200 |
---|---|---|
committer | dzwdz | 2022-08-07 00:04:54 +0200 |
commit | 93ce4f4248999adeb85c6859dc2b5c54d7a8d7b7 (patch) | |
tree | d2ed20a632cdaf782facce76c870413a47c3f60e | |
parent | 809f9a9899b219e33ec839b3f9bee644fcfecacc (diff) |
user/libc: strchr
-rw-r--r-- | src/user/bootstrap/tar.c | 9 | ||||
-rw-r--r-- | src/user/lib/include/string.h | 2 | ||||
-rw-r--r-- | src/user/lib/stdlib.c | 8 | ||||
-rw-r--r-- | 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 <camellia/syscalls.h> #include <shared/mem.h> #include <stdint.h> +#include <string.h> #include <unistd.h> #include <user/lib/fs/dir.h> @@ -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; +} |