changeset 126:de391a7d85b1

Created strtok_r, now using it
author Jonathan Pevarnek <pevarnj@gmail.com>
date Fri, 24 Jun 2011 10:50:09 -0400
parents e2396f625db8
children 2d47a53a5736
files include/string.h src/os/memStack.c src/os/shell.c src/os/string.c src/prog/echo.c
diffstat 5 files changed, 21 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/include/string.h	Thu Jun 23 11:50:50 2011 -0400
+++ b/include/string.h	Fri Jun 24 10:50:09 2011 -0400
@@ -8,5 +8,6 @@
 void* memset(void *dest, u8 value, size_t num);
 size_t strlen(const char *str);
 char *strtok(char *str, const char *delimiters);
+char *strtok_r(char *str, const char *delimiters, char **loc);
 
 #endif
--- a/src/os/memStack.c	Thu Jun 23 11:50:50 2011 -0400
+++ b/src/os/memStack.c	Fri Jun 24 10:50:09 2011 -0400
@@ -34,11 +34,6 @@
 	return new;
 }
 
-static void doNothing(void *foo)
-{
-	return;
-}
-
 void msDestroy(MemStack *stack, void (*fn)(void*))
 {
 	Pointer point;
--- a/src/os/shell.c	Thu Jun 23 11:50:50 2011 -0400
+++ b/src/os/shell.c	Fri Jun 24 10:50:09 2011 -0400
@@ -16,7 +16,7 @@
 void start(u64 __memsize)
 {
 	char buffer[128];
-	char *b, *c;
+	char *b, *c, *track;
 	ErrCode err;
 	init_all(__memsize);
 	if(isError(init_fs(0x100, __memsize))) die();
@@ -27,7 +27,7 @@
 		sPrint("$ ");
 		sGet(buffer, 78);
 		b = buffer;
-		c = strtok(b, " ");
+		c = strtok_r(b, " ", &track);
 
 		u32 fid;
 		if(isError(err = lookupFile(c, &fid))) {
--- a/src/os/string.c	Thu Jun 23 11:50:50 2011 -0400
+++ b/src/os/string.c	Fri Jun 24 10:50:09 2011 -0400
@@ -1,3 +1,5 @@
+#include <string.h>
+
 //returns true if c is a character in the c-string characters, false otherwise
 static char isChar(const char c, const char *characters);
 
@@ -55,14 +57,20 @@
 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 strtok_r(str, delimiters, &last);
+}
+
+char *strtok_r(char *str, const char *delimiters, char **loc)
+{
+	char *track = *loc, *ret;
+	if(str) track = str;
+	if(!track) return track; //return null if done
+	while(isChar(*track, delimiters)) track++;
+	ret = track; //first non-deliminator character, start of string
+	while(*track && !isChar(*track, delimiters)) track++;
+	if(!*track) track = NULL;
+	else *track++ = '\0';
+	*loc = track;
 	return ret;
 }
 
--- a/src/prog/echo.c	Thu Jun 23 11:50:50 2011 -0400
+++ b/src/prog/echo.c	Fri Jun 24 10:50:09 2011 -0400
@@ -5,10 +5,10 @@
 void start() //TODO: accept a command line argument of what to print
 {
 	char buffer[128];
-	char *b, *c;
+	char *b, *c, *track;
 	sGet(buffer, 128);
 	b = buffer;
-	while((c = strtok(b, " ,."))) {
+	while((c = strtok_r(b, " ,.", &track))) {
 		if(b) b = NULL;
 		sPrint(c);
 		sPrint("\n");