1*66826Sbostic.\" Copyright (c) 1989, 1991, 1993, 1994 262861Selan.\" The Regents of the University of California. 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*66826Sbostic.\" @(#)glob.3 8.3 (Berkeley) 04/16/94 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 2057897Sbostic.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 : 9657897Sbostic.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. 16457897Sbostic.It Dv GLOB_NOSORT 16557897SbosticBy default, the pathnames are sorted in ascending 16657897Sbostic.Tn ASCII 16757897Sbosticorder; 16857897Sbosticthis flag prevents that sorting (speeding up 16957897Sbostic.Fn glob ) . 17057897Sbostic.El 17157897Sbostic.Pp 17257897SbosticThe following values may also be included in 17357897Sbostic.Fa flags , 17457897Sbostichowever, they are non-standard extensions to 17557897Sbostic.St -p1003.2 . 17657897Sbostic.Bl -tag -width GLOB_ALTDIRFUNC 17757897Sbostic.It Dv GLOB_ALTDIRFUNC 17857897SbosticThe following additional fields in the pglob structure have been 17957897Sbosticinitialized with alternate functions for glob to use to open, read, 18057897Sbosticand close directories and to get stat information on names found 18157897Sbosticin those directories. 18257897Sbostic.Bd -literal 183*66826Sbosticvoid *(*gl_opendir)(const char * name); 184*66826Sbosticstruct dirent *(*gl_readdir)(void *); 185*66826Sbosticvoid (*gl_closedir)(void *); 186*66826Sbosticint (*gl_lstat)(const char *name, struct stat *st); 187*66826Sbosticint (*gl_stat)(const char *name, struct stat *st); 18857897Sbostic.Ed 18957897Sbostic.Pp 19057897SbosticThis extension is provided to allow programs such as 19157897Sbostic.Xr restore 8 19257897Sbosticto provide globbing from directories stored on tape. 19357897Sbostic.It Dv GLOB_BRACE 19457897SbosticPre-process the pattern string to expand 19557897Sbostic.Ql {pat,pat,...} 19657897Sbosticstrings like 19757897Sbostic.Xr csh 1. The pattern 19857897Sbostic.Ql {} 19957897Sbosticis left unexpanded for historical reasons 20057897Sbostic.Xr (Csh 1 20157897Sbosticdoes the same thing to 20257897Sbosticease typing 20357897Sbosticof 20457897Sbostic.Xr find 1 20557897Sbosticpatterns). 20657897Sbostic.It Dv GLOB_MAGCHAR 20757897SbosticSet by the 20857897Sbostic.Fn glob 20957897Sbosticfunction if the pattern included globbing characters. 21057897SbosticSee the description of the usage of the 21157897Sbostic.Fa gl_matchc 21257897Sbosticstructure 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. 22957897Sbostic.It Dv GLOB_TILDE 23057897SbosticExpand patterns that start with 23157897Sbostic.Ql ~ 23257897Sbosticto 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 24257897Sbostic.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. 356*66826Sbostic.Sh EXAMPLE 357*66826SbosticA rough equivalent of 358*66826Sbostic.Ql "ls -l *.c *.h" 359*66826Sbosticcan be obtained with the 360*66826Sbosticfollowing code: 361*66826Sbostic.Bd -literal -offset indent 362*66826Sbosticglob_t g; 363*66826Sbostic 364*66826Sbosticg.gl_offs = 2; 365*66826Sbosticglob("*.c", GLOB_DOOFFS, NULL, &g); 366*66826Sbosticglob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g); 367*66826Sbosticg.gl_pathv[0] = "ls"; 368*66826Sbosticg.gl_pathv[1] = "-l"; 369*66826Sbosticexecvp("ls", g.gl_pathv); 370*66826Sbostic.Ed 37148352Scael.Sh SEE ALSO 37248352Scael.Xr sh 1 , 37348352Scael.Xr fnmatch 3 , 37448352Scael.Xr regexp 3 37548352Scael.Sh STANDARDS 37640086SbosticThe 37748352Scael.Fn glob 37848352Scaelfunction is expected to be 37948352Scael.St -p1003.2 38048352Scaelcompatible with the exception 38156944Smckusickthat the flags 38257897Sbostic.Dv GLOB_ALTDIRFUNC, 38357897Sbostic.Dv GLOB_BRACE 38457897Sbostic.Dv GLOB_MAGCHAR, 38557897Sbostic.Dv GLOB_NOMAGIC, 38657897Sbostic.Dv GLOB_QUOTE, 38756944Smckusickand 38857897Sbostic.Dv GLOB_TILDE, 38947591Sbosticand the fields 39048352Scael.Fa gl_matchc 39147591Sbosticand 39248352Scael.Fa gl_flags 39348352Scaelshould not be used by applications striving for strict 39448352Scael.Tn POSIX 39548352Scaelconformance. 39648352Scael.Sh HISTORY 39748352ScaelThe 39848352Scael.Fn glob 39948352Scaeland 40048352Scael.Fn globfree 40162860Selanfunctions first appeared in 4.4BSD. 40248352Scael.Sh BUGS 40348352ScaelPatterns longer than 40448352Scael.Dv MAXPATHLEN 40548352Scaelmay cause unchecked errors. 40648352Scael.Pp 40748352ScaelThe 40848352Scael.Fn glob 40948352Scaelargument 41040086Sbosticmay fail and set errno for any of the errors specified for the 41140086Sbosticlibrary routines 41248352Scael.Xr stat 2 , 41348352Scael.Xr closedir 3 , 41448352Scael.Xr opendir 3 , 41548352Scael.Xr readdir 3 , 41648352Scael.Xr malloc 3 , 41740086Sbosticand 41848352Scael.Xr free 3 . 419