]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/glob.3
Many pages: Use \[ti] instead of \(ti
[thirdparty/man-pages.git] / man3 / glob.3
CommitLineData
a1eaacb1 1'\" t
bf5a7247 2.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
fea681da 3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
c08df37a 5.\"
fea681da
MK
6.\" Modified Wed Jul 28 11:12:17 1993 by Rik Faith (faith@cs.unc.edu)
7.\" Modified Mon May 13 23:08:50 1996 by Martin Schulze (joey@linux.de)
8.\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
9.\" Modified 990912 by aeb
7c2cb022
MK
10.\" 2007-10-10 mtk
11.\" Added description of GLOB_TILDE_NOMATCH
12.\" Expanded the description of various flags
13.\" Various wording fixes.
fea681da 14.\"
4c1c5274 15.TH glob 3 (date) "Linux man-pages (unreleased)"
fea681da
MK
16.SH NAME
17glob, globfree \- find pathnames matching a pattern, free memory from glob()
42009080
AC
18.SH LIBRARY
19Standard C library
20.RI ( libc ", " \-lc )
fea681da
MK
21.SH SYNOPSIS
22.nf
23.B #include <glob.h>
68e4db0a 24.PP
86ce8583 25.BI "int glob(const char *restrict " pattern ", int " flags ,
511bb71b 26.BI " int (*" errfunc ")(const char *" epath ", int " eerrno ),
86ce8583 27.BI " glob_t *restrict " pglob );
fea681da
MK
28.BI "void globfree(glob_t *" pglob );
29.fi
30.SH DESCRIPTION
31The
63aa9df0 32.BR glob ()
fea681da
MK
33function searches for all the pathnames matching
34.I pattern
35according to the rules used by the shell (see
36.BR glob (7)).
37No tilde expansion or parameter substitution is done; if you want
38these, use
39.BR wordexp (3).
40.PP
41The
63aa9df0 42.BR globfree ()
fea681da
MK
43function frees the dynamically allocated storage from an earlier call
44to
63aa9df0 45.BR glob ().
fea681da
MK
46.PP
47The results of a
63aa9df0 48.BR glob ()
fea681da 49call are stored in the structure pointed to by
7c2cb022
MK
50.IR pglob .
51This structure is of type
f19a0f03 52.I glob_t
7c2cb022
MK
53(declared in
54.IR <glob.h> )
fea681da
MK
55and includes the following elements defined by POSIX.2 (more may be
56present as an extension):
57.PP
088a639b 58.in +4n
b8302363 59.EX
b9f02710
MK
60typedef struct {
61 size_t gl_pathc; /* Count of paths matched so far */
62 char **gl_pathv; /* List of matched pathnames. */
a40ee90b 63 size_t gl_offs; /* Slots to reserve in \fIgl_pathv\fP. */
fea681da 64} glob_t;
b8302363 65.EE
088a639b 66.in
fea681da
MK
67.PP
68Results are stored in dynamically allocated storage.
69.PP
c4bb193f 70The argument
fea681da 71.I flags
7c2cb022
MK
72is made up of the bitwise OR of zero or more the following symbolic
73constants, which modify the behavior of
63aa9df0 74.BR glob ():
fea681da
MK
75.TP
76.B GLOB_ERR
7c2cb022
MK
77Return upon a read error (because a directory does not
78have read permission, for example).
79By default,
80.BR glob ()
38f76cd2 81attempts carry on despite errors,
7c2cb022 82reading all of the directories that it can.
fea681da
MK
83.TP
84.B GLOB_MARK
7c2cb022 85Append a slash to each path which corresponds to a directory.
fea681da
MK
86.TP
87.B GLOB_NOSORT
7c2cb022
MK
88Don't sort the returned pathnames.
89The only reason to do this is to save processing time.
90By default, the returned pathnames are sorted.
fea681da
MK
91.TP
92.B GLOB_DOOFFS
7c2cb022 93Reserve
94e9d9fe 94.I pglob\->gl_offs
7c2cb022 95slots at the beginning of the list of strings in
94e9d9fe 96.IR pglob\->pathv .
b437fdd9 97The reserved slots contain null pointers.
fea681da
MK
98.TP
99.B GLOB_NOCHECK
7c2cb022
MK
100If no pattern matches, return the original pattern.
101By default,
102.BR glob ()
103returns
104.B GLOB_NOMATCH
105if there are no matches.
fea681da
MK
106.TP
107.B GLOB_APPEND
7c2cb022
MK
108Append the results of this call to the vector of results
109returned by a previous call to
110.BR glob ().
c13182ef 111Do not set this flag on the first invocation of
63aa9df0 112.BR glob ().
fea681da
MK
113.TP
114.B GLOB_NOESCAPE
b957f81f 115Don't allow backslash (\[aq]\e\[aq]) to be used as an escape
7c2cb022
MK
116character.
117Normally, a backslash can be used to quote the following character,
118providing a mechanism to turn off the special meaning
119metacharacters.
fea681da 120.PP
7c2cb022
MK
121.I flags
122may also include any of the following, which are GNU
fea681da
MK
123extensions and not defined by POSIX.2:
124.TP
125.B GLOB_PERIOD
7c2cb022
MK
126Allow a leading period to be matched by metacharacters.
127By default, metacharacters can't match a leading period.
fea681da
MK
128.TP
129.B GLOB_ALTDIRFUNC
7c2cb022 130Use alternative functions
94e9d9fe
MK
131.IR pglob\->gl_closedir ,
132.IR pglob\->gl_readdir ,
133.IR pglob\->gl_opendir ,
06e88ab8 134.IR pglob\->gl_lstat ,
135and
94e9d9fe 136.I pglob\->gl_stat
9ee4a2b6 137for filesystem access instead of the normal library
7c2cb022 138functions.
fea681da
MK
139.TP
140.B GLOB_BRACE
7c2cb022 141Expand
fea681da 142.BR csh (1)
c9942389
MK
143style brace expressions of the form
144.BR {a,b} .
7c2cb022
MK
145Brace expressions can be nested.
146Thus, for example, specifying the pattern
147"{foo/{,cat,dog},bar}" would return the same results as four separate
148.BR glob ()
149calls using the strings:
150"foo/",
151"foo/cat",
152"foo/dog",
153and
154"bar".
fea681da
MK
155.TP
156.B GLOB_NOMAGIC
6259c9b7 157If the pattern contains no metacharacters,
7c2cb022
MK
158then it should be returned as the sole matching word,
159even if there is no file with that name.
fea681da
MK
160.TP
161.B GLOB_TILDE
7c2cb022 162Carry out tilde expansion.
3f029bc9 163If a tilde (\[aq]\[ti]\[aq]) is the only character in the pattern,
b957f81f 164or an initial tilde is followed immediately by a slash (\[aq]/\[aq]),
7c2cb022
MK
165then the home directory of the caller is substituted for
166the tilde.
3f029bc9 167If an initial tilde is followed by a username (e.g., "\[ti]andrea/bin"),
7c2cb022
MK
168then the tilde and username are substituted by the home directory
169of that user.
170If the username is invalid, or the home directory cannot be
171determined, then no substitution is performed.
172.TP
173.B GLOB_TILDE_CHECK
174This provides behavior similar to that of
175.BR GLOB_TILDE .
176The difference is that if the username is invalid, or the
177home directory cannot be determined, then
178instead of using the pattern itself as the name,
179.BR glob ()
180returns
1ae6b2c7 181.B GLOB_NOMATCH
7c2cb022 182to indicate an error.
fea681da
MK
183.TP
184.B GLOB_ONLYDIR
38f76cd2 185This is a
7c2cb022
MK
186.I hint
187to
188.BR glob ()
189that the caller is interested only in directories that match the pattern.
190If the implementation can easily determine file-type information,
24b74457 191then nondirectory files are not returned to the caller.
7c2cb022
MK
192However, the caller must still check that returned files
193are directories.
194(The purpose of this flag is merely to optimize performance when
ed145e47 195the caller is interested only in directories.)
fea681da
MK
196.PP
197If
198.I errfunc
8478ee02 199is not NULL,
fea681da
MK
200it will be called in case of an error with the arguments
201.IR epath ,
202a pointer to the path which failed, and
203.IR eerrno ,
204the value of
205.I errno
206as returned from one of the calls to
91fe9d5c
MK
207.BR opendir (3),
208.BR readdir (3),
c13182ef 209or
91fe9d5c 210.BR stat (2).
c13182ef 211If
fea681da 212.I errfunc
c7094399 213returns nonzero, or if
fea681da 214.B GLOB_ERR
c13182ef 215is set,
63aa9df0 216.BR glob ()
fea681da
MK
217will terminate after the call to
218.IR errfunc .
219.PP
c13182ef 220Upon successful return,
94e9d9fe 221.I pglob\->gl_pathc
fea681da 222contains the number of matched pathnames and
94e9d9fe 223.I pglob\->gl_pathv
7c2cb022 224contains a pointer to the list of pointers to matched pathnames.
b437fdd9 225The list of pointers is terminated by a null pointer.
fea681da
MK
226.PP
227It is possible to call
63aa9df0 228.BR glob ()
c13182ef
MK
229several times.
230In that case, the
fea681da
MK
231.B GLOB_APPEND
232flag has to be set in
233.I flags
234on the second and later invocations.
235.PP
236As a GNU extension,
94e9d9fe 237.I pglob\->gl_flags
c9942389
MK
238is set to the flags specified,
239.BR or ed
240with
fea681da
MK
241.B GLOB_MAGCHAR
242if any metacharacters were found.
47297adb 243.SH RETURN VALUE
c13182ef 244On successful completion,
63aa9df0 245.BR glob ()
fea681da
MK
246returns zero.
247Other possible returns are:
248.TP
249.B GLOB_NOSPACE
250for running out of memory,
251.TP
252.B GLOB_ABORTED
253for a read error, and
254.TP
255.B GLOB_NOMATCH
256for no found matches.
624e7579
ZL
257.SH ATTRIBUTES
258For an explanation of the terms used in this section, see
259.BR attributes (7).
c466875e
MK
260.ad l
261.nh
624e7579
ZL
262.TS
263allbox;
c466875e 264lb lb lbx
624e7579
ZL
265l l l.
266Interface Attribute Value
267T{
268.BR glob ()
269T} Thread safety T{
270MT-Unsafe race:utent env
624e7579
ZL
271sig:ALRM timer locale
272T}
273T{
274.BR globfree ()
275T} Thread safety MT-Safe
276.TE
c466875e
MK
277.hy
278.ad
847e0d88 279.sp 1
624e7579
ZL
280In the above table,
281.I utent
282in
283.I race:utent
284signifies that if any of the functions
285.BR setutent (3),
286.BR getutent (3),
287or
288.BR endutent (3)
289are used in parallel in different threads of a program,
290then data races could occur.
bf7bc8b8 291.BR glob ()
624e7579
ZL
292calls those functions,
293so we use race:utent to remind users.
3113c7f3 294.SH STANDARDS
ad6e6e50 295POSIX.1-2001, POSIX.1-2008, POSIX.2.
2b2581ee
MK
296.SH NOTES
297The structure elements
298.I gl_pathc
299and
300.I gl_offs
301are declared as
0daa9e92 302.I size_t
7c2cb022 303in glibc 2.1, as they should be according to POSIX.2,
2b2581ee
MK
304but are declared as
305.I int
f51d8d4d 306in glibc 2.0.
2b2581ee
MK
307.SH BUGS
308The
309.BR glob ()
310function may fail due to failure of underlying function calls, such as
311.BR malloc (3)
312or
313.BR opendir (3).
314These will store their error code in
315.IR errno .
a14af333 316.SH EXAMPLES
fea681da 317One example of use is the following code, which simulates typing
51f5698d 318.PP
296d592b 319.in +4n
bdd915e2 320.EX
296d592b 321ls \-l *.c ../*.c
bdd915e2 322.EE
296d592b 323.in
51f5698d 324.PP
defcceb3 325in the shell:
e646a1ba 326.PP
296d592b 327.in +4n
e646a1ba 328.EX
fea681da
MK
329glob_t globbuf;
330
331globbuf.gl_offs = 2;
332glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
333glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
334globbuf.gl_pathv[0] = "ls";
8c383102 335globbuf.gl_pathv[1] = "\-l";
fea681da 336execvp("ls", &globbuf.gl_pathv[0]);
b8302363 337.EE
e646a1ba 338.in
47297adb 339.SH SEE ALSO
fea681da
MK
340.BR ls (1),
341.BR sh (1),
342.BR stat (2),
343.BR exec (3),
2ff9c803 344.BR fnmatch (3),
fea681da
MK
345.BR malloc (3),
346.BR opendir (3),
347.BR readdir (3),
348.BR wordexp (3),
349.BR glob (7)