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