typo
[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.\"
91cff1e1 9.\" @(#)glob.3 5.2 (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 {
39 int gl_pathc; /* count of paths matching pattern */
40 int gl_offs; /* reserved at beginning of gl_pathv */
41 char **gl_pathv; /* list of paths matching pattern */
42} glob_t;
43.fi
44.RE
45.PP
46The argument
47.I pattern
48is a pointer to a pathname pattern to be expanded.
49.I Glob
50matches all accessible pathnames against the pattern and creates
51a list of the pathnames that match.
52In order to have access to a pathname,
53.I glob
54requires search permission on every component of a path except the last
55and read permission on each directory of any filename component of
56.I pattern
57that contains any of the special characters ``*'', ``?'' or ``[''.
58.PP
59.I Glob
60stores the number of matched pathnames into the
61.I gl_pathc
62field, and a pointer to a list of pointers to pathnames into the
63.I gl_pathv
64field.
65The first pointer after the last pathname is NULL.
66If the pattern does not match any pathnames, the returned number of
67matched paths is set to zero.
68.PP
69It is the caller's responsibility to create the structure pointed to by
70.IR pglob .
71The
72.I glob
73function allocates other space as needed, including the memory pointed
74to by
75.IR gl_pathv .
76.PP
77The argument
78.I flags
79is used to modify the behavior of
80.IR glob .
81The value of
82.I flags
83is the bitwise inclusive OR of any of the following
84values defined in
85.IR glob.h :
86.TP
87GLOB_APPEND
88Append pathnames generated to the ones from a previous call (or calls)
89to
90.IR glob .
91The value of
92.I gl_pathc
93will be the total matches found by this call and the previous call(s).
94The pathnames are appended to, not merged with the pathnames returned by
95the previous call(s).
96Between calls, the caller must not change the setting of the
97GLOB_DOOFFS flag, nor change the value of
98.I gl_offs
99when
100GLOB_DOOFFS is set, nor (obviously) call
101.I globfree
102for
103.I pglob.
104.TP
105GLOB_DOOFFS
106Make use of the
107.I gl_offs
108field.
109If this flag is set,
110.I gl_offs
111is used to specify how many NULL pointers to prepend to the beginning
112of the
113.I gl_pathv
114field.
115In other words,
116.I gl_pathv
117will point to
118.I gl_offs
119NULL pointers,
120followed by
121.I gl_pathc
122pathname pointers, followed by a NULL pointer.
123.TP
124GLOB_ERR
125Causes
126.I glob
127to return when it encounters a directory that it cannot open or read.
128Ordinarily,
129.I glob
130continues to find matches.
131.TP
132GLOB_MARK
133Each pathname that is a directory that matches
134.I pattern
135has a slash
136appended.
137.TP
138GLOB_NOSORT
139By default, the pathnames are sorted in ascending ASCII order;
140this flag prevents that sorting (speeding up
141.IR glob ).
142.TP
143GLOB_NOCHECK
144If
145.I pattern
146does not match any pathname, then
147.I glob
148returns a list
149consisting of only
150.IR pattern ,
151and the number of matched pathnames is set to 1.
152If
153.I GLOB_QUOTE
154is set, its effect is present in the pattern returned.
155.TP
156GLOB_QUOTE
157Use the backslash (``\e'') character for quoting: every occurrence of
158a backslash followed by a character in the pattern is replaced by that
159character, avoiding any special interpretation of the character.
160.PP
161If, during the search, a directory is encountered that cannot be opened
162or read and
163.I errfunc
164is non-NULL,
165.I glob
166calls (*\fIerrfunc\fP)(\fIpath\fP, \fIerrno\fP).
167This may be unintuitive: a pattern like ``*/Makefile'' will try to
168.IR stat (2)
169``foo/Makefile'' even if ``foo'' is not a directory, resulting in a
170call to
171.IR errfunc .
172The error routine can suppress this action by testing for ENOENT and
173ENOTDIR; however, the GLOB_ERR flag will still cause an immediate
174return when this happens.
175.PP
176If
177.I errfunc
178returns non-zero,
179.I glob
180stops the scan and returns
181.I GLOB_ABEND
182after setting
183.I gl_pathc
184and
185.I gl_pathv
186to reflect any paths already matched.
187This also happens if an error is encountered and
188.I GLOB_ERR
189is set in
190.IR flags ,
191regardless of the return value of
192.IR errfunc ,
193if called.
194If
195.I GLOB_ERR
196is not set and either
197.I errfunc
198is NULL or
199.I errfunc
200returns zero, the error is ignored.
201.PP
202The
203.I globfree
204function frees any space associated with
205.I pglob
206from a previous call(s) to
207.IR glob .
208.SH RETURNS
209On successful completion,
210.I glob
211returns zero.
212The field
213.I gl_pathc
214returns the number of matched pathnames and
215the field
216.I gl_pathv
217contains a pointer to a NULL-terminated list of matched pathnames.
218However, if
219.I pglob->gl_pathc
220is zero, the contents of
221.I pglob->gl_pathv
222is undefined.
223.PP
224If
225.I glob
226terminates due to an error, it sets errno and returns one of the
227following non-zero constants, which are defined in the include
228file <glob.h>:
229.TP
230GLOB_NOSPACE
231An attempt to allocate memory failed.
232.TP
233GLOB_ABEND
234The scan was stopped because an error was encountered and either
235GLOB_ERR was set or (*\fIerrfunc\fR)() returned non-zero.
236.PP
237The arguments
238.I pglob->gl_pathc
239and
240.I pglob->gl_pathv
241are still set as specified above.
242.SH STANDARDS
243The
244.I glob
245function is expected to be POSIX 1003.2 compatible with the exception
246that the flag GLOB_QUOTE should not be used by applications striving
247for strict POSIX conformance.
248.SH EXAMPLE
249A rough equivalent of ``ls -l *.c *.h'' can be obtained with the
250following code:
251.sp
252.nf
253.RS
254glob_t g;
255
256g.gl_offs = 2;
257glob("*.c", GLOB_DOOFFS, NULL, &g);
258glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
259g.gl_pathv[0] = "ls";
260g.gl_pathv[1] = "-l";
261execvp("ls", g.gl_pathv);
262.RE
263.fi
264.SH SEE ALSO
265sh(1), fnmatch(3), wordexp(3), regexp(3)
266.SH BUGS
267Patterns longer than MAXPATHLEN may cause unchecked errors.
268.PP
269.I Glob
270may fail and set errno for any of the errors specified for the
271library routines
272.I stat (2),
273.I closedir (3),
274.I opendir (3),
275.I readdir (3),
276.I malloc (3),
277and
278.I free (3).
279