xref: /csrg-svn/lib/libc/string/strtok.c (revision 34479)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific written prior permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static char sccsid[] = "@(#)strtok.c	5.3 (Berkeley) 05/25/88";
15 #endif /* LIBC_SCCS and not lint */
16 
17 char *
18 strtok(s, sep)
19 	register char *s, *sep;
20 {
21 	register char *p;
22 	register c;
23 	static char *lasts;
24 
25 	if (s == 0)
26 		s = lasts;
27 	if (s == 0)
28 		return (0);
29 
30 	while (c = *s) {
31 		if (!index(sep, c))
32 			break;
33 		s++;
34 	}
35 
36 	if (c == '\0') {
37 		lasts = 0;
38 		return (0);
39 	}
40 
41 	for (p = s; c = *++p; )
42 		if (index(sep, c))
43 			break;
44 
45 	if (c == '\0')
46 		lasts = 0;
47 	else {
48 		*p++ = '\0';
49 		lasts = p;
50 	}
51 	return (s);
52 }
53