xref: /csrg-svn/lib/libc/string/strsep.3 (revision 62921)
1*62921Sbostic.\" Copyright (c) 1990, 1991, 1993
2*62921Sbostic.\"	The Regents of the University of California.  All rights reserved.
341981Sbostic.\"
441981Sbostic.\" This code is derived from software contributed to Berkeley by
541981Sbostic.\" Chris Torek.
641981Sbostic.\"
750757Scael.\" %sccs.include.redist.roff%
841981Sbostic.\"
9*62921Sbostic.\"	@(#)strsep.3	8.1 (Berkeley) 06/09/93
1050757Scael.\"
1148351Scael.Dd
1248351Scael.Dt STRSEP 3
1348351Scael.Os
1448351Scael.Sh NAME
1548351Scael.Nm strsep
1648351Scael.Nd separate strings
1748351Scael.Sh SYNOPSIS
1848351Scael.Fd #include <string.h>
1948351Scael.Ft char *
2048351Scael.Fn strsep "char **stringp" "char *delim"
2148351Scael.Sh DESCRIPTION
2248351ScaelThe
2348351Scael.Fn strsep
2451726Sbosticfunction locates, in the string referenced by
2551726Sbostic.Fa *stringp ,
2651726Sbosticthe first occurrence of any character in the string
2748351Scael.Fa delim
2851726Sbostic(or the terminating
2951726Sbostic.Ql \e0
3051726Sbosticcharacter) and replaces it with a
3151726Sbostic.Ql \e0 .
3251726SbosticThe location of the next character after the delimiter character
3351726Sbostic(or NULL, if the end of the string was reached) is stored in
3448351Scael.Fa *stringp .
3551726SbosticThe original value of
3648351Scael.Fa *stringp
3751726Sbosticis returned.
3851726Sbostic.Pp
3951726SbosticAn ``empty'' field, i.e. one caused by two adjacent delimiter characters,
4051726Sbosticcan be detected by comparing the location referenced by the pointer returned
4151726Sbosticin
4251726Sbostic.Fa *stringp
4348351Scaelto
4451726Sbostic.Ql \e0 .
4551726Sbostic.Pp
4651726SbosticIf
4748351Scael.Fa *stringp
4848351Scaelis initially
4948351Scael.Dv NULL ,
5048351Scael.Fn strsep
5148351Scaelreturns
5248351Scael.Dv NULL .
5348351Scael.Sh EXAMPLES
5441981SbosticThe following uses
5548351Scael.Fn strsep
5651726Sbosticto parse a string, containing tokens delimited by white space, into an
5751726Sbosticargument vector:
5848351Scael.Bd -literal -offset indent
5951726Sbosticchar **ap, *argv[10], *inputstring;
6051726Sbostic
6151726Sbosticfor (ap = argv; (*ap = strsep(&inputstring, " \et")) != NULL;)
6251726Sbostic	if (**ap != '\e0')
6351726Sbostic		++ap;
6448351Scael.Ed
6548351Scael.Sh HISTORY
6648351ScaelThe
6748351Scael.Fn strsep
6851726Sbosticfunction
6951726Sbosticis intended as a replacement for the
7051726Sbostic.Fn strtok
7151726Sbosticfunction.
7251726SbosticWhile the
7351726Sbostic.Fn strtok
7451726Sbosticfunction should be preferred for portability reasons (it conforms to
7551726Sbostic.St -ansiC )
7651726Sbosticit is unable to handle empty fields, i.e. detect fields delimited by
7751726Sbostictwo adjacent delimiter characters, or to be used for more than a single
7851726Sbosticstring at a time.
7951726SbosticThe
8051726Sbostic.Fn strsep
8162920Sbosticfunction first appeared in 4.4BSD.
82