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