summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strsep.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/strsep.3')
-rw-r--r--src/lib/libc/string/strsep.384
1 files changed, 54 insertions, 30 deletions
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3
index 21aa7376f2..848dab1eb3 100644
--- a/src/lib/libc/string/strsep.3
+++ b/src/lib/libc/string/strsep.3
@@ -1,8 +1,11 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strsep.3,v 1.10 2001/11/21 14:32:45 deraadt Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" This code is derived from software contributed to Berkeley by 6.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek. 7.\" Chris Torek.
8.\"
6.\" Redistribution and use in source and binary forms, with or without 9.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions 10.\" modification, are permitted provided that the following conditions
8.\" are met: 11.\" are met:
@@ -31,10 +34,9 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 35.\" SUCH DAMAGE.
33.\" 36.\"
34.\" from: @(#)strsep.3 5.3 (Berkeley) 4/19/91 37.\" @(#)strsep.3 8.1 (Berkeley) 6/9/93
35.\" $Id: strsep.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
36.\" 38.\"
37.Dd April 19, 1991 39.Dd June 9, 1993
38.Dt STRSEP 3 40.Dt STRSEP 3
39.Os 41.Os
40.Sh NAME 42.Sh NAME
@@ -43,27 +45,37 @@
43.Sh SYNOPSIS 45.Sh SYNOPSIS
44.Fd #include <string.h> 46.Fd #include <string.h>
45.Ft char * 47.Ft char *
46.Fn strsep "char **stringp" "char *delim" 48.Fn strsep "char **stringp" "const char *delim"
47.Sh DESCRIPTION 49.Sh DESCRIPTION
48The 50The
49.Fn strsep 51.Fn strsep
50locates in the null-terminated string at 52function locates, in the string referenced by
51.Fa *stringp
52the first occurrence of any character in
53.Fa delim
54and replaces this with a
55.Ql \e0 ,
56records the location of the immediate following character in
57.Fa *stringp , 53.Fa *stringp ,
58then returns the original value of 54the first occurrence of any character in the string
55.Fa delim
56(or the terminating
57.Ql \e0
58character) and replaces it with a
59.Ql \e0 .
60The location of the next character after the delimiter character
61(or
62.Dv NULL ,
63if the end of the string was reached) is stored in
59.Fa *stringp . 64.Fa *stringp .
60If no delimiter characters are found, 65The original value of
61.Fn strsep
62sets
63.Fa *stringp 66.Fa *stringp
67is returned.
68.Pp
69An
70.Dq empty
71field, i.e., one caused by two adjacent delimiter characters,
72can be detected by comparing the location referenced by the pointer returned
73by
74.Fn strsep
64to 75to
65.Dv NULL ; 76.Ql \e0 .
66if 77.Pp
78If
67.Fa *stringp 79.Fa *stringp
68is initially 80is initially
69.Dv NULL , 81.Dv NULL ,
@@ -73,20 +85,32 @@ returns
73.Sh EXAMPLES 85.Sh EXAMPLES
74The following uses 86The following uses
75.Fn strsep 87.Fn strsep
76to parse strings containing runs of white space, 88to parse a string, containing tokens delimited by whitespace, into an
77making up an argument vector: 89argument vector:
78.Bd -literal -offset indent 90.Bd -literal -offset indent
79char inputstring[100]; 91char **ap, *argv[10], *inputstring;
80char **argv[51], **ap = argv, *p, *val; 92
81/* set up inputstring */ 93for (ap = argv; ap < &argv[9] &&
82for (p = inputstring; p != NULL; ) { 94 (*ap = strsep(&inputstring, " \et")) != NULL;) {
83 while ((val = strsep(&p, " \et")) != NULL && *val == '\e0'); 95 if (**ap != '\e0')
84 *ap++ = val; 96 ap++;
85} 97}
86*ap = 0; 98*ap = NULL;
87.Ed 99.Ed
88.Sh HISTORY 100.Sh HISTORY
89The 101The
90.Fn strsep 102.Fn strsep
91function is 103function is intended as a replacement for the
92.Ud . 104.Fn strtok
105function.
106While the
107.Fn strtok
108function should be preferred for portability reasons (it conforms to
109.St -ansiC )
110it is unable to handle empty fields, i.e., detect fields delimited by
111two adjacent delimiter characters, or to be used for more than a single
112string at a time.
113The
114.Fn strsep
115function first appeared in
116.Bx 4.4 .