|
@ -1,8 +1,11 @@ |
|
|
.\" Copyright (c) 1990, 1991 The Regents of the University of California. |
|
|
|
|
|
.\" All rights reserved. |
|
|
|
|
|
|
|
|
.\" $OpenBSD: strsep.3,v 1.3 1997/08/20 04:28:13 millert Exp $ |
|
|
|
|
|
.\" |
|
|
|
|
|
.\" Copyright (c) 1990, 1991, 1993 |
|
|
|
|
|
.\" The Regents of the University of California. All rights reserved. |
|
|
.\" |
|
|
.\" |
|
|
.\" This code is derived from software contributed to Berkeley by |
|
|
.\" This code is derived from software contributed to Berkeley by |
|
|
.\" Chris Torek. |
|
|
.\" Chris Torek. |
|
|
|
|
|
.\" |
|
|
.\" Redistribution and use in source and binary forms, with or without |
|
|
.\" Redistribution and use in source and binary forms, with or without |
|
|
.\" modification, are permitted provided that the following conditions |
|
|
.\" modification, are permitted provided that the following conditions |
|
|
.\" are met: |
|
|
.\" are met: |
|
@ -31,9 +34,9 @@ |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
.\" SUCH DAMAGE. |
|
|
.\" SUCH DAMAGE. |
|
|
.\" |
|
|
.\" |
|
|
.\" $OpenBSD: strsep.3,v 1.2 1996/08/19 08:34:24 tholo Exp $ |
|
|
|
|
|
|
|
|
.\" @(#)strsep.3 8.1 (Berkeley) 6/9/93 |
|
|
.\" |
|
|
.\" |
|
|
.Dd April 19, 1991 |
|
|
|
|
|
|
|
|
.Dd June 9, 1993 |
|
|
.Dt STRSEP 3 |
|
|
.Dt STRSEP 3 |
|
|
.Os |
|
|
.Os |
|
|
.Sh NAME |
|
|
.Sh NAME |
|
@ -46,23 +49,29 @@ |
|
|
.Sh DESCRIPTION |
|
|
.Sh DESCRIPTION |
|
|
The |
|
|
The |
|
|
.Fn strsep |
|
|
.Fn strsep |
|
|
locates in the null-terminated string at |
|
|
|
|
|
.Fa *stringp |
|
|
|
|
|
the first occurrence of any character in |
|
|
|
|
|
.Fa delim |
|
|
|
|
|
and replaces this with a |
|
|
|
|
|
.Ql \e0 , |
|
|
|
|
|
records the location of the immediate following character in |
|
|
|
|
|
|
|
|
function locates, in the string referenced by |
|
|
.Fa *stringp , |
|
|
.Fa *stringp , |
|
|
then returns the original value of |
|
|
|
|
|
|
|
|
the first occurrence of any character in the string |
|
|
|
|
|
.Fa delim |
|
|
|
|
|
(or the terminating |
|
|
|
|
|
.Ql \e0 |
|
|
|
|
|
character) and replaces it with a |
|
|
|
|
|
.Ql \e0 . |
|
|
|
|
|
The location of the next character after the delimiter character |
|
|
|
|
|
(or NULL, if the end of the string was reached) is stored in |
|
|
.Fa *stringp . |
|
|
.Fa *stringp . |
|
|
If no delimiter characters are found, |
|
|
|
|
|
.Fn strsep |
|
|
|
|
|
sets |
|
|
|
|
|
|
|
|
The original value of |
|
|
|
|
|
.Fa *stringp |
|
|
|
|
|
is returned. |
|
|
|
|
|
.Pp |
|
|
|
|
|
An ``empty'' field, i.e. one caused by two adjacent delimiter characters, |
|
|
|
|
|
can be detected by comparing the location referenced by the pointer returned |
|
|
|
|
|
in |
|
|
.Fa *stringp |
|
|
.Fa *stringp |
|
|
to |
|
|
to |
|
|
.Dv NULL ; |
|
|
|
|
|
if |
|
|
|
|
|
|
|
|
.Ql \e0 . |
|
|
|
|
|
.Pp |
|
|
|
|
|
If |
|
|
.Fa *stringp |
|
|
.Fa *stringp |
|
|
is initially |
|
|
is initially |
|
|
.Dv NULL , |
|
|
.Dv NULL , |
|
@ -72,20 +81,29 @@ returns |
|
|
.Sh EXAMPLES |
|
|
.Sh EXAMPLES |
|
|
The following uses |
|
|
The following uses |
|
|
.Fn strsep |
|
|
.Fn strsep |
|
|
to parse strings containing runs of white space, |
|
|
|
|
|
making up an argument vector: |
|
|
|
|
|
|
|
|
to parse a string, containing tokens delimited by white space, into an |
|
|
|
|
|
argument vector: |
|
|
.Bd -literal -offset indent |
|
|
.Bd -literal -offset indent |
|
|
char inputstring[100]; |
|
|
|
|
|
char **argv[51], **ap = argv, *p, *val; |
|
|
|
|
|
/* set up inputstring */ |
|
|
|
|
|
for (p = inputstring; p != NULL; ) { |
|
|
|
|
|
while ((val = strsep(&p, " \et")) != NULL && *val == '\e0'); |
|
|
|
|
|
*ap++ = val; |
|
|
|
|
|
} |
|
|
|
|
|
*ap = 0; |
|
|
|
|
|
|
|
|
char **ap, *argv[10], *inputstring; |
|
|
|
|
|
|
|
|
|
|
|
for (ap = argv; (*ap = strsep(&inputstring, " \et")) != NULL;) |
|
|
|
|
|
if (**ap != '\e0') |
|
|
|
|
|
++ap; |
|
|
.Ed |
|
|
.Ed |
|
|
.Sh HISTORY |
|
|
.Sh HISTORY |
|
|
The |
|
|
The |
|
|
.Fn strsep |
|
|
.Fn strsep |
|
|
function is |
|
|
|
|
|
.Ud . |
|
|
|
|
|
|
|
|
function |
|
|
|
|
|
is intended as a replacement for the |
|
|
|
|
|
.Fn strtok |
|
|
|
|
|
function. |
|
|
|
|
|
While the |
|
|
|
|
|
.Fn strtok |
|
|
|
|
|
function should be preferred for portability reasons (it conforms to |
|
|
|
|
|
.St -ansiC ) |
|
|
|
|
|
it is unable to handle empty fields, i.e. detect fields delimited by |
|
|
|
|
|
two adjacent delimiter characters, or to be used for more than a single |
|
|
|
|
|
string at a time. |
|
|
|
|
|
The |
|
|
|
|
|
.Fn strsep |
|
|
|
|
|
function first appeared in 4.4BSD. |