xref: /minix3/lib/libc/string/strsep.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993
2*2fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
3*2fe8fb19SBen Gras.\"
4*2fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by
5*2fe8fb19SBen Gras.\" Chris Torek.
6*2fe8fb19SBen Gras.\"
7*2fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
8*2fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
9*2fe8fb19SBen Gras.\" are met:
10*2fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
11*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
12*2fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
13*2fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
14*2fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
15*2fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
16*2fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
17*2fe8fb19SBen Gras.\"    without specific prior written permission.
18*2fe8fb19SBen Gras.\"
19*2fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*2fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*2fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*2fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*2fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*2fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*2fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*2fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*2fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*2fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*2fe8fb19SBen Gras.\" SUCH DAMAGE.
30*2fe8fb19SBen Gras.\"
31*2fe8fb19SBen Gras.\"     from: @(#)strsep.3	8.1 (Berkeley) 6/9/93
32*2fe8fb19SBen Gras.\"	$NetBSD: strsep.3,v 1.19 2006/10/16 08:48:45 wiz Exp $
33*2fe8fb19SBen Gras.\"
34*2fe8fb19SBen Gras.Dd August 12, 2006
35*2fe8fb19SBen Gras.Dt STRSEP 3
36*2fe8fb19SBen Gras.Os
37*2fe8fb19SBen Gras.Sh NAME
38*2fe8fb19SBen Gras.Nm strsep ,
39*2fe8fb19SBen Gras.Nm stresep
40*2fe8fb19SBen Gras.Nd separate strings
41*2fe8fb19SBen Gras.Sh LIBRARY
42*2fe8fb19SBen Gras.Lb libc
43*2fe8fb19SBen Gras.Sh SYNOPSIS
44*2fe8fb19SBen Gras.In string.h
45*2fe8fb19SBen Gras.Ft char *
46*2fe8fb19SBen Gras.Fn strsep "char **stringp" "const char *delim"
47*2fe8fb19SBen Gras.Ft char *
48*2fe8fb19SBen Gras.Fn stresep "char **stringp" "const char *delim" "int escape"
49*2fe8fb19SBen Gras.Sh DESCRIPTION
50*2fe8fb19SBen GrasThe
51*2fe8fb19SBen Gras.Fn strsep
52*2fe8fb19SBen Grasfunction locates, in the nul-terminated string referenced by
53*2fe8fb19SBen Gras.Fa *stringp ,
54*2fe8fb19SBen Grasthe first occurrence of any character in the string
55*2fe8fb19SBen Gras.Fa delim
56*2fe8fb19SBen Gras(or the terminating
57*2fe8fb19SBen Gras.Ql \e0
58*2fe8fb19SBen Grascharacter) and replaces it with a
59*2fe8fb19SBen Gras.Ql \e0 .
60*2fe8fb19SBen GrasThe location of the next character after the delimiter character
61*2fe8fb19SBen Gras(or
62*2fe8fb19SBen Gras.Dv NULL ,
63*2fe8fb19SBen Grasif the end of the string was reached) is stored in
64*2fe8fb19SBen Gras.Fa *stringp .
65*2fe8fb19SBen GrasThe original value of
66*2fe8fb19SBen Gras.Fa *stringp
67*2fe8fb19SBen Grasis returned.
68*2fe8fb19SBen Gras.Pp
69*2fe8fb19SBen GrasAn
70*2fe8fb19SBen Gras.Dq empty
71*2fe8fb19SBen Grasfield, i.e., one caused by two adjacent delimiter characters,
72*2fe8fb19SBen Grascan be detected by comparing the location referenced by the pointer returned
73*2fe8fb19SBen Grasby
74*2fe8fb19SBen Gras.Fn strsep
75*2fe8fb19SBen Grasto
76*2fe8fb19SBen Gras.Ql \e0 .
77*2fe8fb19SBen Gras.Pp
78*2fe8fb19SBen GrasIf
79*2fe8fb19SBen Gras.Fa *stringp
80*2fe8fb19SBen Grasis initially
81*2fe8fb19SBen Gras.Dv NULL ,
82*2fe8fb19SBen Gras.Fn strsep
83*2fe8fb19SBen Grasreturns
84*2fe8fb19SBen Gras.Dv NULL .
85*2fe8fb19SBen GrasThe
86*2fe8fb19SBen Gras.Fn stresep
87*2fe8fb19SBen Grasfunction also takes an escape character that allows quoting the delimiter
88*2fe8fb19SBen Grascharacter so that it can be part of the source string.
89*2fe8fb19SBen Gras.Sh EXAMPLES
90*2fe8fb19SBen GrasThe following uses
91*2fe8fb19SBen Gras.Fn strsep
92*2fe8fb19SBen Grasto parse a string, containing tokens delimited by white space, into an
93*2fe8fb19SBen Grasargument vector:
94*2fe8fb19SBen Gras.Bd -literal -offset indent
95*2fe8fb19SBen Graschar **ap, *argv[10], *inputstring;
96*2fe8fb19SBen Gras
97*2fe8fb19SBen Grasfor (ap = argv; ap \*[Lt] \*[Am]argv[9] \*[Am]\*[Am]
98*2fe8fb19SBen Gras    (*ap = strsep(\*[Am]inputstring, " \et")) != NULL;) {
99*2fe8fb19SBen Gras	if (**ap != '\e0')
100*2fe8fb19SBen Gras		ap++;
101*2fe8fb19SBen Gras}
102*2fe8fb19SBen Gras.Ed
103*2fe8fb19SBen Gras.Sh HISTORY
104*2fe8fb19SBen GrasThe
105*2fe8fb19SBen Gras.Fn strsep
106*2fe8fb19SBen Grasfunction
107*2fe8fb19SBen Grasis intended as a replacement for the
108*2fe8fb19SBen Gras.Fn strtok
109*2fe8fb19SBen Grasfunction.
110*2fe8fb19SBen GrasWhile the
111*2fe8fb19SBen Gras.Fn strtok
112*2fe8fb19SBen Grasfunction should be preferred for portability reasons (it conforms to
113*2fe8fb19SBen Gras.St -ansiC )
114*2fe8fb19SBen Grasit is unable to handle empty fields, i.e., detect fields delimited by
115*2fe8fb19SBen Grastwo adjacent delimiter characters, or to be used for more than a single
116*2fe8fb19SBen Grasstring at a time.
117*2fe8fb19SBen GrasThe
118*2fe8fb19SBen Gras.Fn strsep
119*2fe8fb19SBen Grasfunction first appeared in
120*2fe8fb19SBen Gras.Bx 4.4 .
121