1.\" $NetBSD: glob.3,v 1.32 2008/02/22 18:33:51 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 February 22, 2008 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.It Dv GLOB_NO_DOTDIRS 265Hide 266.Sq Li \&. 267and 268.Sq Li \&.. 269from metacharacter matches, regardless of whether 270.Dv GLOB_PERIOD 271is set and whether the pattern component begins with a literal period. 272.El 273.Pp 274If, during the search, a directory is encountered that cannot be opened 275or read and 276.Fa errfunc 277is 278.Pf non- Dv NULL , 279.Fn glob 280calls 281.Fa (*errfunc)(path, errno) . 282This may be unintuitive: a pattern like 283.Ql */Makefile 284will try to 285.Xr stat 2 286.Ql foo/Makefile 287even if 288.Ql foo 289is not a directory, resulting in a 290call to 291.Fa errfunc . 292The error routine can suppress this action by testing for 293.Dv ENOENT 294and 295.Dv ENOTDIR ; 296however, the 297.Dv GLOB_ERR 298flag will still cause an immediate 299return when this happens. 300.Pp 301If 302.Fa errfunc 303returns non-zero, 304.Fn glob 305stops the scan and returns 306.Dv GLOB_ABORTED 307after setting 308.Fa gl_pathc 309and 310.Fa gl_pathv 311to reflect any paths already matched. 312This also happens if an error is encountered and 313.Dv GLOB_ERR 314is set in 315.Fa flags , 316regardless of the return value of 317.Fa errfunc , 318if called. 319If 320.Dv GLOB_ERR 321is not set and either 322.Fa errfunc 323is 324.Dv NULL 325or 326.Fa errfunc 327returns zero, the error is ignored. 328.Pp 329The 330.Fn globfree 331function frees any space associated with 332.Fa pglob 333from a previous call(s) to 334.Fn glob . 335.Pp 336The historical 337.Dv GLOB_QUOTE 338flag is no longer supported. 339Per 340.St -p1003.2-92 , 341backslash escaping of special characters is the default behaviour; 342it may be disabled by specifying the 343.Dv GLOB_NOESCAPE 344flag. 345.Sh RETURN VALUES 346On successful completion, 347.Fn glob 348returns zero. 349In addition the fields of 350.Fa pglob 351contain the values described below: 352.Bl -tag -width GLOB_NOCHECK 353.It Fa gl_pathc 354contains the total number of matched pathnames so far. 355This includes other matches from previous invocations of 356.Fn glob 357if 358.Dv GLOB_APPEND 359was specified. 360.It Fa gl_matchc 361contains the number of matched pathnames in the current invocation of 362.Fn glob . 363.It Fa gl_flags 364contains a copy of the 365.Fa flags 366parameter with the bit 367.Dv GLOB_MAGCHAR 368set if 369.Fa pattern 370contained any of the special characters ``*'', ``?'' or ``['', cleared 371if not. 372.It Fa gl_pathv 373contains a pointer to a 374.Dv NULL Ns -terminated 375list of matched pathnames. 376However, if 377.Fa gl_pathc 378is zero, the contents of 379.Fa gl_pathv 380are undefined. 381.El 382.Pp 383If 384.Fn glob 385terminates due to an error, it sets 386.Va errno 387and returns one of the following non-zero constants, which are defined 388in the include file 389.Aq Pa glob.h : 390.Bl -tag -width GLOB_ABORTEDXXX 391.It Dv GLOB_ABORTED 392The scan was stopped because an error was encountered and either 393.Dv GLOB_ERR 394was set or 395.Fa (*errfunc)() 396returned non-zero. 397.It Dv GLOB_NOMATCH 398The pattern does not match any existing pathname, and 399.Dv GLOB_NOCHECK 400was not set in 401.Dv flags . 402.It Dv GLOB_NOSPACE 403An attempt to allocate memory failed, or if 404.Va errno 405was 0 406.Li GLOB_LIMIT 407was specified in the flags and 408.Li ARG_MAX 409patterns were matched. 410.El 411.Pp 412The historical 413.Dv GLOB_ABEND 414return constant is no longer supported. 415Portable applications should use the 416.Dv GLOB_ABORTED 417constant instead. 418.Pp 419The arguments 420.Fa pglob\-\*[Gt]gl_pathc 421and 422.Fa pglob\-\*[Gt]gl_pathv 423are still set as specified above. 424.Sh ENVIRONMENT 425.Bl -tag -width HOME -compact 426.It Ev HOME 427If defined, used as the home directory of the current user in 428tilde expansions. 429.El 430.Sh EXAMPLES 431A rough equivalent of 432.Ql "ls -l *.c *.h" 433can be obtained with the 434following code: 435.Bd -literal -offset indent 436glob_t g; 437 438g.gl_offs = 2; 439glob("*.c", GLOB_DOOFFS, NULL, \*[Am]g); 440glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, \*[Am]g); 441g.gl_pathv[0] = "ls"; 442g.gl_pathv[1] = "-l"; 443execvp("ls", g.gl_pathv); 444.Ed 445.Sh SEE ALSO 446.Xr sh 1 , 447.Xr fnmatch 3 , 448.Xr regexp 3 449.Sh STANDARDS 450The 451.Fn glob 452function is expected to be 453.St -p1003.2 454compatible with the exception 455that the flags 456.Dv GLOB_ALTDIRFUNC , 457.Dv GLOB_BRACE , 458.Dv GLOB_MAGCHAR , 459.Dv GLOB_NOMAGIC , 460.Dv GLOB_TILDE , 461and 462.Dv GLOB_LIMIT 463and the fields 464.Fa gl_matchc 465and 466.Fa gl_flags 467should not be used by applications striving for strict 468.Tn POSIX 469conformance. 470.Sh HISTORY 471The 472.Fn glob 473and 474.Fn globfree 475functions first appeared in 476.Bx 4.4 . 477.Sh BUGS 478Patterns longer than 479.Dv MAXPATHLEN 480may cause unchecked errors. 481.Pp 482The 483.Fn glob 484function may fail and set 485.Va errno 486for any of the errors specified for the library routines 487.Xr stat 2 , 488.Xr closedir 3 , 489.Xr opendir 3 , 490.Xr readdir 3 , 491.Xr malloc 3 , 492and 493.Xr free 3 . 494