home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / strtok.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  64 lines

  1. /* from Henry Spencer's stringlib */
  2. #include <string.h>
  3.  
  4. /*
  5.  * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
  6.  * where tokens are nonempty strings separated by runs of
  7.  * chars from delim.  Writes NULs into s to end tokens.  delim need not
  8.  * remain constant from call to call.
  9.  */
  10.  
  11. static char *scanpoint = NULL;
  12.  
  13. char *                /* NULL if no token left */
  14. strtok(s, delim)
  15. char *s;
  16. register const char *delim;
  17. {
  18.     register char *scan;
  19.     char *tok;
  20.     register const char *dscan;
  21.  
  22.     if (s == NULL && scanpoint == NULL)
  23.         return(NULL);
  24.     if (s != NULL)
  25.         scan = s;
  26.     else
  27.         scan = scanpoint;
  28.  
  29.     /*
  30.      * Scan leading delimiters.
  31.      */
  32.     for (; *scan != '\0'; scan++) {
  33.         for (dscan = delim; *dscan != '\0'; dscan++)
  34.             if (*scan == *dscan)
  35.                 break;
  36.         if (*dscan == '\0')
  37.             break;
  38.     }
  39.     if (*scan == '\0') {
  40.         scanpoint = NULL;
  41.         return(NULL);
  42.     }
  43.  
  44.     tok = scan;
  45.  
  46.     /*
  47.      * Scan token.
  48.      */
  49.     for (; *scan != '\0'; scan++) {
  50.         for (dscan = delim; *dscan != '\0';)    /* ++ moved down. */
  51.             if (*scan == *dscan++) {
  52.                 scanpoint = scan+1;
  53.                 *scan = '\0';
  54.                 return(tok);
  55.             }
  56.     }
  57.  
  58.     /*
  59.      * Reached end of string.
  60.      */
  61.     scanpoint = NULL;
  62.     return(tok);
  63. }
  64.