9os

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

commit 7358f3ab5ae9eb51290ac8e4d96a8ef4eafb660e
parent 0d00f681251d107dab8cf41154286d7fb807db36
Author: Roberto Vargas <roberto.vargas@arm.com>
Date:   Thu,  4 Apr 2019 09:52:44 +0100

[libk] Add tokenize()

This function is intended to make easier parsing ctl
messages.

Change-Id: I7b9b62dc39543b0badeadf6a86937f85d08ef75d

Diffstat:
Minclude/libk.h | 1+
Msrc/libk/Makefile | 1+
Asrc/libk/tokenize.c | 24++++++++++++++++++++++++
3 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/include/libk.h b/include/libk.h @@ -15,3 +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); diff --git a/src/libk/Makefile b/src/libk/Makefile @@ -12,6 +12,7 @@ OBJS = doprnt.o \ kgets.o \ kerror.o \ kwrite.o \ + tokenize.o \ putenv-$(MODE).o \ __assert.o \ diff --git a/src/libk/tokenize.c b/src/libk/tokenize.c @@ -0,0 +1,24 @@ +#include <ctype.h> + +#include <libk.h> + +int +tokenize(char *line, char *tokens[], int ntoks) +{ + int n; + + for (n = 0; n < ntoks; n++) { + while (isspace(*line)) + *line++ = '\0'; + + if (*line == '\0') + return n; + tokens[n] = line; + + while (*line && !isspace(*line)) + line++; + *line++ = '\0'; + } + + return n; +}