]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/glob.3
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[thirdparty/man-pages.git] / man3 / glob.3
CommitLineData
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
30glob, 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
44The
63aa9df0 45.BR glob ()
fea681da
MK
46function searches for all the pathnames matching
47.I pattern
48according to the rules used by the shell (see
49.BR glob (7)).
50No tilde expansion or parameter substitution is done; if you want
51these, use
52.BR wordexp (3).
53.PP
54The
63aa9df0 55.BR globfree ()
fea681da
MK
56function frees the dynamically allocated storage from an earlier call
57to
63aa9df0 58.BR glob ().
fea681da
MK
59.PP
60The results of a
63aa9df0 61.BR glob ()
fea681da
MK
62call are stored in the structure pointed to by
63.IR pglob ,
64which is a
65.B glob_t
66which is declared in
bd12ab88 67.I <glob.h>
fea681da
MK
68and includes the following elements defined by POSIX.2 (more may be
69present as an extension):
70.PP
71.br
72.nf
73.in 10
b9f02710
MK
74typedef 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
81Results are stored in dynamically allocated storage.
82.PP
83The parameter
84.I flags
85is made up of bitwise OR of zero or more the following symbolic
86constants, which modify the of behaviour of
63aa9df0 87.BR glob ():
fea681da
MK
88.TP
89.B GLOB_ERR
90which means to return upon read error (because a directory does not
91have read permission, for example),
92.TP
93.B GLOB_MARK
94which means to append a slash to each path which corresponds to a directory,
95.TP
96.B GLOB_NOSORT
97which means don't sort the returned pathnames (they are by default),
98.TP
99.B GLOB_DOOFFS
100which means that
101.I pglob->gl_offs
102slots will be reserved at the beginning of the list of strings in
103.IR pglob->pathv ,
104.TP
105.B GLOB_NOCHECK
106which means that, if no pattern matches, to return the original pattern,
107.TP
108.B GLOB_APPEND
c13182ef
MK
109which means to append to the results of a previous call.
110Do not set this flag on the first invocation of
63aa9df0 111.BR glob ().
fea681da
MK
112.TP
113.B GLOB_NOESCAPE
114which means that meta characters cannot be quoted by backslashes.
115.PP
116The flags may also include some of the following, which are GNU
117extensions and not defined by POSIX.2:
118.TP
119.B GLOB_PERIOD
120which means that a leading period can be matched by meta characters,
121.TP
122.B GLOB_ALTDIRFUNC
123which 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
129are used for file system access instead of the normal library
130functions,
131.TP
132.B GLOB_BRACE
133which means that
134.BR csh (1)
b9560046 135style brace expressions \fB{a,b}\fR are expanded,
fea681da
MK
136.TP
137.B GLOB_NOMAGIC
138which means that the pattern is returned if it contains no metacharacters,
139.TP
140.B GLOB_TILDE
141which means that tilde expansion is carried out, and
142.TP
143.B GLOB_ONLYDIR
144which means that only directories are matched.
145.PP
146If
147.I errfunc
8478ee02 148is not NULL,
fea681da
MK
149it will be called in case of an error with the arguments
150.IR epath ,
151a pointer to the path which failed, and
152.IR eerrno ,
153the value of
154.I errno
155as returned from one of the calls to
c13182ef
MK
156.BR opendir (),
157.BR readdir (),
158or
4d52e8f8 159.BR stat ().
c13182ef 160If
fea681da
MK
161.I errfunc
162returns non-zero, or if
163.B GLOB_ERR
c13182ef 164is set,
63aa9df0 165.BR glob ()
fea681da
MK
166will terminate after the call to
167.IR errfunc .
168.PP
c13182ef 169Upon successful return,
fea681da
MK
170.I pglob->gl_pathc
171contains the number of matched pathnames and
172.I pglob->gl_pathv
c13182ef
MK
173a pointer to the list of matched pathnames.
174The first pointer after the last pathname is NULL.
fea681da
MK
175.PP
176It is possible to call
63aa9df0 177.BR glob ()
c13182ef
MK
178several times.
179In that case, the
fea681da
MK
180.B GLOB_APPEND
181flag has to be set in
182.I flags
183on the second and later invocations.
184.PP
185As a GNU extension,
186.I pglob->gl_flags
187is set to the flags specified, \fBor\fRed with
188.B GLOB_MAGCHAR
189if any metacharacters were found.
190.SH "RETURN VALUE"
c13182ef 191On successful completion,
63aa9df0 192.BR glob ()
fea681da
MK
193returns zero.
194Other possible returns are:
195.TP
196.B GLOB_NOSPACE
197for running out of memory,
198.TP
199.B GLOB_ABORTED
200for a read error, and
201.TP
202.B GLOB_NOMATCH
203for no found matches.
9b336505 204.SH EXAMPLE
fea681da 205One example of use is the following code, which simulates typing
defcceb3 206.sp
c65433e6 207.B ls \-l *.c ../*.c
defcceb3
MK
208.sp
209in the shell:
fea681da
MK
210.nf
211.in 10
212
213glob_t globbuf;
214
215globbuf.gl_offs = 2;
216glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
217glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
218globbuf.gl_pathv[0] = "ls";
8c383102 219globbuf.gl_pathv[1] = "\-l";
fea681da
MK
220execvp("ls", &globbuf.gl_pathv[0]);
221.fi
222.SH "CONFORMING TO"
68e1685c 223POSIX.2, POSIX.1-2001.
fea681da
MK
224.SH BUGS
225The
63aa9df0 226.BR glob ()
fea681da 227function may fail due to failure of underlying function calls, such as
c13182ef 228.BR malloc ()
e1d6264d
MK
229or
230.BR opendir ().
fea681da
MK
231These will store their error code in
232.IR errno .
233.SH NOTES
234The structure elements
235.I gl_pathc
236and
237.I gl_offs
238are declared as
239.BR size_t
240in glibc 2.1, as they should according to POSIX.2,
241but are declared as
9ff08aad 242.I int
fea681da
MK
243in 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)