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