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