1.\" $NetBSD: glob.3,v 1.31 2007/12/05 20:25:55 christos Exp $ 2.\" 3.\" Copyright (c) 1989, 1991, 1993, 1994 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" Guido van Rossum. 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)glob.3 8.3 (Berkeley) 4/16/94 33.\" 34.Dd December 4, 2007 35.Dt GLOB 3 36.Os 37.Sh NAME 38.Nm glob , 39.Nm globfree 40.Nd generate pathnames matching a pattern 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In glob.h 45.Ft int 46.Fn glob "const char * restrict pattern" "int flags" "const int (*errfunc)(const char *, int)" "glob_t * restrict pglob" 47.Ft void 48.Fn globfree "glob_t *pglob" 49.Sh DESCRIPTION 50The 51.Fn glob 52function 53is a pathname generator that implements the rules for file name pattern 54matching used by the shell. 55.Pp 56The include file 57.Pa glob.h 58defines the structure type 59.Fa glob_t , 60which contains at least the following fields: 61.Bd -literal 62typedef struct { 63 size_t gl_pathc; /* count of total paths so far */ 64 size_t gl_matchc; /* count of paths matching pattern */ 65 size_t gl_offs; /* reserved at beginning of gl_pathv */ 66 int gl_flags; /* returned flags */ 67 char **gl_pathv; /* list of paths matching pattern */ 68} glob_t; 69.Ed 70.Pp 71The argument 72.Fa pattern 73is a pointer to a pathname pattern to be expanded. 74The 75.Fn glob 76argument 77matches all accessible pathnames against the pattern and creates 78a list of the pathnames that match. 79In order to have access to a pathname, 80.Fn glob 81requires search permission on every component of a path except the last 82and read permission on each directory of any filename component of 83.Fa pattern 84that contains any of the special characters 85.Ql * , 86.Ql \&? 87or 88.Ql \&[ . 89.Pp 90The 91.Fn glob 92argument 93stores the number of matched pathnames into the 94.Fa gl_pathc 95field, and a pointer to a list of pointers to pathnames into the 96.Fa gl_pathv 97field. 98The first pointer after the last pathname is 99.Dv NULL . 100If the pattern does not match any pathnames, the returned number of 101matched paths is set to zero. 102.Pp 103It is the caller's responsibility to create the structure pointed to by 104.Fa pglob . 105The 106.Fn glob 107function allocates other space as needed, including the memory pointed 108to by 109.Fa gl_pathv . 110.Pp 111The argument 112.Fa flags 113is used to modify the behavior of 114.Fn glob . 115The value of 116.Fa flags 117is the bitwise inclusive 118.Tn OR 119of any of the following 120values defined in 121.Pa glob.h : 122.Bl -tag -width GLOB_ALTDIRFUNC 123.It Dv GLOB_APPEND 124Append pathnames generated to the ones from a previous call (or calls) 125to 126.Fn glob . 127The value of 128.Fa gl_pathc 129will be the total matches found by this call and the previous call(s). 130The pathnames are appended to, not merged with the pathnames returned by 131the previous call(s). 132Between calls, the caller must not change the setting of the 133.Dv GLOB_DOOFFS 134flag, nor change the value of 135.Fa gl_offs 136when 137.Dv GLOB_DOOFFS 138is set, nor (obviously) call 139.Fn globfree 140for 141.Fa pglob . 142.It Dv GLOB_DOOFFS 143Make use of the 144.Fa gl_offs 145field. 146If this flag is set, 147.Fa gl_offs 148is used to specify how many 149.Dv NULL 150pointers to prepend to the beginning 151of the 152.Fa gl_pathv 153field. 154In other words, 155.Fa gl_pathv 156will point to 157.Fa gl_offs 158.Dv NULL 159pointers, 160followed by 161.Fa gl_pathc 162pathname pointers, followed by a 163.Dv NULL 164pointer. 165.It Dv GLOB_ERR 166Causes 167.Fn glob 168to return when it encounters a directory that it cannot open or read. 169Ordinarily, 170.Fn glob 171continues to find matches. 172.It Dv GLOB_MARK 173Each pathname that is a directory that matches 174.Fa pattern 175has a slash 176appended. 177.It Dv GLOB_NOCHECK 178If 179.Fa pattern 180does not match any pathname, then 181.Fn glob 182returns a list 183consisting of only 184.Fa pattern , 185with the number of total pathnames set to 1, and the number of matched 186pathnames set to 0. 187.It Dv GLOB_NOSORT 188By default, the pathnames are sorted in ascending 189.Tn ASCII 190order; 191this flag prevents that sorting (speeding up 192.Fn glob ) . 193.El 194.Pp 195The following values may also be included in 196.Fa flags , 197however, they are non-standard extensions to 198.St -p1003.2 . 199.Bl -tag -width GLOB_ALTDIRFUNC 200.It Dv GLOB_ALTDIRFUNC 201The following additional fields in the pglob structure have been 202initialized with alternate functions for glob to use to open, read, 203and close directories and to get stat information on names found 204in those directories. 205.Bd -literal 206 void *(*gl_opendir)(const char * name); 207 struct dirent *(*gl_readdir)(void *); 208 void (*gl_closedir)(void *); 209 int (*gl_lstat)(const char *name, struct stat *st); 210 int (*gl_stat)(const char *name, struct stat *st); 211.Ed 212.Pp 213This extension is provided to allow programs such as 214.Xr restore 8 215to provide globbing from directories stored on tape. 216.It Dv GLOB_BRACE 217Pre-process the pattern string to expand 218.Ql {pat,pat,...} 219strings like 220.Xr csh 1 . 221The pattern 222.Ql {} 223is left unexpanded for historical reasons 224.Po 225.Xr csh 1 226does the same thing to ease typing of 227.Xr find 1 228patterns 229.Pc . 230.It Dv GLOB_MAGCHAR 231Set by the 232.Fn glob 233function if the pattern included globbing characters. 234See the description of the usage of the 235.Fa gl_matchc 236structure member for more details. 237.It Dv GLOB_NOMAGIC 238Is the same as 239.Dv GLOB_NOCHECK 240but it only appends the 241.Fa pattern 242if it does not contain any of the special characters ``*'', ``?'' or ``[''. 243.Dv GLOB_NOMAGIC 244is provided to simplify implementing the historic 245.Xr csh 1 246globbing behavior and should probably not be used anywhere else. 247.It Dv GLOB_NOESCAPE 248Disable the use of the backslash 249.Pq Ql \e 250character for quoting. 251.It Dv GLOB_TILDE 252Expand patterns that start with 253.Ql ~ 254to user name home directories. 255.It Dv GLOB_LIMIT 256Limit the amount of memory used by matches to 257.Li ARG_MAX . 258This option should be set for programs that can be coerced to a denial of 259service attack via patterns that expand to a very large number of matches, 260such as a long string of 261.Li */../*/.. 262.It Dv GLOB_PERIOD 263Allow metacharacters to match a leading period in a filename. 264.El 265.Pp 266If, during the search, a directory is encountered that cannot be opened 267or read and 268.Fa errfunc 269is 270.Pf non- Dv NULL , 271.Fn glob 272calls 273.Fa (*errfunc)(path, errno) . 274This may be unintuitive: a pattern like 275.Ql */Makefile 276will try to 277.Xr stat 2 278.Ql foo/Makefile 279even if 280.Ql foo 281is not a directory, resulting in a 282call to 283.Fa errfunc . 284The error routine can suppress this action by testing for 285.Dv ENOENT 286and 287.Dv ENOTDIR ; 288however, the 289.Dv GLOB_ERR 290flag will still cause an immediate 291return when this happens. 292.Pp 293If 294.Fa errfunc 295returns non-zero, 296.Fn glob 297stops the scan and returns 298.Dv GLOB_ABORTED 299after setting 300.Fa gl_pathc 301and 302.Fa gl_pathv 303to reflect any paths already matched. 304This also happens if an error is encountered and 305.Dv GLOB_ERR 306is set in 307.Fa flags , 308regardless of the return value of 309.Fa errfunc , 310if called. 311If 312.Dv GLOB_ERR 313is not set and either 314.Fa errfunc 315is 316.Dv NULL 317or 318.Fa errfunc 319returns zero, the error is ignored. 320.Pp 321The 322.Fn globfree 323function frees any space associated with 324.Fa pglob 325from a previous call(s) to 326.Fn glob . 327.Pp 328The historical 329.Dv GLOB_QUOTE 330flag is no longer supported. 331Per 332.St -p1003.2-92 , 333backslash escaping of special characters is the default behaviour; 334it may be disabled by specifying the 335.Dv GLOB_NOESCAPE 336flag. 337.Sh RETURN VALUES 338On successful completion, 339.Fn glob 340returns zero. 341In addition the fields of 342.Fa pglob 343contain the values described below: 344.Bl -tag -width GLOB_NOCHECK 345.It Fa gl_pathc 346contains the total number of matched pathnames so far. 347This includes other matches from previous invocations of 348.Fn glob 349if 350.Dv GLOB_APPEND 351was specified. 352.It Fa gl_matchc 353contains the number of matched pathnames in the current invocation of 354.Fn glob . 355.It Fa gl_flags 356contains a copy of the 357.Fa flags 358parameter with the bit 359.Dv GLOB_MAGCHAR 360set if 361.Fa pattern 362contained any of the special characters ``*'', ``?'' or ``['', cleared 363if not. 364.It Fa gl_pathv 365contains a pointer to a 366.Dv NULL Ns -terminated 367list of matched pathnames. 368However, if 369.Fa gl_pathc 370is zero, the contents of 371.Fa gl_pathv 372are undefined. 373.El 374.Pp 375If 376.Fn glob 377terminates due to an error, it sets 378.Va errno 379and returns one of the following non-zero constants, which are defined 380in the include file 381.Aq Pa glob.h : 382.Bl -tag -width GLOB_ABORTEDXXX 383.It Dv GLOB_ABORTED 384The scan was stopped because an error was encountered and either 385.Dv GLOB_ERR 386was set or 387.Fa (*errfunc)() 388returned non-zero. 389.It Dv GLOB_NOMATCH 390The pattern does not match any existing pathname, and 391.Dv GLOB_NOCHECK 392was not set in 393.Dv flags . 394.It Dv GLOB_NOSPACE 395An attempt to allocate memory failed, or if 396.Va errno 397was 0 398.Li GLOB_LIMIT 399was specified in the flags and 400.Li ARG_MAX 401patterns were matched. 402.El 403.Pp 404The historical 405.Dv GLOB_ABEND 406return constant is no longer supported. 407Portable applications should use the 408.Dv GLOB_ABORTED 409constant instead. 410.Pp 411The arguments 412.Fa pglob\-\*[Gt]gl_pathc 413and 414.Fa pglob\-\*[Gt]gl_pathv 415are still set as specified above. 416.Sh ENVIRONMENT 417.Bl -tag -width HOME -compact 418.It Ev HOME 419If defined, used as the home directory of the current user in 420tilde expansions. 421.El 422.Sh EXAMPLES 423A rough equivalent of 424.Ql "ls -l *.c *.h" 425can be obtained with the 426following code: 427.Bd -literal -offset indent 428glob_t g; 429 430g.gl_offs = 2; 431glob("*.c", GLOB_DOOFFS, NULL, \*[Am]g); 432glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, \*[Am]g); 433g.gl_pathv[0] = "ls"; 434g.gl_pathv[1] = "-l"; 435execvp("ls", g.gl_pathv); 436.Ed 437.Sh SEE ALSO 438.Xr sh 1 , 439.Xr fnmatch 3 , 440.Xr regexp 3 441.Sh STANDARDS 442The 443.Fn glob 444function is expected to be 445.St -p1003.2 446compatible with the exception 447that the flags 448.Dv GLOB_ALTDIRFUNC , 449.Dv GLOB_BRACE , 450.Dv GLOB_MAGCHAR , 451.Dv GLOB_NOMAGIC , 452.Dv GLOB_TILDE , 453and 454.Dv GLOB_LIMIT 455and the fields 456.Fa gl_matchc 457and 458.Fa gl_flags 459should not be used by applications striving for strict 460.Tn POSIX 461conformance. 462.Sh HISTORY 463The 464.Fn glob 465and 466.Fn globfree 467functions first appeared in 468.Bx 4.4 . 469.Sh BUGS 470Patterns longer than 471.Dv MAXPATHLEN 472may cause unchecked errors. 473.Pp 474The 475.Fn glob 476function may fail and set 477.Va errno 478for any of the errors specified for the library routines 479.Xr stat 2 , 480.Xr closedir 3 , 481.Xr opendir 3 , 482.Xr readdir 3 , 483.Xr malloc 3 , 484and 485.Xr free 3 . 486