]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/glob.3
tsearch.3: Minor tweak to Florian's patch
[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.\"
9ba01802 34.TH GLOB 3 2019-03-06 "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 ,
69f8784e 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.
f81fb444
MK
178If a tilde (\(aq~\(aq) is the only character in the pattern,
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.
182If an initial tilde is followed by a username (e.g., "~andrea/bin"),
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).
275.TS
276allbox;
277lb lb lbw24
278l l l.
279Interface Attribute Value
280T{
281.BR glob ()
282T} Thread safety T{
283MT-Unsafe race:utent env
284.br
285sig:ALRM timer locale
286T}
287T{
288.BR globfree ()
289T} Thread safety MT-Safe
290.TE
847e0d88 291.sp 1
624e7579
ZL
292In the above table,
293.I utent
294in
295.I race:utent
296signifies that if any of the functions
297.BR setutent (3),
298.BR getutent (3),
299or
300.BR endutent (3)
301are used in parallel in different threads of a program,
302then data races could occur.
bf7bc8b8 303.BR glob ()
624e7579
ZL
304calls those functions,
305so we use race:utent to remind users.
47297adb 306.SH CONFORMING TO
ad6e6e50 307POSIX.1-2001, POSIX.1-2008, POSIX.2.
2b2581ee
MK
308.SH NOTES
309The structure elements
310.I gl_pathc
311and
312.I gl_offs
313are declared as
0daa9e92 314.I size_t
7c2cb022 315in glibc 2.1, as they should be according to POSIX.2,
2b2581ee
MK
316but are declared as
317.I int
f51d8d4d 318in glibc 2.0.
2b2581ee
MK
319.SH BUGS
320The
321.BR glob ()
322function may fail due to failure of underlying function calls, such as
323.BR malloc (3)
324or
325.BR opendir (3).
326These will store their error code in
327.IR errno .
9b336505 328.SH EXAMPLE
fea681da 329One example of use is the following code, which simulates typing
51f5698d 330.PP
296d592b 331.in +4n
bdd915e2 332.EX
296d592b 333ls \-l *.c ../*.c
bdd915e2 334.EE
296d592b 335.in
51f5698d 336.PP
defcceb3 337in the shell:
e646a1ba 338.PP
296d592b 339.in +4n
e646a1ba 340.EX
fea681da
MK
341glob_t globbuf;
342
343globbuf.gl_offs = 2;
344glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
345glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
346globbuf.gl_pathv[0] = "ls";
8c383102 347globbuf.gl_pathv[1] = "\-l";
fea681da 348execvp("ls", &globbuf.gl_pathv[0]);
b8302363 349.EE
e646a1ba 350.in
47297adb 351.SH SEE ALSO
fea681da
MK
352.BR ls (1),
353.BR sh (1),
354.BR stat (2),
355.BR exec (3),
2ff9c803 356.BR fnmatch (3),
fea681da
MK
357.BR malloc (3),
358.BR opendir (3),
359.BR readdir (3),
360.BR wordexp (3),
361.BR glob (7)