chiark / gitweb /
rm some todos
[inn-innduct.git] / lib / strtok.c
1 /*  $Id: strtok.c 6118 2003-01-13 06:44:24Z rra $
2 **
3 **  This file has been modified to get it to compile more easily
4 **  on pre-4.4BSD systems.  Rich $alz, June 1991.
5 */
6
7 #include "config.h"
8 #include "clibrary.h"
9
10
11 /*
12  * Copyright (c) 1988 Regents of the University of California.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms are permitted
16  * provided that: (1) source distributions retain this entire copyright
17  * notice and comment, and (2) distributions including binaries display
18  * the following acknowledgement:  ``This product includes software
19  * developed by the University of California, Berkeley and its contributors''
20  * in the documentation or other materials provided with the distribution
21  * and in all advertising materials mentioning features or use of this
22  * software. Neither the name of the University nor the names of its
23  * contributors may be used to endorse or promote products derived
24  * from this software without specific prior written permission.
25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28  */
29
30 #if 0
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)strtok.c    5.7 (Berkeley) 6/1/90";
33 #endif /* LIBC_SCCS and not lint */
34
35 #include <stddef.h>
36 #include <string.h>
37 #endif
38
39 char *
40 strtok(s, delim)
41         char *s, *delim;
42 {
43         char *spanp;
44         int c, sc;
45         char *tok;
46         static char *last;
47
48
49         if (s == NULL && (s = last) == NULL)
50                 return (NULL);
51
52         /*
53          * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
54          */
55 cont:
56         c = *s++;
57         for (spanp = delim; (sc = *spanp++) != 0;) {
58                 if (c == sc)
59                         goto cont;
60         }
61
62         if (c == 0) {           /* no non-delimiter characters */
63                 last = NULL;
64                 return (NULL);
65         }
66         tok = s - 1;
67
68         /*
69          * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
70          * Note that delim must have one NUL; we stop if we see that, too.
71          */
72         for (;;) {
73                 c = *s++;
74                 spanp = delim;
75                 do {
76                         if ((sc = *spanp++) == c) {
77                                 if (c == 0)
78                                         s = NULL;
79                                 else
80                                         s[-1] = 0;
81                                 last = s;
82                                 return (tok);
83                         }
84                 } while (sc != 0);
85         }
86         /* NOTREACHED */
87 }