xref: /csrg-svn/lib/libc/string/strtok.c (revision 34831)
124193Skre /*
234479Sbostic  * Copyright (c) 1985 Regents of the University of California.
334479Sbostic  * All rights reserved.
434479Sbostic  *
534479Sbostic  * Redistribution and use in source and binary forms are permitted
6*34831Sbostic  * provided that the above copyright notice and this paragraph are
7*34831Sbostic  * duplicated in all such forms and that any documentation,
8*34831Sbostic  * advertising materials, and other materials related to such
9*34831Sbostic  * distribution and use acknowledge that the software was developed
10*34831Sbostic  * by the University of California, Berkeley.  The name of the
11*34831Sbostic  * University may not be used to endorse or promote products derived
12*34831Sbostic  * from this software without specific prior written permission.
13*34831Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34831Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34831Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1624193Skre  */
1724193Skre 
1826538Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34831Sbostic static char sccsid[] = "@(#)strtok.c	5.4 (Berkeley) 06/27/88";
2034479Sbostic #endif /* LIBC_SCCS and not lint */
2124193Skre 
2224193Skre char *
2324193Skre strtok(s, sep)
2424193Skre 	register char *s, *sep;
2524193Skre {
2624193Skre 	register char *p;
2724193Skre 	register c;
2824193Skre 	static char *lasts;
2924193Skre 
3024193Skre 	if (s == 0)
3124193Skre 		s = lasts;
3224193Skre 	if (s == 0)
3324193Skre 		return (0);
3424193Skre 
3524193Skre 	while (c = *s) {
3624193Skre 		if (!index(sep, c))
3724193Skre 			break;
3824193Skre 		s++;
3924193Skre 	}
4024193Skre 
4124193Skre 	if (c == '\0') {
4224193Skre 		lasts = 0;
4324193Skre 		return (0);
4424193Skre 	}
4524193Skre 
4624193Skre 	for (p = s; c = *++p; )
4724193Skre 		if (index(sep, c))
4824193Skre 			break;
4924193Skre 
5024193Skre 	if (c == '\0')
5124193Skre 		lasts = 0;
5224193Skre 	else {
5324193Skre 		*p++ = '\0';
5424193Skre 		lasts = p;
5524193Skre 	}
5624193Skre 	return (s);
5724193Skre }
58