use isinf(3) and isnan(3) instead of rolling our own
[unix-history] / usr / src / lib / libc / gen / glob.3
CommitLineData
b4ee7c09
KB
1.\" Copyright (c) 1989 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Guido van Rossum.
6.\"
91cff1e1 7.\" %sccs.include.redist.man%
b4ee7c09 8.\"
f6ec3c7f 9.\" @(#)glob.3 5.3 (Berkeley) %G%
b4ee7c09
KB
10.\"
11.TH GLOB 3 ""
12.UC 7
13.SH NAME
14glob, globfree \- generate pathnames matching a pattern
15.SH SYNOPSIS
16.nf
17#include <glob.h>
18
19glob(const char *pattern, int flags,
20 const int (*errfunc)(char *, int), glob_t *pglob);
21
22void globfree(glob_t *pglob);
23.fi
24.SH DESCRIPTION
25.I Glob
26is a pathname generator that implements the rules for file name pattern
27matching used by the shell.
28.PP
29The include file
30.I glob.h
31defines the structure type
32.IR glob_t ,
33which contains at least the following fields:
34.sp
35.RS
36.nf
37.ta .5i +\w'char **gl_pathv;\0\0\0'u
38typedef struct {
f6ec3c7f
KB
39 int gl_pathc; /* count of total paths so far */
40 int gl_matchc; /* count of paths matching pattern */
b4ee7c09 41 int gl_offs; /* reserved at beginning of gl_pathv */
f6ec3c7f 42 int gl_flags; /* returned flags */
b4ee7c09
KB
43 char **gl_pathv; /* list of paths matching pattern */
44} glob_t;
45.fi
46.RE
47.PP
48The argument
49.I pattern
50is a pointer to a pathname pattern to be expanded.
51.I Glob
52matches all accessible pathnames against the pattern and creates
53a list of the pathnames that match.
54In order to have access to a pathname,
55.I glob
56requires search permission on every component of a path except the last
57and read permission on each directory of any filename component of
58.I pattern
59that contains any of the special characters ``*'', ``?'' or ``[''.
60.PP
61.I Glob
62stores the number of matched pathnames into the
63.I gl_pathc
64field, and a pointer to a list of pointers to pathnames into the
65.I gl_pathv
66field.
67The first pointer after the last pathname is NULL.
68If the pattern does not match any pathnames, the returned number of
69matched paths is set to zero.
70.PP
71It is the caller's responsibility to create the structure pointed to by
72.IR pglob .
73The
74.I glob
75function allocates other space as needed, including the memory pointed
76to by
77.IR gl_pathv .
78.PP
79The argument
80.I flags
81is used to modify the behavior of
82.IR glob .
83The value of
84.I flags
85is the bitwise inclusive OR of any of the following
86values defined in
87.IR glob.h :
88.TP
89GLOB_APPEND
90Append pathnames generated to the ones from a previous call (or calls)
91to
92.IR glob .
93The value of
94.I gl_pathc
95will be the total matches found by this call and the previous call(s).
96The pathnames are appended to, not merged with the pathnames returned by
97the previous call(s).
98Between calls, the caller must not change the setting of the
99GLOB_DOOFFS flag, nor change the value of
100.I gl_offs
101when
102GLOB_DOOFFS is set, nor (obviously) call
103.I globfree
104for
105.I pglob.
106.TP
107GLOB_DOOFFS
108Make use of the
109.I gl_offs
110field.
111If this flag is set,
112.I gl_offs
113is used to specify how many NULL pointers to prepend to the beginning
114of the
115.I gl_pathv
116field.
117In other words,
118.I gl_pathv
119will point to
120.I gl_offs
121NULL pointers,
122followed by
123.I gl_pathc
124pathname pointers, followed by a NULL pointer.
125.TP
126GLOB_ERR
127Causes
128.I glob
129to return when it encounters a directory that it cannot open or read.
130Ordinarily,
131.I glob
132continues to find matches.
133.TP
134GLOB_MARK
135Each pathname that is a directory that matches
136.I pattern
137has a slash
138appended.
139.TP
140GLOB_NOSORT
141By default, the pathnames are sorted in ascending ASCII order;
142this flag prevents that sorting (speeding up
143.IR glob ).
144.TP
145GLOB_NOCHECK
146If
147.I pattern
148does not match any pathname, then
149.I glob
150returns a list
151consisting of only
152.IR pattern ,
f6ec3c7f
KB
153with the number of total pathnames is set to 1, and the number of matched
154pathnames set to 0.
b4ee7c09
KB
155If
156.I GLOB_QUOTE
157is set, its effect is present in the pattern returned.
158.TP
159GLOB_QUOTE
160Use the backslash (``\e'') character for quoting: every occurrence of
161a backslash followed by a character in the pattern is replaced by that
162character, avoiding any special interpretation of the character.
163.PP
164If, during the search, a directory is encountered that cannot be opened
165or read and
166.I errfunc
167is non-NULL,
168.I glob
169calls (*\fIerrfunc\fP)(\fIpath\fP, \fIerrno\fP).
170This may be unintuitive: a pattern like ``*/Makefile'' will try to
171.IR stat (2)
172``foo/Makefile'' even if ``foo'' is not a directory, resulting in a
173call to
174.IR errfunc .
175The error routine can suppress this action by testing for ENOENT and
176ENOTDIR; however, the GLOB_ERR flag will still cause an immediate
177return when this happens.
178.PP
179If
180.I errfunc
181returns non-zero,
182.I glob
183stops the scan and returns
184.I GLOB_ABEND
185after setting
186.I gl_pathc
187and
188.I gl_pathv
189to reflect any paths already matched.
190This also happens if an error is encountered and
191.I GLOB_ERR
192is set in
193.IR flags ,
194regardless of the return value of
195.IR errfunc ,
196if called.
197If
198.I GLOB_ERR
199is not set and either
200.I errfunc
201is NULL or
202.I errfunc
203returns zero, the error is ignored.
204.PP
205The
206.I globfree
207function frees any space associated with
208.I pglob
209from a previous call(s) to
210.IR glob .
211.SH RETURNS
212On successful completion,
213.I glob
214returns zero.
f6ec3c7f
KB
215In addition the fields of
216.I pglob
217contain the values described below:
218.TP
b4ee7c09 219.I gl_pathc
f6ec3c7f
KB
220contains the total number of matched pathnames so far.
221This includes other matches from previous invocations of
222.I glob
223if
224.I GLOB_APPEND
225was specified.
226.TP
227.I gl_matchc
228contains the number of matched pathnames in the current invocation of
229.I glob.
230.TP
231.I gl_flags
232contains a copy of the
233.I flags
234parameter with the bit GLOB_MAGCHAR set if
235.I pattern
236contained any of the special characters ``*'', ``?'' or ``['', cleared
237if not.
238.TP
b4ee7c09
KB
239.I gl_pathv
240contains a pointer to a NULL-terminated list of matched pathnames.
241However, if
f6ec3c7f 242.I gl_pathc
b4ee7c09 243is zero, the contents of
f6ec3c7f
KB
244.I gl_pathv
245are undefined.
b4ee7c09
KB
246.PP
247If
248.I glob
249terminates due to an error, it sets errno and returns one of the
250following non-zero constants, which are defined in the include
251file <glob.h>:
252.TP
253GLOB_NOSPACE
254An attempt to allocate memory failed.
255.TP
256GLOB_ABEND
257The scan was stopped because an error was encountered and either
258GLOB_ERR was set or (*\fIerrfunc\fR)() returned non-zero.
259.PP
260The arguments
261.I pglob->gl_pathc
262and
263.I pglob->gl_pathv
264are still set as specified above.
265.SH STANDARDS
266The
267.I glob
268function is expected to be POSIX 1003.2 compatible with the exception
f6ec3c7f
KB
269that the flag
270.I GLOB_QUOTE
271and the fields
272.I gl_matchc
273and
274.I gl_flags
275should not be used by applications striving for strict POSIX conformance.
b4ee7c09
KB
276.SH EXAMPLE
277A rough equivalent of ``ls -l *.c *.h'' can be obtained with the
278following code:
279.sp
280.nf
281.RS
282glob_t g;
283
284g.gl_offs = 2;
285glob("*.c", GLOB_DOOFFS, NULL, &g);
286glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
287g.gl_pathv[0] = "ls";
288g.gl_pathv[1] = "-l";
289execvp("ls", g.gl_pathv);
290.RE
291.fi
292.SH SEE ALSO
293sh(1), fnmatch(3), wordexp(3), regexp(3)
294.SH BUGS
295Patterns longer than MAXPATHLEN may cause unchecked errors.
296.PP
297.I Glob
298may fail and set errno for any of the errors specified for the
299library routines
300.I stat (2),
301.I closedir (3),
302.I opendir (3),
303.I readdir (3),
304.I malloc (3),
305and
306.I free (3).
307