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