source: trunk/third/tcsh/glob.3 @ 9006

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