diff options
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/string/strsep.3 | 76 | ||||
| -rw-r--r-- | src/lib/libc/string/strsep.c | 21 |
2 files changed, 61 insertions, 36 deletions
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3 index 0cae3e0f1b..471e693ecc 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.3 1997/08/20 04:28:13 millert 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,9 +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 | .\" $OpenBSD: strsep.3,v 1.2 1996/08/19 08:34:24 tholo Exp $ | 37 | .\" @(#)strsep.3 8.1 (Berkeley) 6/9/93 |
| 35 | .\" | 38 | .\" |
| 36 | .Dd April 19, 1991 | 39 | .Dd June 9, 1993 |
| 37 | .Dt STRSEP 3 | 40 | .Dt STRSEP 3 |
| 38 | .Os | 41 | .Os |
| 39 | .Sh NAME | 42 | .Sh NAME |
| @@ -46,23 +49,29 @@ | |||
| 46 | .Sh DESCRIPTION | 49 | .Sh DESCRIPTION |
| 47 | The | 50 | The |
| 48 | .Fn strsep | 51 | .Fn strsep |
| 49 | locates in the null-terminated string at | 52 | function locates, in the string referenced by |
| 50 | .Fa *stringp | ||
| 51 | the first occurrence of any character in | ||
| 52 | .Fa delim | ||
| 53 | and replaces this with a | ||
| 54 | .Ql \e0 , | ||
| 55 | records the location of the immediate following character in | ||
| 56 | .Fa *stringp , | 53 | .Fa *stringp , |
| 57 | 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 NULL, if the end of the string was reached) is stored in | ||
| 58 | .Fa *stringp . | 62 | .Fa *stringp . |
| 59 | If no delimiter characters are found, | 63 | The original value of |
| 60 | .Fn strsep | 64 | .Fa *stringp |
| 61 | sets | 65 | is returned. |
| 66 | .Pp | ||
| 67 | An ``empty'' field, i.e. one caused by two adjacent delimiter characters, | ||
| 68 | can be detected by comparing the location referenced by the pointer returned | ||
| 69 | in | ||
| 62 | .Fa *stringp | 70 | .Fa *stringp |
| 63 | to | 71 | to |
| 64 | .Dv NULL ; | 72 | .Ql \e0 . |
| 65 | if | 73 | .Pp |
| 74 | If | ||
| 66 | .Fa *stringp | 75 | .Fa *stringp |
| 67 | is initially | 76 | is initially |
| 68 | .Dv NULL , | 77 | .Dv NULL , |
| @@ -72,20 +81,29 @@ returns | |||
| 72 | .Sh EXAMPLES | 81 | .Sh EXAMPLES |
| 73 | The following uses | 82 | The following uses |
| 74 | .Fn strsep | 83 | .Fn strsep |
| 75 | to parse strings containing runs of white space, | 84 | to parse a string, containing tokens delimited by white space, into an |
| 76 | making up an argument vector: | 85 | argument vector: |
| 77 | .Bd -literal -offset indent | 86 | .Bd -literal -offset indent |
| 78 | char inputstring[100]; | 87 | char **ap, *argv[10], *inputstring; |
| 79 | char **argv[51], **ap = argv, *p, *val; | 88 | |
| 80 | /* set up inputstring */ | 89 | for (ap = argv; (*ap = strsep(&inputstring, " \et")) != NULL;) |
| 81 | for (p = inputstring; p != NULL; ) { | 90 | if (**ap != '\e0') |
| 82 | while ((val = strsep(&p, " \et")) != NULL && *val == '\e0'); | 91 | ++ap; |
| 83 | *ap++ = val; | ||
| 84 | } | ||
| 85 | *ap = 0; | ||
| 86 | .Ed | 92 | .Ed |
| 87 | .Sh HISTORY | 93 | .Sh HISTORY |
| 88 | The | 94 | The |
| 89 | .Fn strsep | 95 | .Fn strsep |
| 90 | function is | 96 | function |
| 91 | .Ud . | 97 | is intended as a replacement for the |
| 98 | .Fn strtok | ||
| 99 | function. | ||
| 100 | While the | ||
| 101 | .Fn strtok | ||
| 102 | function should be preferred for portability reasons (it conforms to | ||
| 103 | .St -ansiC ) | ||
| 104 | it is unable to handle empty fields, i.e. detect fields delimited by | ||
| 105 | two adjacent delimiter characters, or to be used for more than a single | ||
| 106 | string at a time. | ||
| 107 | The | ||
| 108 | .Fn strsep | ||
| 109 | function first appeared in 4.4BSD. | ||
diff --git a/src/lib/libc/string/strsep.c b/src/lib/libc/string/strsep.c index 09f187b62d..b69b715fc5 100644 --- a/src/lib/libc/string/strsep.c +++ b/src/lib/libc/string/strsep.c | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | /* $OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $ */ | ||
| 2 | |||
| 1 | /*- | 3 | /*- |
| 2 | * Copyright (c) 1990 The Regents of the University of California. | 4 | * Copyright (c) 1990, 1993 |
| 3 | * All rights reserved. | 5 | * The Regents of the University of California. All rights reserved. |
| 4 | * | 6 | * |
| 5 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
| @@ -31,14 +33,19 @@ | |||
| 31 | * SUCH DAMAGE. | 33 | * SUCH DAMAGE. |
| 32 | */ | 34 | */ |
| 33 | 35 | ||
| 36 | #include <string.h> | ||
| 37 | #include <stdio.h> | ||
| 38 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | 39 | #if defined(LIBC_SCCS) && !defined(lint) |
| 35 | static char *rcsid = "$OpenBSD: strsep.c,v 1.2 1996/08/19 08:34:24 tholo Exp $"; | 40 | #if 0 |
| 41 | static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; | ||
| 42 | #else | ||
| 43 | static char *rcsid = "$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $"; | ||
| 44 | #endif | ||
| 36 | #endif /* LIBC_SCCS and not lint */ | 45 | #endif /* LIBC_SCCS and not lint */ |
| 37 | 46 | ||
| 38 | #include <string.h> | ||
| 39 | |||
| 40 | /* | 47 | /* |
| 41 | * Get next token from string *stringp, where tokens are nonempty | 48 | * Get next token from string *stringp, where tokens are possibly-empty |
| 42 | * strings separated by characters from delim. | 49 | * strings separated by characters from delim. |
| 43 | * | 50 | * |
| 44 | * Writes NULs into the string at *stringp to end tokens. | 51 | * Writes NULs into the string at *stringp to end tokens. |
| @@ -46,7 +53,7 @@ static char *rcsid = "$OpenBSD: strsep.c,v 1.2 1996/08/19 08:34:24 tholo Exp $"; | |||
| 46 | * On return, *stringp points past the last NUL written (if there might | 53 | * On return, *stringp points past the last NUL written (if there might |
| 47 | * be further tokens), or is NULL (if there are definitely no more tokens). | 54 | * be further tokens), or is NULL (if there are definitely no more tokens). |
| 48 | * | 55 | * |
| 49 | * If *stringp is NULL, strtoken returns NULL. | 56 | * If *stringp is NULL, strsep returns NULL. |
| 50 | */ | 57 | */ |
| 51 | char * | 58 | char * |
| 52 | strsep(stringp, delim) | 59 | strsep(stringp, delim) |
