📄 glob.3
字号:
.\" Copyright (c) 1989, 1991, 1993, 1994.\" The Regents of the University of California. All rights reserved..\".\" This code is derived from software contributed to Berkeley by.\" Guido van Rossum..\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)glob.3 8.3 (Berkeley) 4/16/94.\".Dd April 16, 1994.Dt GLOB 3.Os.Sh NAME.Nm glob ,.Nm globfree.Nd generate pathnames matching a pattern.Sh SYNOPSIS.Fd #include <glob.h>.Ft int.Fn glob "const char *pattern" "int flags" "const int (*errfunc)(const char *, int)" "glob_t *pglob".Ft void .Fn globfree "glob_t *pglob".Sh DESCRIPTIONThe.Fn globfunctionis a pathname generator that implements the rules for file name patternmatching used by the shell..PpThe include file.Pa glob.hdefines the structure type.Fa glob_t ,which contains at least the following fields:.Bd -literaltypedef struct { int gl_pathc; /* count of total paths so far */ int gl_matchc; /* count of paths matching pattern */ int gl_offs; /* reserved at beginning of gl_pathv */ int gl_flags; /* returned flags */ char **gl_pathv; /* list of paths matching pattern */} glob_t;.Ed.PpThe argument.Fa patternis a pointer to a pathname pattern to be expanded.The.Fn globargumentmatches all accessible pathnames against the pattern and createsa list of the pathnames that match.In order to have access to a pathname,.Fn globrequires search permission on every component of a path except the lastand read permission on each directory of any filename component of.Fa patternthat contains any of the special characters.Ql * ,.Ql ?or.Ql [ ..PpThe.Fn globargumentstores the number of matched pathnames into the.Fa gl_pathcfield, and a pointer to a list of pointers to pathnames into the.Fa gl_pathvfield.The first pointer after the last pathname is.Dv NULL . If the pattern does not match any pathnames, the returned number ofmatched paths is set to zero..PpIt is the caller's responsibility to create the structure pointed to by.Fa pglob .The.Fn globfunction allocates other space as needed, including the memory pointedto by.Fa gl_pathv ..PpThe argument.Fa flagsis used to modify the behavior of.Fn glob .The value of.Fa flagsis the bitwise inclusive.Tn ORof any of the followingvalues defined in.Pa glob.h :.Bl -tag -width GLOB_ALTDIRFUNC.It Dv GLOB_APPENDAppend pathnames generated to the ones from a previous call (or calls)to.Fn glob .The value of.Fa gl_pathcwill be the total matches found by this call and the previous call(s).The pathnames are appended to, not merged with the pathnames returned bythe previous call(s).Between calls, the caller must not change the setting of the.Dv GLOB_DOOFFSflag, nor change the value of.Fa gl_offswhen.Dv GLOB_DOOFFSis set, nor (obviously) call.Fn globfreefor.Fa pglob ..It Dv GLOB_DOOFFSMake use of the.Fa gl_offsfield.If this flag is set,.Fa gl_offsis used to specify how many.Dv NULLpointers to prepend to the beginningof the.Fa gl_pathvfield.In other words,.Fa gl_pathvwill point to.Fa gl_offs.Dv NULLpointers,followed by.Fa gl_pathcpathname pointers, followed by a.Dv NULLpointer..It Dv GLOB_ERRCauses.Fn globto return when it encounters a directory that it cannot open or read.Ordinarily,.Fn globcontinues to find matches..It Dv GLOB_MARKEach pathname that is a directory that matches.Fa patternhas a slashappended..It Dv GLOB_NOCHECKIf.Fa patterndoes not match any pathname, then.Fn globreturns a listconsisting of only.Fa pattern ,with the number of total pathnames is set to 1, and the number of matchedpathnames set to 0.If.Dv GLOB_QUOTEis set, its effect is present in the pattern returned..It Dv GLOB_NOSORTBy default, the pathnames are sorted in ascending.Tn ASCIIorder;this flag prevents that sorting (speeding up.Fn glob ) ..El.PpThe following values may also be included in.Fa flags ,however, they are non-standard extensions to.St -p1003.2 ..Bl -tag -width GLOB_ALTDIRFUNC.It Dv GLOB_ALTDIRFUNCThe following additional fields in the pglob structure have beeninitialized with alternate functions for glob to use to open, read,and close directories and to get stat information on names foundin those directories..Bd -literalvoid *(*gl_opendir)(const char * name);struct dirent *(*gl_readdir)(void *);void (*gl_closedir)(void *);int (*gl_lstat)(const char *name, struct stat *st);int (*gl_stat)(const char *name, struct stat *st);.Ed.PpThis extension is provided to allow programs such as.Xr restore 8to provide globbing from directories stored on tape..It Dv GLOB_BRACEPre-process the pattern string to expand .Ql {pat,pat,...} strings like .Xr csh 1. The pattern .Ql {}is left unexpanded for historical reasons.Xr (Csh 1 does the same thing to ease typingof .Xr find 1 patterns)..It Dv GLOB_MAGCHARSet by the.Fn globfunction if the pattern included globbing characters.See the description of the usage of the .Fa gl_matchcstructure member for more details..It Dv GLOB_NOMAGICIs the same as .Dv GLOB_NOCHECK but it only appends the.Fa patternif it does not contain any of the special characters ``*'', ``?'' or ``[''..Dv GLOB_NOMAGIC is provided to simplify implementing the historic.Xr csh 1globbing behavior and should probably not be used anywhere else..It Dv GLOB_QUOTEUse the backslash.Pq Ql \echaracter for quoting: every occurrence ofa backslash followed by a character in the pattern is replaced by thatcharacter, avoiding any special interpretation of the character..It Dv GLOB_TILDEExpand patterns that start with.Ql ~to user name home directories..El.PpIf, during the search, a directory is encountered that cannot be openedor read and.Fa errfuncis.Pf non- Dv NULL , .Fn globcalls.Fa (*errfunc)(path, errno) .This may be unintuitive: a pattern like.Ql */Makefilewill try to.Xr stat 2.Ql foo/Makefileeven if.Ql foois not a directory, resulting in acall to.Fa errfunc .The error routine can suppress this action by testing for.Dv ENOENTand.Dv ENOTDIR ; however, the.Dv GLOB_ERRflag will still cause an immediatereturn when this happens..PpIf.Fa errfuncreturns non-zero,.Fn globstops the scan and returns.Dv GLOB_ABENDafter setting.Fa gl_pathcand.Fa gl_pathvto reflect any paths already matched.This also happens if an error is encountered and.Dv GLOB_ERRis set in.Fa flags ,regardless of the return value of.Fa errfunc ,if called.If.Dv GLOB_ERRis not set and either.Fa errfuncis.Dv NULLor.Fa errfuncreturns zero, the error is ignored..PpThe.Fn globfreefunction frees any space associated with.Fa pglobfrom a previous call(s) to.Fn glob ..Sh RETURN VALUESOn successful completion,.Fn globreturns zero.In addition the fields of.Fa pglobcontain the values described below:.Bl -tag -width GLOB_NOCHECK.It Fa gl_pathccontains the total number of matched pathnames so far.This includes other matches from previous invocations of .Fn globif .Dv GLOB_APPENDwas specified..It Fa gl_matchccontains the number of matched pathnames in the current invocation of.Fn glob ..It Fa gl_flagscontains a copy of the .Fa flagsparameter with the bit.Dv GLOB_MAGCHARset if.Fa patterncontained any of the special characters ``*'', ``?'' or ``['', clearedif not..It Fa gl_pathvcontains a pointer to a.Dv NULL Ns -terminated list of matched pathnames.However, if.Fa gl_pathcis zero, the contents of.Fa gl_pathvare undefined..El.PpIf.Fn globterminates due to an error, it sets errno and returns one of thefollowing non-zero constants, which are defined in the includefile.Aq Pa glob.h :.Bl -tag -width GLOB_NOCHECK.It Dv GLOB_NOSPACEAn attempt to allocate memory failed..It Dv GLOB_ABENDThe scan was stopped because an error was encountered and either.Dv GLOB_ERRwas set or.Fa (*errfunc)()returned non-zero..El.PpThe arguments.Fa pglob\->gl_pathcand.Fa pglob\->gl_pathvare still set as specified above..Sh EXAMPLEA rough equivalent of.Ql "ls -l *.c *.h"can be obtained with thefollowing code:.Bd -literal -offset indentglob_t g;g.gl_offs = 2;glob("*.c", GLOB_DOOFFS, NULL, &g);glob("*.h", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);g.gl_pathv[0] = "ls";g.gl_pathv[1] = "-l";execvp("ls", g.gl_pathv);.Ed.Sh SEE ALSO.Xr sh 1 ,.Xr fnmatch 3 ,.Xr regexp 3.Sh STANDARDSThe.Fn globfunction is expected to be.St -p1003.2compatible with the exceptionthat the flags.Dv GLOB_ALTDIRFUNC,.Dv GLOB_BRACE.Dv GLOB_MAGCHAR,.Dv GLOB_NOMAGIC,.Dv GLOB_QUOTE,and.Dv GLOB_TILDE,and the fields .Fa gl_matchcand .Fa gl_flagsshould not be used by applications striving for strict.Tn POSIXconformance..Sh HISTORYThe.Fn globand.Fn globfreefunctions first appeared in 4.4BSD..Sh BUGSPatterns longer than.Dv MAXPATHLENmay cause unchecked errors..PpThe.Fn globargumentmay fail and set errno for any of the errors specified for thelibrary routines.Xr stat 2 ,.Xr closedir 3 ,.Xr opendir 3 ,.Xr readdir 3 ,.Xr malloc 3 ,and.Xr free 3 .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -