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