1GLOB(3) Linux Programmer's Manual GLOB(3)
2
3
4
6 glob, globfree - find pathnames matching a pattern, free memory from
7 glob()
8
10 #include <glob.h>
11
12 int glob(const char *pattern, int flags,
13 int (*errfunc) (const char *epath, int eerrno),
14 glob_t *pglob);
15 void globfree(glob_t *pglob);
16
18 The glob() function searches for all the pathnames matching pattern
19 according to the rules used by the shell (see glob(7)). No tilde
20 expansion or parameter substitution is done; if you want these, use
21 wordexp(3).
22
23 The globfree() function frees the dynamically allocated storage from an
24 earlier call to glob().
25
26 The results of a glob() call are stored in the structure pointed to by
27 pglob, which is a glob_t which is declared in <glob.h> and includes the
28 following elements defined by POSIX.2 (more may be present as an exten‐
29 sion):
30
31 typedef struct
32 {
33 size_t gl_pathc; /* Count of paths matched so far */
34 char **gl_pathv; /* List of matched pathnames. */
35 size_t gl_offs; /* Slots to reserve in `gl_pathv'. */
36 } glob_t;
37
38 Results are stored in dynamically allocated storage.
39
40 The parameter flags is made up of bitwise OR of zero or more the fol‐
41 lowing symbolic constants, which modify the of behaviour of glob():
42
43 GLOB_ERR
44 which means to return upon read error (because a directory does
45 not have read permission, for example),
46
47 GLOB_MARK
48 which means to append a slash to each path which corresponds to
49 a directory,
50
51 GLOB_NOSORT
52 which means don't sort the returned pathnames (they are by
53 default),
54
55 GLOB_DOOFFS
56 which means that pglob->gl_offs slots will be reserved at the
57 beginning of the list of strings in pglob->pathv,
58
59 GLOB_NOCHECK
60 which means that, if no pattern matches, to return the original
61 pattern,
62
63 GLOB_APPEND
64 which means to append to the results of a previous call. Do not
65 set this flag on the first invocation of glob().
66
67 GLOB_NOESCAPE
68 which means that meta characters cannot be quoted by back‐
69 slashes.
70
71 The flags may also include some of the following, which are GNU exten‐
72 sions and not defined by POSIX.2:
73
74 GLOB_PERIOD
75 which means that a leading period can be matched by meta charac‐
76 ters,
77
78 GLOB_ALTDIRFUNC
79 which means that alternative functions pglob->gl_closedir,
80 pglob->gl_readdir, pglob->gl_opendir, pglob->gl_lstat, and
81 pglob->gl_stat are used for file system access instead of the
82 normal library functions,
83
84 GLOB_BRACE
85 which means that csh(1) style brace expressions {a,b} are
86 expanded,
87
88 GLOB_NOMAGIC
89 which means that the pattern is returned if it contains no
90 metacharacters,
91
92 GLOB_TILDE
93 which means that tilde expansion is carried out, and
94
95 GLOB_ONLYDIR
96 which means that only directories are matched.
97
98 If errfunc is not NULL, it will be called in case of an error with the
99 arguments epath, a pointer to the path which failed, and eerrno, the
100 value of errno as returned from one of the calls to opendir(), read‐
101 dir(), or stat(). If errfunc returns non-zero, or if GLOB_ERR is set,
102 glob() will terminate after the call to errfunc.
103
104 Upon successful return, pglob->gl_pathc contains the number of matched
105 pathnames and pglob->gl_pathv a pointer to the list of matched path‐
106 names. The first pointer after the last pathname is NULL.
107
108 It is possible to call glob() several times. In that case, the
109 GLOB_APPEND flag has to be set in flags on the second and later invoca‐
110 tions.
111
112 As a GNU extension, pglob->gl_flags is set to the flags specified, ored
113 with GLOB_MAGCHAR if any metacharacters were found.
114
116 On successful completion, glob() returns zero. Other possible returns
117 are:
118
119 GLOB_NOSPACE
120 for running out of memory,
121
122 GLOB_ABORTED
123 for a read error, and
124
125 GLOB_NOMATCH
126 for no found matches.
127
129 One example of use is the following code, which simulates typing
130
131 ls -l *.c ../*.c
132
133 in the shell:
134
135 glob_t globbuf;
136
137 globbuf.gl_offs = 2;
138 glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
139 glob("../*.c", GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
140 globbuf.gl_pathv[0] = "ls";
141 globbuf.gl_pathv[1] = "-l";
142 execvp("ls", &globbuf.gl_pathv[0]);
143
145 POSIX.2, POSIX.1-2001.
146
148 The glob() function may fail due to failure of underlying function
149 calls, such as malloc() or opendir(). These will store their error
150 code in errno.
151
153 The structure elements gl_pathc and gl_offs are declared as size_t in
154 glibc 2.1, as they should according to POSIX.2, but are declared as int
155 in libc4, libc5 and glibc 2.0.
156
158 ls(1), sh(1), stat(2), exec(3), fnmatch(3), malloc(3), opendir(3),
159 readdir(3), wordexp(3), glob(7)
160
161
162
163GNU 1999-09-12 GLOB(3)