diff options
Diffstat (limited to 'src/lib/libc/string/strsep.3')
| -rw-r--r-- | src/lib/libc/string/strsep.3 | 84 |
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 |
| 48 | The | 50 | The |
| 49 | .Fn strsep | 51 | .Fn strsep |
| 50 | locates in the null-terminated string at | 52 | function locates, in the string referenced by |
| 51 | .Fa *stringp | ||
| 52 | the first occurrence of any character in | ||
| 53 | .Fa delim | ||
| 54 | and replaces this with a | ||
| 55 | .Ql \e0 , | ||
| 56 | records the location of the immediate following character in | ||
| 57 | .Fa *stringp , | 53 | .Fa *stringp , |
| 58 | then returns the original value of | 54 | the first occurrence of any character in the string |
| 55 | .Fa delim | ||
| 56 | (or the terminating | ||
| 57 | .Ql \e0 | ||
| 58 | character) and replaces it with a | ||
| 59 | .Ql \e0 . | ||
| 60 | The location of the next character after the delimiter character | ||
| 61 | (or | ||
| 62 | .Dv NULL , | ||
| 63 | if the end of the string was reached) is stored in | ||
| 59 | .Fa *stringp . | 64 | .Fa *stringp . |
| 60 | If no delimiter characters are found, | 65 | The original value of |
| 61 | .Fn strsep | ||
| 62 | sets | ||
| 63 | .Fa *stringp | 66 | .Fa *stringp |
| 67 | is returned. | ||
| 68 | .Pp | ||
| 69 | An | ||
| 70 | .Dq empty | ||
| 71 | field, i.e., one caused by two adjacent delimiter characters, | ||
| 72 | can be detected by comparing the location referenced by the pointer returned | ||
| 73 | by | ||
| 74 | .Fn strsep | ||
| 64 | to | 75 | to |
| 65 | .Dv NULL ; | 76 | .Ql \e0 . |
| 66 | if | 77 | .Pp |
| 78 | If | ||
| 67 | .Fa *stringp | 79 | .Fa *stringp |
| 68 | is initially | 80 | is initially |
| 69 | .Dv NULL , | 81 | .Dv NULL , |
| @@ -73,20 +85,32 @@ returns | |||
| 73 | .Sh EXAMPLES | 85 | .Sh EXAMPLES |
| 74 | The following uses | 86 | The following uses |
| 75 | .Fn strsep | 87 | .Fn strsep |
| 76 | to parse strings containing runs of white space, | 88 | to parse a string, containing tokens delimited by whitespace, into an |
| 77 | making up an argument vector: | 89 | argument vector: |
| 78 | .Bd -literal -offset indent | 90 | .Bd -literal -offset indent |
| 79 | char inputstring[100]; | 91 | char **ap, *argv[10], *inputstring; |
| 80 | char **argv[51], **ap = argv, *p, *val; | 92 | |
| 81 | /* set up inputstring */ | 93 | for (ap = argv; ap < &argv[9] && |
| 82 | for (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 |
| 89 | The | 101 | The |
| 90 | .Fn strsep | 102 | .Fn strsep |
| 91 | function is | 103 | function is intended as a replacement for the |
| 92 | .Ud . | 104 | .Fn strtok |
| 105 | function. | ||
| 106 | While the | ||
| 107 | .Fn strtok | ||
| 108 | function should be preferred for portability reasons (it conforms to | ||
| 109 | .St -ansiC ) | ||
| 110 | it is unable to handle empty fields, i.e., detect fields delimited by | ||
| 111 | two adjacent delimiter characters, or to be used for more than a single | ||
| 112 | string at a time. | ||
| 113 | The | ||
| 114 | .Fn strsep | ||
| 115 | function first appeared in | ||
| 116 | .Bx 4.4 . | ||
