]>
Commit | Line | Data |
---|---|---|
a1eaacb1 | 1 | '\" t |
fea681da MK |
2 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
3 | .\" | |
5fbde956 | 4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da MK |
5 | .\" |
6 | .\" References consulted: | |
7 | .\" Linux libc source code | |
8 | .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) | |
9 | .\" 386BSD man pages | |
10 | .\" Modified Sat Jul 24 18:00:10 1993 by Rik Faith (faith@cs.unc.edu) | |
11 | .\" Modified Mon Jan 20 12:04:18 1997 by Andries Brouwer (aeb@cwi.nl) | |
12 | .\" Modified Tue Jan 23 20:23:07 2001 by Andries Brouwer (aeb@cwi.nl) | |
13 | .\" | |
4c1c5274 | 14 | .TH strsep 3 (date) "Linux man-pages (unreleased)" |
fea681da MK |
15 | .SH NAME |
16 | strsep \- extract token from string | |
5e277835 AC |
17 | .SH LIBRARY |
18 | Standard C library | |
8fc3b2cf | 19 | .RI ( libc ", " \-lc ) |
fea681da MK |
20 | .SH SYNOPSIS |
21 | .nf | |
22 | .B #include <string.h> | |
68e4db0a | 23 | .PP |
2d91ea53 | 24 | .BI "char *strsep(char **restrict " stringp ", const char *restrict " delim ); |
fea681da | 25 | .fi |
68e4db0a | 26 | .PP |
d39ad78f | 27 | .RS -4 |
cc4615cc MK |
28 | Feature Test Macro Requirements for glibc (see |
29 | .BR feature_test_macros (7)): | |
d39ad78f | 30 | .RE |
68e4db0a | 31 | .PP |
cc4615cc | 32 | .BR strsep (): |
9d281e06 | 33 | .nf |
51c612fb MK |
34 | Since glibc 2.19: |
35 | _DEFAULT_SOURCE | |
75c018a1 | 36 | glibc 2.19 and earlier: |
51c612fb | 37 | _BSD_SOURCE |
9d281e06 | 38 | .fi |
fea681da | 39 | .SH DESCRIPTION |
46d8df8e MK |
40 | If |
41 | .I *stringp | |
42 | is NULL, the | |
60a90ecd MK |
43 | .BR strsep () |
44 | function returns NULL | |
c13182ef MK |
45 | and does nothing else. |
46 | Otherwise, this function finds the first token | |
46d8df8e | 47 | in the string |
addd36ed | 48 | .I *stringp |
4d9fbd4e | 49 | that is delimited by one of the bytes in the string |
46d8df8e | 50 | .IR delim . |
71d9e7ae | 51 | This token is terminated by overwriting the delimiter |
b957f81f | 52 | with a null byte (\[aq]\e0\[aq]), |
46d8df8e MK |
53 | and |
54 | .I *stringp | |
55 | is updated to point past the token. | |
fea681da | 56 | In case no delimiter was found, the token is taken to be |
46d8df8e MK |
57 | the entire string |
58 | .IR *stringp , | |
59 | and | |
60 | .I *stringp | |
61 | is made NULL. | |
47297adb | 62 | .SH RETURN VALUE |
60a90ecd MK |
63 | The |
64 | .BR strsep () | |
65 | function returns a pointer to the token, | |
46d8df8e MK |
66 | that is, it returns the original value of |
67 | .IR *stringp . | |
178c91f4 | 68 | .SH ATTRIBUTES |
0c5deb50 PH |
69 | For an explanation of the terms used in this section, see |
70 | .BR attributes (7). | |
71 | .TS | |
72 | allbox; | |
c466875e | 73 | lbx lb lb |
0c5deb50 PH |
74 | l l l. |
75 | Interface Attribute Value | |
76 | T{ | |
9e54434e BR |
77 | .na |
78 | .nh | |
178c91f4 | 79 | .BR strsep () |
0c5deb50 PH |
80 | T} Thread safety MT-Safe |
81 | .TE | |
c466875e | 82 | .sp 1 |
3113c7f3 | 83 | .SH STANDARDS |
4131356c AC |
84 | None. |
85 | .SH HISTORY | |
44a2c328 | 86 | 4.4BSD. |
4131356c | 87 | .PP |
60a90ecd MK |
88 | The |
89 | .BR strsep () | |
90 | function was introduced as a replacement for | |
91 | .BR strtok (3), | |
92 | since the latter cannot handle empty fields. | |
93 | However, | |
94 | .BR strtok (3) | |
e0089d55 | 95 | conforms to C89/C99 and hence is more portable. |
fea681da | 96 | .SH BUGS |
d1a7f7d9 MK |
97 | Be cautious when using this function. |
98 | If you do use it, note that: | |
cdede5cd | 99 | .IP \[bu] 3 |
d1a7f7d9 | 100 | This function modifies its first argument. |
cdede5cd | 101 | .IP \[bu] |
d1a7f7d9 | 102 | This function cannot be used on constant strings. |
cdede5cd | 103 | .IP \[bu] |
d1a7f7d9 | 104 | The identity of the delimiting character is lost. |
395380d3 | 105 | .SH EXAMPLES |
106 | The program below is a port of the one found in | |
107 | .BR strtok (3), | |
108 | which, however, doesn't discard multiple delimiters or empty tokens: | |
109 | .PP | |
110 | .in +4n | |
111 | .EX | |
b957f81f | 112 | .RB "$" " ./a.out \[aq]a/bbb///cc;xxx:yyy:\[aq] \[aq]:;\[aq] \[aq]/\[aq]" |
395380d3 | 113 | 1: a/bbb///cc |
114 | \-\-> a | |
115 | \-\-> bbb | |
116 | \-\-> | |
117 | \-\-> | |
118 | \-\-> cc | |
119 | 2: xxx | |
120 | \-\-> xxx | |
121 | 3: yyy | |
122 | \-\-> yyy | |
123 | 4: | |
124 | \-\-> | |
125 | .EE | |
126 | .in | |
127 | .SS Program source | |
128 | \& | |
b0b6ab4e | 129 | .\" SRC BEGIN (strsep.c) |
395380d3 | 130 | .EX |
131 | #include <stdio.h> | |
132 | #include <stdlib.h> | |
133 | #include <string.h> | |
fe5dba13 | 134 | \& |
395380d3 | 135 | int |
136 | main(int argc, char *argv[]) | |
137 | { | |
138 | char *token, *subtoken; | |
fe5dba13 | 139 | \& |
395380d3 | 140 | if (argc != 4) { |
764b8fa9 | 141 | fprintf(stderr, "Usage: %s string delim subdelim\en", argv[0]); |
395380d3 | 142 | exit(EXIT_FAILURE); |
143 | } | |
fe5dba13 | 144 | \& |
b42296e4 AC |
145 | for (unsigned int j = 1; (token = strsep(&argv[1], argv[2])); j++) { |
146 | printf("%u: %s\en", j, token); | |
fe5dba13 | 147 | \& |
395380d3 | 148 | while ((subtoken = strsep(&token, argv[3]))) |
149 | printf("\et \-\-> %s\en", subtoken); | |
150 | } | |
fe5dba13 | 151 | \& |
395380d3 | 152 | exit(EXIT_SUCCESS); |
153 | } | |
154 | .EE | |
b0b6ab4e | 155 | .\" SRC END |
47297adb | 156 | .SH SEE ALSO |
fea681da | 157 | .BR memchr (3), |
fea681da | 158 | .BR strchr (3), |
d095200e | 159 | .BR string (3), |
fea681da MK |
160 | .BR strpbrk (3), |
161 | .BR strspn (3), | |
162 | .BR strstr (3), | |
163 | .BR strtok (3) |