Add strtok_r().
[pintos-anon] / src / lib / lib.c
index e443be0ab117facbfb89922dd9828b41c55612d9..25b4cc1ea039970a5a7e9b3c02fbdb8c8d514adb 100644 (file)
@@ -155,6 +155,49 @@ strcmp (const char *a_, const char *b_)
 
   return *a < *b ? -1 : *a > *b;
 }
+
+char *
+strtok_r (char *s, const char *delimiters, char **save_ptr) 
+{
+  char *token;
+  
+  ASSERT (delimiters != NULL);
+  ASSERT (save_ptr != NULL);
+
+  /* If S is nonnull, start from it.
+     If S is null, start from saved position. */
+  if (s == NULL)
+    s = *save_ptr;
+  ASSERT (s != NULL);
+
+  /* Skip any DELIMITERS at our current position. */
+  while (strchr (delimiters, *s) != NULL) 
+    {
+      /* strchr() will always return nonnull if we're searching
+         for a null byte, because every string contains a null
+         byte (at the end). */
+      if (*s == '\0')
+        {
+          *save_ptr = s;
+          return NULL;
+        }
+
+      s++;
+    }
+
+  /* Skip any non-DELIMITERS up to the end of the string. */
+  token = s;
+  while (strchr (delimiters, *s) == NULL)
+    s++;
+  if (*s != '\0') 
+    {
+      *s = '\0';
+      *save_ptr = s + 1;
+    }
+  else 
+    *save_ptr = s;
+  return token;
+}
 \f
 static void
 vprintf_core (const char *format, va_list args,