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*57897Sbostic.\" @(#)glob.3 5.8 (Berkeley) 02/10/93 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 20*57897Sbostic.Fn glob "const char *pattern" "int flags" "const int (*errfunc)(const 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: 3550725Scael.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 : 96*57897Sbostic.Bl -tag -width GLOB_ALTDIRFUNC 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. 164*57897Sbostic.It Dv GLOB_NOSORT 165*57897SbosticBy default, the pathnames are sorted in ascending 166*57897Sbostic.Tn ASCII 167*57897Sbosticorder; 168*57897Sbosticthis flag prevents that sorting (speeding up 169*57897Sbostic.Fn glob ) . 170*57897Sbostic.El 171*57897Sbostic.Pp 172*57897SbosticThe following values may also be included in 173*57897Sbostic.Fa flags , 174*57897Sbostichowever, they are non-standard extensions to 175*57897Sbostic.St -p1003.2 . 176*57897Sbostic.Bl -tag -width GLOB_ALTDIRFUNC 177*57897Sbostic.It Dv GLOB_ALTDIRFUNC 178*57897SbosticThe following additional fields in the pglob structure have been 179*57897Sbosticinitialized with alternate functions for glob to use to open, read, 180*57897Sbosticand close directories and to get stat information on names found 181*57897Sbosticin those directories. 182*57897Sbostic.Bd -literal 183*57897Sbostic void *(*gl_opendir)(const char * name); 184*57897Sbostic struct dirent *(*gl_readdir)(void *); 185*57897Sbostic void (*gl_closedir)(void *); 186*57897Sbostic int (*gl_lstat)(const char *name, struct stat *st); 187*57897Sbostic int (*gl_stat)(const char *name, struct stat *st); 188*57897Sbostic.Ed 189*57897Sbostic.Pp 190*57897SbosticThis extension is provided to allow programs such as 191*57897Sbostic.Xr restore 8 192*57897Sbosticto provide globbing from directories stored on tape. 193*57897Sbostic.It Dv GLOB_BRACE 194*57897SbosticPre-process the pattern string to expand 195*57897Sbostic.Ql {pat,pat,...} 196*57897Sbosticstrings like 197*57897Sbostic.Xr csh 1. The pattern 198*57897Sbostic.Ql {} 199*57897Sbosticis left unexpanded for historical reasons 200*57897Sbostic.Xr (Csh 1 201*57897Sbosticdoes the same thing to 202*57897Sbosticease typing 203*57897Sbosticof 204*57897Sbostic.Xr find 1 205*57897Sbosticpatterns). 206*57897Sbostic.It Dv GLOB_MAGCHAR 207*57897SbosticSet by the 208*57897Sbostic.Fn glob 209*57897Sbosticfunction if the pattern included globbing characters. 210*57897SbosticSee the description of the usage of the 211*57897Sbostic.Fa gl_matchc 212*57897Sbosticstructure member for more details. 21350420Sbostic.It Dv GLOB_NOMAGIC 21450420SbosticIs the same as 21550420Sbostic.Dv GLOB_NOCHECK 21650420Sbosticbut it only appends the 21750420Sbostic.Fa pattern 21850420Sbosticif it does not contain any of the special characters ``*'', ``?'' or ``[''. 21950420Sbostic.Dv GLOB_NOMAGIC 22050420Sbosticis provided to simplify implementing the historic 22150420Sbostic.Xr csh 1 22250420Sbosticglobbing behavior and should probably not be used anywhere else. 22348352Scael.It Dv GLOB_QUOTE 22448352ScaelUse the backslash 22548352Scael.Pq Ql \e 22648352Scaelcharacter for quoting: every occurrence of 22740086Sbostica backslash followed by a character in the pattern is replaced by that 22840086Sbosticcharacter, avoiding any special interpretation of the character. 229*57897Sbostic.It Dv GLOB_TILDE 230*57897SbosticExpand patterns that start with 231*57897Sbostic.Ql ~ 232*57897Sbosticto user name home directories. 23348352Scael.El 23448352Scael.Pp 23540086SbosticIf, during the search, a directory is encountered that cannot be opened 23640086Sbosticor read and 23748352Scael.Fa errfunc 23848352Scaelis 23948352Scael.Pf non- Dv NULL , 24048352Scael.Fn glob 24148352Scaelcalls 242*57897Sbostic.Fa (*errfunc)(path, errno) . 24348352ScaelThis may be unintuitive: a pattern like 24448352Scael.Ql */Makefile 24548352Scaelwill try to 24648352Scael.Xr stat 2 24748352Scael.Ql foo/Makefile 24848352Scaeleven if 24948352Scael.Ql foo 25048352Scaelis not a directory, resulting in a 25140086Sbosticcall to 25248352Scael.Fa errfunc . 25348352ScaelThe error routine can suppress this action by testing for 25448352Scael.Dv ENOENT 25548352Scaeland 25648352Scael.Dv ENOTDIR ; 25748352Scaelhowever, the 25848352Scael.Dv GLOB_ERR 25948352Scaelflag will still cause an immediate 26040086Sbosticreturn when this happens. 26148352Scael.Pp 26240086SbosticIf 26348352Scael.Fa errfunc 26440086Sbosticreturns non-zero, 26548352Scael.Fn glob 26640086Sbosticstops the scan and returns 26748352Scael.Dv GLOB_ABEND 26840086Sbosticafter setting 26948352Scael.Fa gl_pathc 27040086Sbosticand 27148352Scael.Fa gl_pathv 27240086Sbosticto reflect any paths already matched. 27340086SbosticThis also happens if an error is encountered and 27448352Scael.Dv GLOB_ERR 27540086Sbosticis set in 27648352Scael.Fa flags , 27740086Sbosticregardless of the return value of 27848352Scael.Fa errfunc , 27940086Sbosticif called. 28040086SbosticIf 28148352Scael.Dv GLOB_ERR 28240086Sbosticis not set and either 28348352Scael.Fa errfunc 28448352Scaelis 28548352Scael.Dv NULL 28648352Scaelor 28748352Scael.Fa errfunc 28840086Sbosticreturns zero, the error is ignored. 28948352Scael.Pp 29040086SbosticThe 29148352Scael.Fn globfree 29240086Sbosticfunction frees any space associated with 29348352Scael.Fa pglob 29440086Sbosticfrom a previous call(s) to 29548352Scael.Fn glob . 29648352Scael.Sh RETURN VALUES 29740086SbosticOn successful completion, 29848352Scael.Fn glob 29940086Sbosticreturns zero. 30047591SbosticIn addition the fields of 30148352Scael.Fa pglob 30247591Sbosticcontain the values described below: 30348352Scael.Bl -tag -width GLOB_NOCHECK 30448352Scael.It Fa gl_pathc 30547591Sbosticcontains the total number of matched pathnames so far. 30647591SbosticThis includes other matches from previous invocations of 30748352Scael.Fn glob 30847591Sbosticif 30948352Scael.Dv GLOB_APPEND 31047591Sbosticwas specified. 31148352Scael.It Fa gl_matchc 31247591Sbosticcontains the number of matched pathnames in the current invocation of 31348352Scael.Fn glob . 31448352Scael.It Fa gl_flags 31547591Sbosticcontains a copy of the 31648352Scael.Fa flags 31748352Scaelparameter with the bit 31848352Scael.Dv GLOB_MAGCHAR 31948352Scaelset if 32048352Scael.Fa pattern 32147591Sbosticcontained any of the special characters ``*'', ``?'' or ``['', cleared 32247591Sbosticif not. 32348352Scael.It Fa gl_pathv 32448352Scaelcontains a pointer to a 32548352Scael.Dv NULL Ns -terminated 32648352Scaellist of matched pathnames. 32740086SbosticHowever, if 32848352Scael.Fa gl_pathc 32940086Sbosticis zero, the contents of 33048352Scael.Fa gl_pathv 33147591Sbosticare undefined. 33248352Scael.El 33348352Scael.Pp 33440086SbosticIf 33548352Scael.Fn glob 33640086Sbosticterminates due to an error, it sets errno and returns one of the 33740086Sbosticfollowing non-zero constants, which are defined in the include 33848352Scaelfile 33948352Scael.Aq Pa glob.h : 34048352Scael.Bl -tag -width GLOB_NOCHECK 34148352Scael.It Dv GLOB_NOSPACE 34240086SbosticAn attempt to allocate memory failed. 34348352Scael.It Dv GLOB_ABEND 34440086SbosticThe scan was stopped because an error was encountered and either 34548352Scael.Dv GLOB_ERR 34648352Scaelwas set or 34748352Scael.Fa (*errfunc)() 34848352Scaelreturned non-zero. 34948352Scael.El 35048352Scael.Pp 35140086SbosticThe arguments 35248352Scael.Fa pglob\->gl_pathc 35340086Sbosticand 35448352Scael.Fa pglob\->gl_pathv 35540086Sbosticare still set as specified above. 35648352Scael.Sh SEE ALSO 35748352Scael.Xr sh 1 , 35848352Scael.Xr fnmatch 3 , 35948352Scael.Xr wordexp 3 , 36048352Scael.Xr regexp 3 36148352Scael.Sh STANDARDS 36240086SbosticThe 36348352Scael.Fn glob 36448352Scaelfunction is expected to be 36548352Scael.St -p1003.2 36648352Scaelcompatible with the exception 36756944Smckusickthat the flags 368*57897Sbostic.Dv GLOB_ALTDIRFUNC, 369*57897Sbostic.Dv GLOB_BRACE 370*57897Sbostic.Dv GLOB_MAGCHAR, 371*57897Sbostic.Dv GLOB_NOMAGIC, 372*57897Sbostic.Dv GLOB_QUOTE, 37356944Smckusickand 374*57897Sbostic.Dv GLOB_TILDE, 37547591Sbosticand the fields 37648352Scael.Fa gl_matchc 37747591Sbosticand 37848352Scael.Fa gl_flags 37948352Scaelshould not be used by applications striving for strict 38048352Scael.Tn POSIX 38148352Scaelconformance. 38248352Scael.Sh EXAMPLE 38348352ScaelA rough equivalent of 38448352Scael.Ql "ls -l *.c *.h" 38548352Scaelcan be obtained with the 38640086Sbosticfollowing code: 38748352Scael.Bd -literal -offset indent 38848352ScaelGLOB_t g; 38940086Sbostic 39040086Sbosticg.gl_offs = 2; 39140086Sbosticglob("*.c", GLOB_DOOFFS, NULL, &g); 39240086Sbosticglob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g); 39340086Sbosticg.gl_pathv[0] = "ls"; 39440086Sbosticg.gl_pathv[1] = "-l"; 39540086Sbosticexecvp("ls", g.gl_pathv); 39648352Scael.Ed 39748352Scael.Sh HISTORY 39848352ScaelThe 39948352Scael.Fn glob 40048352Scaeland 40148352Scael.Fn globfree 40248352Scaelfunctions are 40348352Scael.Ud . 40448352Scael.Sh BUGS 40548352ScaelPatterns longer than 40648352Scael.Dv MAXPATHLEN 40748352Scaelmay cause unchecked errors. 40848352Scael.Pp 40948352ScaelThe 41048352Scael.Fn glob 41148352Scaelargument 41240086Sbosticmay fail and set errno for any of the errors specified for the 41340086Sbosticlibrary routines 41448352Scael.Xr stat 2 , 41548352Scael.Xr closedir 3 , 41648352Scael.Xr opendir 3 , 41748352Scael.Xr readdir 3 , 41848352Scael.Xr malloc 3 , 41940086Sbosticand 42048352Scael.Xr free 3 . 421