# HG changeset patch # User Jonathan Pevarnek # Date 1308840715 14400 # Node ID 56447a5e2d2f40924b7dc26a12823cf8c2314bca # Parent 2a35ea7e123ba50f550361419bf1b9abcd7ec9f7 Added strtok function, should function just as the C stdlib version does diff -r 2a35ea7e123b -r 56447a5e2d2f include/string.h --- a/include/string.h Thu Jun 23 10:05:55 2011 -0400 +++ b/include/string.h Thu Jun 23 10:51:55 2011 -0400 @@ -7,5 +7,6 @@ void* memcpy(void *dest, const void *src, size_t num); void* memset(void *dest, u8 value, size_t num); size_t strlen(const char *str); +char *strtok(char *str, const char *delimiters); #endif diff -r 2a35ea7e123b -r 56447a5e2d2f src/os/string.c --- a/src/os/string.c Thu Jun 23 10:05:55 2011 -0400 +++ b/src/os/string.c Thu Jun 23 10:51:55 2011 -0400 @@ -1,3 +1,6 @@ +//returns true if c is a character in the c-string characters, false otherwise +static char isChar(const char c, const char *characters); + int strcmp(const char *a, const char *b) { while(1) { @@ -48,3 +51,26 @@ while(*ptr && *++ptr); return ptr - str; } + +char *strtok(char *str, const char *delimiters) +{ + static char* last = NULL; + char* ret; + if(str) last = str; + if(!last) return last; //return null if done + while(isChar(*last, delimiters)) last++; + ret = last; //first non-deliminator character, start of string + while(*last && !isChar(*last, delimiters)) last++; + if(!*last) last = NULL; + else *last++ = '\0'; + return ret; +} + +static char isChar(const char c, const char *characters) +{ + while(*characters) { + if(*characters == c) return *characters; + characters++; + } + return '\0'; +} diff -r 2a35ea7e123b -r 56447a5e2d2f src/prog/echo.c --- a/src/prog/echo.c Thu Jun 23 10:05:55 2011 -0400 +++ b/src/prog/echo.c Thu Jun 23 10:51:55 2011 -0400 @@ -1,11 +1,17 @@ #include #include +#include void start() //TODO: accept a command line argument of what to print { char buffer[128]; + char *b, *c; sGet(buffer, 128); - sPrint(buffer); - sPrint("\n"); + b = buffer; + while((c = strtok(b, " ,."))) { + if(b) b = NULL; + sPrint(c); + sPrint("\n"); + } exit(); }