9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 403bea0766b1cddc12c698f95a81475f72b56f65
parent 40bedc7b2fc8ca42cbf58b3b6decbc2ed19d877c
Author: Roberto Vargas <roberto.vargas@arm.com>
Date:   Thu,  4 Apr 2019 16:20:04 +0100

[libk] Make tokenize independent of NUL strings

Change-Id: Id6ec78ca38fcf4a866aae2afbfb2f60ce940fe65

Diffstat:
Minclude/libk.h | 2+-
Msrc/libk/tokenize.c | 15+++++++++------
2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/include/libk.h b/include/libk.h @@ -15,4 +15,4 @@ extern int kvprint(const char *fmt, va_list va); extern int kwrite(char *buf, int n); extern void kerror(const char *s); extern int putenv(char *name); -extern int tokenize(char *line, char *tokens[], int ntoks); +extern int tokenize(char *line, int siz, char *tokens[], int ntoks); diff --git a/src/libk/tokenize.c b/src/libk/tokenize.c @@ -3,20 +3,23 @@ #include <libk.h> int -tokenize(char *line, char *tokens[], int ntoks) +tokenize(char *line, int siz, char *tokens[], int ntoks) { int n; for (n = 0; n < ntoks; n++) { - while (isspace(*line)) - *line++ = '\0'; + while (siz > 0 && isspace(*line)) + siz--, *line++ = '\0'; - if (*line == '\0') + if (siz == 0) return n; tokens[n] = line; - while (*line && !isspace(*line)) - line++; + while (siz > 0 && !isspace(*line)) + siz--, line++; + if (siz == 0) + return n; + *line++ = '\0'; }