]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) |
2 | .\" | |
3 | .\" Permission is granted to make and distribute verbatim copies of this | |
4 | .\" manual provided the copyright notice and this permission notice are | |
5 | .\" preserved on all copies. | |
6 | .\" | |
7 | .\" Permission is granted to copy and distribute modified versions of this | |
8 | .\" manual under the conditions for verbatim copying, provided that the | |
9 | .\" entire resulting derived work is distributed under the terms of a | |
10 | .\" permission notice identical to this one. | |
c13182ef | 11 | .\" |
fea681da MK |
12 | .\" Since the Linux kernel and libraries are constantly changing, this |
13 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
14 | .\" responsibility for errors or omissions, or for damages resulting from | |
15 | .\" the use of the information contained herein. The author(s) may not | |
16 | .\" have taken the same level of care in the production of this manual, | |
17 | .\" which is licensed free of charge, as they might when working | |
18 | .\" professionally. | |
c13182ef | 19 | .\" |
fea681da MK |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" License. | |
23 | .\" Modified Wed Jul 28 11:12:17 1993 by Rik Faith (faith@cs.unc.edu) | |
24 | .\" Modified Mon May 13 23:08:50 1996 by Martin Schulze (joey@linux.de) | |
25 | .\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk) | |
26 | .\" Modified 990912 by aeb | |
27 | .\" | |
28 | .TH GLOB 3 1999-09-12 "GNU" "Linux Programmer's Manual" | |
29 | .SH NAME | |
30 | glob, globfree \- find pathnames matching a pattern, free memory from glob() | |
31 | .SH SYNOPSIS | |
32 | .nf | |
33 | .B #include <glob.h> | |
34 | .sp | |
35 | .BI "int glob(const char *" pattern ", int " flags , | |
defcceb3 | 36 | .br |
69f8784e | 37 | .BI " int (*" errfunc ") (const char *" epath ", int " eerrno ), |
defcceb3 | 38 | .br |
fea681da | 39 | .BI " glob_t *" pglob ); |
defcceb3 | 40 | .br |
fea681da MK |
41 | .BI "void globfree(glob_t *" pglob ); |
42 | .fi | |
43 | .SH DESCRIPTION | |
44 | The | |
63aa9df0 | 45 | .BR glob () |
fea681da MK |
46 | function searches for all the pathnames matching |
47 | .I pattern | |
48 | according to the rules used by the shell (see | |
49 | .BR glob (7)). | |
50 | No tilde expansion or parameter substitution is done; if you want | |
51 | these, use | |
52 | .BR wordexp (3). | |
53 | .PP | |
54 | The | |
63aa9df0 | 55 | .BR globfree () |
fea681da MK |
56 | function frees the dynamically allocated storage from an earlier call |
57 | to | |
63aa9df0 | 58 | .BR glob (). |
fea681da MK |
59 | .PP |
60 | The results of a | |
63aa9df0 | 61 | .BR glob () |
fea681da MK |
62 | call are stored in the structure pointed to by |
63 | .IR pglob , | |
64 | which is a | |
65 | .B glob_t | |
66 | which is declared in | |
bd12ab88 | 67 | .I <glob.h> |
fea681da MK |
68 | and includes the following elements defined by POSIX.2 (more may be |
69 | present as an extension): | |
70 | .PP | |
71 | .br | |
72 | .nf | |
73 | .in 10 | |
b9f02710 MK |
74 | typedef struct { |
75 | size_t gl_pathc; /* Count of paths matched so far */ | |
76 | char **gl_pathv; /* List of matched pathnames. */ | |
77 | size_t gl_offs; /* Slots to reserve in `gl_pathv'. */ | |
fea681da MK |
78 | } glob_t; |
79 | .fi | |
80 | .PP | |
81 | Results are stored in dynamically allocated storage. | |
82 | .PP | |
83 | The parameter | |
84 | .I flags | |
85 | is made up of bitwise OR of zero or more the following symbolic | |
86 | constants, which modify the of behaviour of | |
63aa9df0 | 87 | .BR glob (): |
fea681da MK |
88 | .TP |
89 | .B GLOB_ERR | |
90 | which means to return upon read error (because a directory does not | |
91 | have read permission, for example), | |
92 | .TP | |
93 | .B GLOB_MARK | |
94 | which means to append a slash to each path which corresponds to a directory, | |
95 | .TP | |
96 | .B GLOB_NOSORT | |
97 | which means don't sort the returned pathnames (they are by default), | |
98 | .TP | |
99 | .B GLOB_DOOFFS | |
100 | which means that | |
101 | .I pglob->gl_offs | |
102 | slots will be reserved at the beginning of the list of strings in | |
103 | .IR pglob->pathv , | |
104 | .TP | |
105 | .B GLOB_NOCHECK | |
106 | which means that, if no pattern matches, to return the original pattern, | |
107 | .TP | |
108 | .B GLOB_APPEND | |
c13182ef MK |
109 | which means to append to the results of a previous call. |
110 | Do not set this flag on the first invocation of | |
63aa9df0 | 111 | .BR glob (). |
fea681da MK |
112 | .TP |
113 | .B GLOB_NOESCAPE | |
114 | which means that meta characters cannot be quoted by backslashes. | |
115 | .PP | |
116 | The flags may also include some of the following, which are GNU | |
117 | extensions and not defined by POSIX.2: | |
118 | .TP | |
119 | .B GLOB_PERIOD | |
120 | which means that a leading period can be matched by meta characters, | |
121 | .TP | |
122 | .B GLOB_ALTDIRFUNC | |
123 | which means that alternative functions | |
124 | .IR pglob->gl_closedir , | |
125 | .IR pglob->gl_readdir , | |
126 | .IR pglob->gl_opendir , | |
127 | .IR pglob->gl_lstat ", and" | |
128 | .I pglob->gl_stat | |
129 | are used for file system access instead of the normal library | |
130 | functions, | |
131 | .TP | |
132 | .B GLOB_BRACE | |
133 | which means that | |
134 | .BR csh (1) | |
b9560046 | 135 | style brace expressions \fB{a,b}\fR are expanded, |
fea681da MK |
136 | .TP |
137 | .B GLOB_NOMAGIC | |
138 | which means that the pattern is returned if it contains no metacharacters, | |
139 | .TP | |
140 | .B GLOB_TILDE | |
141 | which means that tilde expansion is carried out, and | |
142 | .TP | |
143 | .B GLOB_ONLYDIR | |
144 | which means that only directories are matched. | |
145 | .PP | |
146 | If | |
147 | .I errfunc | |
8478ee02 | 148 | is not NULL, |
fea681da MK |
149 | it will be called in case of an error with the arguments |
150 | .IR epath , | |
151 | a pointer to the path which failed, and | |
152 | .IR eerrno , | |
153 | the value of | |
154 | .I errno | |
155 | as returned from one of the calls to | |
c13182ef MK |
156 | .BR opendir (), |
157 | .BR readdir (), | |
158 | or | |
4d52e8f8 | 159 | .BR stat (). |
c13182ef | 160 | If |
fea681da MK |
161 | .I errfunc |
162 | returns non-zero, or if | |
163 | .B GLOB_ERR | |
c13182ef | 164 | is set, |
63aa9df0 | 165 | .BR glob () |
fea681da MK |
166 | will terminate after the call to |
167 | .IR errfunc . | |
168 | .PP | |
c13182ef | 169 | Upon successful return, |
fea681da MK |
170 | .I pglob->gl_pathc |
171 | contains the number of matched pathnames and | |
172 | .I pglob->gl_pathv | |
c13182ef MK |
173 | a pointer to the list of matched pathnames. |
174 | The first pointer after the last pathname is NULL. | |
fea681da MK |
175 | .PP |
176 | It is possible to call | |
63aa9df0 | 177 | .BR glob () |
c13182ef MK |
178 | several times. |
179 | In that case, the | |
fea681da MK |
180 | .B GLOB_APPEND |
181 | flag has to be set in | |
182 | .I flags | |
183 | on the second and later invocations. | |
184 | .PP | |
185 | As a GNU extension, | |
186 | .I pglob->gl_flags | |
187 | is set to the flags specified, \fBor\fRed with | |
188 | .B GLOB_MAGCHAR | |
189 | if any metacharacters were found. | |
190 | .SH "RETURN VALUE" | |
c13182ef | 191 | On successful completion, |
63aa9df0 | 192 | .BR glob () |
fea681da MK |
193 | returns zero. |
194 | Other possible returns are: | |
195 | .TP | |
196 | .B GLOB_NOSPACE | |
197 | for running out of memory, | |
198 | .TP | |
199 | .B GLOB_ABORTED | |
200 | for a read error, and | |
201 | .TP | |
202 | .B GLOB_NOMATCH | |
203 | for no found matches. | |
9b336505 | 204 | .SH EXAMPLE |
fea681da | 205 | One example of use is the following code, which simulates typing |
defcceb3 | 206 | .sp |
c65433e6 | 207 | .B ls \-l *.c ../*.c |
defcceb3 MK |
208 | .sp |
209 | in the shell: | |
fea681da MK |
210 | .nf |
211 | .in 10 | |
212 | ||
213 | glob_t globbuf; | |
214 | ||
215 | globbuf.gl_offs = 2; | |
216 | glob("*.c", GLOB_DOOFFS, NULL, &globbuf); | |
217 | glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf); | |
218 | globbuf.gl_pathv[0] = "ls"; | |
8c383102 | 219 | globbuf.gl_pathv[1] = "\-l"; |
fea681da MK |
220 | execvp("ls", &globbuf.gl_pathv[0]); |
221 | .fi | |
222 | .SH "CONFORMING TO" | |
68e1685c | 223 | POSIX.2, POSIX.1-2001. |
fea681da MK |
224 | .SH BUGS |
225 | The | |
63aa9df0 | 226 | .BR glob () |
fea681da | 227 | function may fail due to failure of underlying function calls, such as |
c13182ef | 228 | .BR malloc () |
e1d6264d MK |
229 | or |
230 | .BR opendir (). | |
fea681da MK |
231 | These will store their error code in |
232 | .IR errno . | |
233 | .SH NOTES | |
234 | The structure elements | |
235 | .I gl_pathc | |
236 | and | |
237 | .I gl_offs | |
238 | are declared as | |
239 | .BR size_t | |
240 | in glibc 2.1, as they should according to POSIX.2, | |
241 | but are declared as | |
9ff08aad | 242 | .I int |
fea681da MK |
243 | in libc4, libc5 and glibc 2.0. |
244 | .SH "SEE ALSO" | |
245 | .BR ls (1), | |
246 | .BR sh (1), | |
247 | .BR stat (2), | |
248 | .BR exec (3), | |
2ff9c803 | 249 | .BR fnmatch (3), |
fea681da MK |
250 | .BR malloc (3), |
251 | .BR opendir (3), | |
252 | .BR readdir (3), | |
253 | .BR wordexp (3), | |
254 | .BR glob (7) |