156a424ccSmp /*
256a424ccSmp * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
356a424ccSmp *
456a424ccSmp */
556a424ccSmp
656a424ccSmp /*
756a424ccSmp * Copyright (c) 1988 Regents of the University of California.
856a424ccSmp * All rights reserved.
956a424ccSmp *
1056a424ccSmp * Redistribution and use in source and binary forms are permitted
1156a424ccSmp * provided that: (1) source distributions retain this entire copyright
1256a424ccSmp * notice and comment, and (2) distributions including binaries display
1356a424ccSmp * the following acknowledgement: ``This product includes software
1456a424ccSmp * developed by the University of California, Berkeley and its contributors''
1556a424ccSmp * in the documentation or other materials provided with the distribution
1656a424ccSmp * and in all advertising materials mentioning features or use of this
1756a424ccSmp * software. Neither the name of the University nor the names of its
1856a424ccSmp * contributors may be used to endorse or promote products derived
1956a424ccSmp * from this software without specific prior written permission.
2056a424ccSmp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
2156a424ccSmp * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2256a424ccSmp * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2356a424ccSmp */
2456a424ccSmp
2556a424ccSmp #include <stddef.h>
2656a424ccSmp #include <string.h>
2756a424ccSmp #include "nstrtok.h"
2856a424ccSmp
2956a424ccSmp /*
3056a424ccSmp * Function: nstrtok
31*2a8bcb4eSToomas Soome *
3256a424ccSmp * Purpose: the same as strtok ... just different. does not deal with
3356a424ccSmp * multiple tokens in row.
3456a424ccSmp *
3556a424ccSmp * Arguments:
3656a424ccSmp * s (input) string to scan
3756a424ccSmp * delim (input) list of delimiters
3856a424ccSmp * <return value> string or null on error.
3956a424ccSmp *
4056a424ccSmp * Requires:
4156a424ccSmp * nuttin
42*2a8bcb4eSToomas Soome *
4356a424ccSmp * Effects:
4456a424ccSmp * sets last to string
4556a424ccSmp *
4656a424ccSmp * Modifies:
4756a424ccSmp * last
48*2a8bcb4eSToomas Soome *
4956a424ccSmp */
5056a424ccSmp
5156a424ccSmp char *
nstrtok(s,delim)5256a424ccSmp nstrtok(s, delim)
5356a424ccSmp register char *s;
5456a424ccSmp register const char *delim;
5556a424ccSmp {
5656a424ccSmp register const char *spanp;
5756a424ccSmp register int c, sc;
5856a424ccSmp char *tok;
5956a424ccSmp static char *last;
6056a424ccSmp
6156a424ccSmp
6256a424ccSmp if (s == NULL && (s = last) == NULL)
6356a424ccSmp return (NULL);
6456a424ccSmp
6556a424ccSmp /*
6656a424ccSmp * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
6756a424ccSmp */
68*2a8bcb4eSToomas Soome #ifdef OLD
6956a424ccSmp cont:
7056a424ccSmp c = *s++;
7156a424ccSmp for (spanp = delim; (sc = *spanp++) != 0;) {
7256a424ccSmp if (c == sc)
7356a424ccSmp goto cont;
7456a424ccSmp }
7556a424ccSmp
7656a424ccSmp if (c == 0) { /* no non-delimiter characters */
7756a424ccSmp last = NULL;
7856a424ccSmp return (NULL);
7956a424ccSmp }
8056a424ccSmp tok = s - 1;
8156a424ccSmp #else
8256a424ccSmp tok = s;
83*2a8bcb4eSToomas Soome #endif
8456a424ccSmp
8556a424ccSmp /*
8656a424ccSmp * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
8756a424ccSmp * Note that delim must have one NUL; we stop if we see that, too.
8856a424ccSmp */
8956a424ccSmp for (;;) {
9056a424ccSmp c = *s++;
9156a424ccSmp spanp = delim;
9256a424ccSmp do {
9356a424ccSmp if ((sc = *spanp++) == c) {
9456a424ccSmp if (c == 0)
9556a424ccSmp s = NULL;
9656a424ccSmp else
9756a424ccSmp s[-1] = 0;
9856a424ccSmp last = s;
9956a424ccSmp return (tok);
10056a424ccSmp }
10156a424ccSmp } while (sc != 0);
10256a424ccSmp }
10356a424ccSmp /* NOTREACHED */
10456a424ccSmp }
10556a424ccSmp
106