xref: /csrg-svn/lib/libc/gen/glob.3 (revision 50725)
148352Scael.\" Copyright (c) 1989, 1991 The Regents of the University of California.
240086Sbostic.\" All rights reserved.
340086Sbostic.\"
440086Sbostic.\" This code is derived from software contributed to Berkeley by
540086Sbostic.\" Guido van Rossum.
643571Strent.\" %sccs.include.redist.man%
740086Sbostic.\"
8*50725Scael.\"     @(#)glob.3	5.6 (Berkeley) 07/31/91
940086Sbostic.\"
1048352Scael.Dd
1148352Scael.Dt GLOB 3
1248352Scael.Os
1348352Scael.Sh NAME
1448352Scael.Nm glob ,
1548352Scael.Nm globfree
1648352Scael.Nd generate pathnames matching a pattern
1748352Scael.Sh SYNOPSIS
1848352Scael.Fd #include <glob.h>
1948352Scael.Ft int
2048352Scael.Fn glob "const char *pattern" "int flags" "const int (*errfunc)(char *, int)" "glob_t *pglob"
2148352Scael.Ft void
2248352Scael.Fn globfree "glob_t *pglob"
2348352Scael.Sh DESCRIPTION
2448352ScaelThe
2548352Scael.Fn glob
2648352Scaelfunction
2740086Sbosticis a pathname generator that implements the rules for file name pattern
2840086Sbosticmatching used by the shell.
2948352Scael.Pp
3040086SbosticThe include file
3148352Scael.Pa glob.h
3240086Sbosticdefines the structure type
3348352Scael.Fa glob_t ,
3440086Sbosticwhich contains at least the following fields:
35*50725Scael.Bd -literal
3640086Sbostictypedef struct {
3747591Sbostic	int gl_pathc;		/* count of total paths so far */
3847591Sbostic	int gl_matchc;		/* count of paths matching pattern */
3940086Sbostic	int gl_offs;		/* reserved at beginning of gl_pathv */
4047591Sbostic	int gl_flags;		/* returned flags */
4140086Sbostic	char **gl_pathv;	/* list of paths matching pattern */
4240086Sbostic} glob_t;
4348352Scael.Ed
4448352Scael.Pp
4540086SbosticThe argument
4648352Scael.Fa pattern
4740086Sbosticis a pointer to a pathname pattern to be expanded.
4848352ScaelThe
4948352Scael.Fn glob
5048352Scaelargument
5140086Sbosticmatches all accessible pathnames against the pattern and creates
5240086Sbostica list of the pathnames that match.
5340086SbosticIn order to have access to a pathname,
5448352Scael.Fn glob
5540086Sbosticrequires search permission on every component of a path except the last
5640086Sbosticand read permission on each directory of any filename component of
5748352Scael.Fa pattern
5848352Scaelthat contains any of the special characters
5948352Scael.Ql * ,
6048352Scael.Ql ?
6148352Scaelor
6248352Scael.Ql [ .
6348352Scael.Pp
6448352ScaelThe
6548352Scael.Fn glob
6648352Scaelargument
6740086Sbosticstores the number of matched pathnames into the
6848352Scael.Fa gl_pathc
6940086Sbosticfield, and a pointer to a list of pointers to pathnames into the
7048352Scael.Fa gl_pathv
7140086Sbosticfield.
7248352ScaelThe first pointer after the last pathname is
7348352Scael.Dv NULL .
7440086SbosticIf the pattern does not match any pathnames, the returned number of
7540086Sbosticmatched paths is set to zero.
7648352Scael.Pp
7740086SbosticIt is the caller's responsibility to create the structure pointed to by
7848352Scael.Fa pglob .
7940086SbosticThe
8048352Scael.Fn glob
8140086Sbosticfunction allocates other space as needed, including the memory pointed
8240086Sbosticto by
8348352Scael.Fa gl_pathv .
8448352Scael.Pp
8540086SbosticThe argument
8648352Scael.Fa flags
8740086Sbosticis used to modify the behavior of
8848352Scael.Fn glob .
8940086SbosticThe value of
9048352Scael.Fa flags
9148352Scaelis the bitwise inclusive
9248352Scael.Tn OR
9348352Scaelof any of the following
9440086Sbosticvalues defined in
9548352Scael.Pa glob.h :
9648352Scael.Bl -tag -width GLOB_NOCHECK
9748352Scael.It Dv GLOB_APPEND
9840086SbosticAppend pathnames generated to the ones from a previous call (or calls)
9940086Sbosticto
10048352Scael.Fn glob .
10140086SbosticThe value of
10248352Scael.Fa gl_pathc
10340086Sbosticwill be the total matches found by this call and the previous call(s).
10440086SbosticThe pathnames are appended to, not merged with the pathnames returned by
10540086Sbosticthe previous call(s).
10640086SbosticBetween calls, the caller must not change the setting of the
10748352Scael.Dv GLOB_DOOFFS
10848352Scaelflag, nor change the value of
10948352Scael.Fa gl_offs
11040086Sbosticwhen
11148352Scael.Dv GLOB_DOOFFS
11248352Scaelis set, nor (obviously) call
11348352Scael.Fn globfree
11440086Sbosticfor
11548352Scael.Fa pglob .
11648352Scael.It Dv GLOB_DOOFFS
11740086SbosticMake use of the
11848352Scael.Fa gl_offs
11940086Sbosticfield.
12040086SbosticIf this flag is set,
12148352Scael.Fa gl_offs
12248352Scaelis used to specify how many
12348352Scael.Dv NULL
12448352Scaelpointers to prepend to the beginning
12540086Sbosticof the
12648352Scael.Fa gl_pathv
12740086Sbosticfield.
12840086SbosticIn other words,
12948352Scael.Fa gl_pathv
13040086Sbosticwill point to
13148352Scael.Fa gl_offs
13248352Scael.Dv NULL
13348352Scaelpointers,
13440086Sbosticfollowed by
13548352Scael.Fa gl_pathc
13648352Scaelpathname pointers, followed by a
13748352Scael.Dv NULL
13848352Scaelpointer.
13948352Scael.It Dv GLOB_ERR
14040086SbosticCauses
14148352Scael.Fn glob
14240086Sbosticto return when it encounters a directory that it cannot open or read.
14340086SbosticOrdinarily,
14448352Scael.Fn glob
14540086Sbosticcontinues to find matches.
14648352Scael.It Dv GLOB_MARK
14740086SbosticEach pathname that is a directory that matches
14848352Scael.Fa pattern
14940086Sbostichas a slash
15040086Sbosticappended.
15148352Scael.It Dv GLOB_NOCHECK
15240086SbosticIf
15348352Scael.Fa pattern
15440086Sbosticdoes not match any pathname, then
15548352Scael.Fn glob
15640086Sbosticreturns a list
15740086Sbosticconsisting of only
15848352Scael.Fa pattern ,
15947591Sbosticwith the number of total pathnames is set to 1, and the number of matched
16047591Sbosticpathnames set to 0.
16140086SbosticIf
16248352Scael.Dv GLOB_QUOTE
16340086Sbosticis set, its effect is present in the pattern returned.
16450420Sbostic.It Dv GLOB_NOMAGIC
16550420SbosticIs the same as
16650420Sbostic.Dv GLOB_NOCHECK
16750420Sbosticbut it only appends the
16850420Sbostic.Fa pattern
16950420Sbosticif it does not contain any of the special characters ``*'', ``?'' or ``[''.
17050420Sbostic.Dv GLOB_NOMAGIC
17150420Sbosticis provided to simplify implementing the historic
17250420Sbostic.Xr csh 1
17350420Sbosticglobbing behavior and should probably not be used anywhere else.
17450420Sbostic.It Dv GLOB_NOSORT
17550420SbosticBy default, the pathnames are sorted in ascending
17650420Sbostic.Tn ASCII
17750420Sbosticorder;
17850420Sbosticthis flag prevents that sorting (speeding up
17950420Sbostic.Fn glob ) .
18048352Scael.It Dv GLOB_QUOTE
18148352ScaelUse the backslash
18248352Scael.Pq Ql \e
18348352Scaelcharacter for quoting: every occurrence of
18440086Sbostica backslash followed by a character in the pattern is replaced by that
18540086Sbosticcharacter, avoiding any special interpretation of the character.
18648352Scael.El
18748352Scael.Pp
18840086SbosticIf, during the search, a directory is encountered that cannot be opened
18940086Sbosticor read and
19048352Scael.Fa errfunc
19148352Scaelis
19248352Scael.Pf non- Dv NULL ,
19348352Scael.Fn glob
19448352Scaelcalls
19548352Scael.Fa (*errfunc)(path,errno) .
19648352ScaelThis may be unintuitive: a pattern like
19748352Scael.Ql */Makefile
19848352Scaelwill try to
19948352Scael.Xr stat 2
20048352Scael.Ql foo/Makefile
20148352Scaeleven if
20248352Scael.Ql foo
20348352Scaelis not a directory, resulting in a
20440086Sbosticcall to
20548352Scael.Fa errfunc .
20648352ScaelThe error routine can suppress this action by testing for
20748352Scael.Dv ENOENT
20848352Scaeland
20948352Scael.Dv ENOTDIR ;
21048352Scaelhowever, the
21148352Scael.Dv GLOB_ERR
21248352Scaelflag will still cause an immediate
21340086Sbosticreturn when this happens.
21448352Scael.Pp
21540086SbosticIf
21648352Scael.Fa errfunc
21740086Sbosticreturns non-zero,
21848352Scael.Fn glob
21940086Sbosticstops the scan and returns
22048352Scael.Dv GLOB_ABEND
22140086Sbosticafter setting
22248352Scael.Fa gl_pathc
22340086Sbosticand
22448352Scael.Fa gl_pathv
22540086Sbosticto reflect any paths already matched.
22640086SbosticThis also happens if an error is encountered and
22748352Scael.Dv GLOB_ERR
22840086Sbosticis set in
22948352Scael.Fa flags ,
23040086Sbosticregardless of the return value of
23148352Scael.Fa errfunc ,
23240086Sbosticif called.
23340086SbosticIf
23448352Scael.Dv GLOB_ERR
23540086Sbosticis not set and either
23648352Scael.Fa errfunc
23748352Scaelis
23848352Scael.Dv NULL
23948352Scaelor
24048352Scael.Fa errfunc
24140086Sbosticreturns zero, the error is ignored.
24248352Scael.Pp
24340086SbosticThe
24448352Scael.Fn globfree
24540086Sbosticfunction frees any space associated with
24648352Scael.Fa pglob
24740086Sbosticfrom a previous call(s) to
24848352Scael.Fn glob .
24948352Scael.Sh RETURN VALUES
25040086SbosticOn successful completion,
25148352Scael.Fn glob
25240086Sbosticreturns zero.
25347591SbosticIn addition the fields of
25448352Scael.Fa pglob
25547591Sbosticcontain the values described below:
25648352Scael.Bl -tag -width GLOB_NOCHECK
25748352Scael.It Fa gl_pathc
25847591Sbosticcontains the total number of matched pathnames so far.
25947591SbosticThis includes other matches from previous invocations of
26048352Scael.Fn glob
26147591Sbosticif
26248352Scael.Dv GLOB_APPEND
26347591Sbosticwas specified.
26448352Scael.It Fa gl_matchc
26547591Sbosticcontains the number of matched pathnames in the current invocation of
26648352Scael.Fn glob .
26748352Scael.It Fa gl_flags
26847591Sbosticcontains a copy of the
26948352Scael.Fa flags
27048352Scaelparameter with the bit
27148352Scael.Dv GLOB_MAGCHAR
27248352Scaelset if
27348352Scael.Fa pattern
27447591Sbosticcontained any of the special characters ``*'', ``?'' or ``['', cleared
27547591Sbosticif not.
27648352Scael.It Fa gl_pathv
27748352Scaelcontains a pointer to a
27848352Scael.Dv NULL Ns -terminated
27948352Scaellist of matched pathnames.
28040086SbosticHowever, if
28148352Scael.Fa gl_pathc
28240086Sbosticis zero, the contents of
28348352Scael.Fa gl_pathv
28447591Sbosticare undefined.
28548352Scael.El
28648352Scael.Pp
28740086SbosticIf
28848352Scael.Fn glob
28940086Sbosticterminates due to an error, it sets errno and returns one of the
29040086Sbosticfollowing non-zero constants, which are defined in the include
29148352Scaelfile
29248352Scael.Aq Pa glob.h :
29348352Scael.Bl -tag -width GLOB_NOCHECK
29448352Scael.It Dv GLOB_NOSPACE
29540086SbosticAn attempt to allocate memory failed.
29648352Scael.It Dv GLOB_ABEND
29740086SbosticThe scan was stopped because an error was encountered and either
29848352Scael.Dv GLOB_ERR
29948352Scaelwas set or
30048352Scael.Fa (*errfunc)()
30148352Scaelreturned non-zero.
30248352Scael.El
30348352Scael.Pp
30440086SbosticThe arguments
30548352Scael.Fa pglob\->gl_pathc
30640086Sbosticand
30748352Scael.Fa pglob\->gl_pathv
30840086Sbosticare still set as specified above.
30948352Scael.Sh SEE ALSO
31048352Scael.Xr sh 1 ,
31148352Scael.Xr fnmatch 3 ,
31248352Scael.Xr wordexp 3 ,
31348352Scael.Xr regexp 3
31448352Scael.Sh STANDARDS
31540086SbosticThe
31648352Scael.Fn glob
31748352Scaelfunction is expected to be
31848352Scael.St -p1003.2
31948352Scaelcompatible with the exception
32047591Sbosticthat the flag
32148352Scael.Dv GLOB_QUOTE
32247591Sbosticand the fields
32348352Scael.Fa gl_matchc
32447591Sbosticand
32548352Scael.Fa gl_flags
32648352Scaelshould not be used by applications striving for strict
32748352Scael.Tn POSIX
32848352Scaelconformance.
32948352Scael.Sh EXAMPLE
33048352ScaelA rough equivalent of
33148352Scael.Ql "ls -l *.c *.h"
33248352Scaelcan be obtained with the
33340086Sbosticfollowing code:
33448352Scael.Bd -literal -offset indent
33548352ScaelGLOB_t g;
33640086Sbostic
33740086Sbosticg.gl_offs = 2;
33840086Sbosticglob("*.c", GLOB_DOOFFS, NULL, &g);
33940086Sbosticglob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
34040086Sbosticg.gl_pathv[0] = "ls";
34140086Sbosticg.gl_pathv[1] = "-l";
34240086Sbosticexecvp("ls", g.gl_pathv);
34348352Scael.Ed
34448352Scael.Sh HISTORY
34548352ScaelThe
34648352Scael.Fn glob
34748352Scaeland
34848352Scael.Fn globfree
34948352Scaelfunctions are
35048352Scael.Ud .
35148352Scael.Sh BUGS
35248352ScaelPatterns longer than
35348352Scael.Dv MAXPATHLEN
35448352Scaelmay cause unchecked errors.
35548352Scael.Pp
35648352ScaelThe
35748352Scael.Fn glob
35848352Scaelargument
35940086Sbosticmay fail and set errno for any of the errors specified for the
36040086Sbosticlibrary routines
36148352Scael.Xr stat 2 ,
36248352Scael.Xr closedir 3 ,
36348352Scael.Xr opendir 3 ,
36448352Scael.Xr readdir 3 ,
36548352Scael.Xr malloc 3 ,
36640086Sbosticand
36748352Scael.Xr free 3 .
368