logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

glob.3p (12347B)


  1. '\" et
  2. .TH GLOB "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. glob,
  12. globfree
  13. \(em generate pathnames matching a pattern
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <glob.h>
  18. .P
  19. int glob(const char *restrict \fIpattern\fP, int \fIflags\fP,
  20. int(*\fIerrfunc\fP)(const char *\fIepath\fP, int \fIeerrno\fP),
  21. glob_t *restrict \fIpglob\fP);
  22. void globfree(glob_t *\fIpglob\fP);
  23. .fi
  24. .SH DESCRIPTION
  25. The
  26. \fIglob\fR()
  27. function is a pathname generator that shall implement the rules
  28. defined in the Shell and Utilities volume of POSIX.1\(hy2017,
  29. .IR "Section 2.13" ", " "Pattern Matching Notation",
  30. with optional support for rule 3 in the Shell and Utilities volume of POSIX.1\(hy2017,
  31. .IR "Section 2.13.3" ", " "Patterns Used for Filename Expansion".
  32. .P
  33. The structure type
  34. .BR glob_t
  35. is defined in
  36. .IR <glob.h>
  37. and includes at least the following members:
  38. .TS
  39. center box tab(!);
  40. cB | cB | cB
  41. lw(1.25i)B | lw(1.25i)I | lw(2.5i).
  42. Member Type!Member Name!Description
  43. _
  44. size_t!gl_pathc!Count of paths matched by \fIpattern\fP.
  45. char **!gl_pathv!Pointer to a list of matched pathnames.
  46. size_t!gl_offs!T{
  47. Slots to reserve at the beginning of \fIgl_pathv\fP.
  48. T}
  49. .TE
  50. .P
  51. The argument
  52. .IR pattern
  53. is a pointer to a pathname pattern to be expanded. The
  54. \fIglob\fR()
  55. function shall match all accessible pathnames against this pattern and
  56. develop a list of all pathnames that match. In order to have access to
  57. a pathname,
  58. \fIglob\fR()
  59. requires search permission on every component of a path except the
  60. last, and read permission on each directory of any filename component
  61. of
  62. .IR pattern
  63. that contains any of the following special characters:
  64. .BR '*' ,
  65. .BR '?' ,
  66. and
  67. .BR '[' .
  68. .P
  69. The
  70. \fIglob\fR()
  71. function shall store the number of matched pathnames into
  72. \fIpglob\fP\->\fIgl_pathc\fP and a pointer to a list of pointers to
  73. pathnames into \fIpglob\fP\->\fIgl_pathv\fP. The pathnames shall be in
  74. sort order as defined by the current setting of the
  75. .IR LC_COLLATE
  76. category; see the Base Definitions volume of POSIX.1\(hy2017,
  77. .IR "Section 7.3.2" ", " "LC_COLLATE".
  78. The first pointer after the last pathname shall be a null pointer. If
  79. the pattern does not match any pathnames, the returned number of matched
  80. paths is set to 0, and the contents of \fIpglob\fP\->\fIgl_pathv\fP
  81. are implementation-defined.
  82. .P
  83. It is the caller's responsibility to create the structure pointed to by
  84. .IR pglob .
  85. The
  86. \fIglob\fR()
  87. function shall allocate other space as needed, including the memory
  88. pointed to by
  89. .IR gl_pathv .
  90. The
  91. \fIglobfree\fR()
  92. function shall free any space associated with
  93. .IR pglob
  94. from a previous call to
  95. \fIglob\fR().
  96. .P
  97. The
  98. .IR flags
  99. argument is used to control the behavior of
  100. \fIglob\fR().
  101. The value of
  102. .IR flags
  103. is a bitwise-inclusive OR of zero or more of the following
  104. constants, which are defined in
  105. .IR <glob.h> :
  106. .IP GLOB_APPEND 14
  107. Append pathnames generated to the ones from a previous call to
  108. \fIglob\fR().
  109. .IP GLOB_DOOFFS 14
  110. Make use of \fIpglob\fP\->\fIgl_offs\fP. If this flag is set,
  111. \fIpglob\fP\->\fIgl_offs\fP is used to specify how many null pointers
  112. to add to the beginning of \fIpglob\fP\->\fIgl_pathv\fP. In other
  113. words, \fIpglob\fP\->\fIgl_pathv\fP shall point to
  114. \fIpglob\fP\->\fIgl_offs\fP null pointers, followed by
  115. \fIpglob\fP\->\fIgl_pathc\fP pathname pointers, followed by a null
  116. pointer.
  117. .IP GLOB_ERR 14
  118. Cause
  119. \fIglob\fR()
  120. to return when it encounters a directory that it cannot open or read.
  121. Ordinarily,
  122. \fIglob\fR()
  123. continues to find matches.
  124. .IP GLOB_MARK 14
  125. Each pathname that is a directory that matches
  126. .IR pattern
  127. shall have a
  128. <slash>
  129. appended.
  130. .IP GLOB_NOCHECK 14
  131. Supports rule 3 in the Shell and Utilities volume of POSIX.1\(hy2017,
  132. .IR "Section 2.13.3" ", " "Patterns Used for Filename Expansion".
  133. If
  134. .IR pattern
  135. does not match any pathname, then
  136. \fIglob\fR()
  137. shall return a list consisting of only
  138. .IR pattern ,
  139. and the number of matched pathnames is 1.
  140. .IP GLOB_NOESCAPE 14
  141. Disable backslash escaping.
  142. .IP GLOB_NOSORT 14
  143. Ordinarily,
  144. \fIglob\fR()
  145. sorts the matching pathnames according to the current setting of the
  146. .IR LC_COLLATE
  147. category; see the Base Definitions volume of POSIX.1\(hy2017,
  148. .IR "Section 7.3.2" ", " "LC_COLLATE".
  149. When this flag is used, the order of pathnames returned is unspecified.
  150. .P
  151. The GLOB_APPEND
  152. flag can be used to append a new set of pathnames to those found in a
  153. previous call to
  154. \fIglob\fR().
  155. The following rules apply to applications when two or more calls to
  156. \fIglob\fR()
  157. are made with the same value of
  158. .IR pglob
  159. and without intervening calls to
  160. \fIglobfree\fR():
  161. .IP " 1." 4
  162. The first such call shall not set GLOB_APPEND. All subsequent calls
  163. shall set it.
  164. .IP " 2." 4
  165. All the calls shall set GLOB_DOOFFS, or all shall not set it.
  166. .IP " 3." 4
  167. After the second call, \fIpglob\fP\->\fIgl_pathv\fP points to a list
  168. containing the following:
  169. .RS 4
  170. .IP " a." 4
  171. Zero or more null pointers, as specified by GLOB_DOOFFS and
  172. \fIpglob\fP\->\fIgl_offs\fP.
  173. .IP " b." 4
  174. Pointers to the pathnames that were in the
  175. \fIpglob\fP\->\fIgl_pathv\fP list before the call, in the same order
  176. as before.
  177. .IP " c." 4
  178. Pointers to the new pathnames generated by the second call, in the
  179. specified order.
  180. .RE
  181. .IP " 4." 4
  182. The count returned in \fIpglob\fP\->\fIgl_pathc\fP shall be the total
  183. number of pathnames from the two calls.
  184. .IP " 5." 4
  185. The application can change any of the fields after a call to
  186. \fIglob\fR().
  187. If it does, the application shall reset them to the original value
  188. before a subsequent call, using the same
  189. .IR pglob
  190. value, to
  191. \fIglobfree\fR()
  192. or
  193. \fIglob\fR()
  194. with the GLOB_APPEND flag.
  195. .P
  196. If, during the search, a directory is encountered that cannot be opened
  197. or read and
  198. .IR errfunc
  199. is not a null pointer,
  200. \fIglob\fR()
  201. calls
  202. \fI(\fR()*errfunc )
  203. with two arguments:
  204. .IP " 1." 4
  205. The
  206. .IR epath
  207. argument is a pointer to the path that failed.
  208. .IP " 2." 4
  209. The
  210. .IR eerrno
  211. argument is the value of
  212. .IR errno
  213. from the failure, as set by
  214. \fIopendir\fR(),
  215. \fIreaddir\fR(),
  216. or
  217. \fIstat\fR().
  218. (Other values may be used to report other errors not explicitly
  219. documented for those functions.)
  220. .P
  221. If
  222. \fI(\fR()*errfunc )
  223. is called and returns non-zero, or if the GLOB_ERR flag is set in
  224. .IR flags ,
  225. \fIglob\fR()
  226. shall stop the scan and return GLOB_ABORTED after setting
  227. .IR gl_pathc
  228. and
  229. .IR gl_pathv
  230. in
  231. .IR pglob
  232. to reflect the paths already scanned. If GLOB_ERR is not set and
  233. either
  234. .IR errfunc
  235. is a null pointer or
  236. \fI(\fR()*errfunc )
  237. returns 0, the error shall be ignored.
  238. .P
  239. The
  240. \fIglob\fR()
  241. function shall not fail because of large files.
  242. .SH "RETURN VALUE"
  243. Upon successful completion,
  244. \fIglob\fR()
  245. shall return 0. The argument \fIpglob\fP\->\fIgl_pathc\fP shall
  246. return the number of matched pathnames and the argument
  247. \fIpglob\fP\->\fIgl_pathv\fP shall contain a pointer to a
  248. null-terminated list of matched and sorted pathnames. However, if
  249. \fIpglob\fP\->\fIgl_pathc\fP is 0, the content of
  250. \fIpglob\fP\->\fIgl_pathv\fP is undefined.
  251. .P
  252. The
  253. \fIglobfree\fR()
  254. function shall not return a value.
  255. .P
  256. If
  257. \fIglob\fR()
  258. terminates due to an error, it shall return one of the non-zero
  259. constants defined in
  260. .IR <glob.h> .
  261. The arguments \fIpglob\fP\->\fIgl_pathc\fP and
  262. \fIpglob\fP\->\fIgl_pathv\fP are still set as defined above.
  263. .SH ERRORS
  264. The
  265. \fIglob\fR()
  266. function shall fail and return the corresponding value if:
  267. .IP GLOB_ABORTED 14
  268. The scan was stopped because GLOB_ERR was set or
  269. \fI(\fR()*errfunc )
  270. returned non-zero.
  271. .IP GLOB_NOMATCH 14
  272. The pattern does not match any existing pathname, and GLOB_NOCHECK was
  273. not set in flags.
  274. .IP GLOB_NOSPACE 14
  275. An attempt to allocate memory failed.
  276. .LP
  277. .IR "The following sections are informative."
  278. .SH "EXAMPLES"
  279. One use of the GLOB_DOOFFS flag is by applications that
  280. build an argument list for use with
  281. \fIexecv\fR(),
  282. \fIexecve\fR(),
  283. or
  284. \fIexecvp\fR().
  285. Suppose, for example, that an application wants to do the equivalent of:
  286. .sp
  287. .RS 4
  288. .nf
  289. ls -l *.c
  290. .fi
  291. .P
  292. .RE
  293. .P
  294. but for some reason:
  295. .sp
  296. .RS 4
  297. .nf
  298. system("ls -l *.c")
  299. .fi
  300. .P
  301. .RE
  302. .P
  303. is not acceptable. The application could obtain approximately the same
  304. result using the sequence:
  305. .sp
  306. .RS 4
  307. .nf
  308. globbuf.gl_offs = 2;
  309. glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
  310. globbuf.gl_pathv[0] = "ls";
  311. globbuf.gl_pathv[1] = "-l";
  312. execvp("ls", &globbuf.gl_pathv[0]);
  313. .fi
  314. .P
  315. .RE
  316. .P
  317. Using the same example:
  318. .sp
  319. .RS 4
  320. .nf
  321. ls -l *.c *.h
  322. .fi
  323. .P
  324. .RE
  325. .P
  326. could be approximately simulated using GLOB_APPEND as follows:
  327. .sp
  328. .RS 4
  329. .nf
  330. globbuf.gl_offs = 2;
  331. glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
  332. glob("*.h", GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf);
  333. \&...
  334. .fi
  335. .P
  336. .RE
  337. .SH "APPLICATION USAGE"
  338. This function is not provided for the purpose of enabling utilities to
  339. perform pathname expansion on their arguments, as this operation is
  340. performed by the shell, and utilities are explicitly not expected to
  341. redo this. Instead, it is provided for applications that need to do
  342. pathname expansion on strings obtained from other sources, such as a
  343. pattern typed by a user or read from a file.
  344. .P
  345. If a utility needs to see if a pathname matches a given pattern, it can
  346. use
  347. \fIfnmatch\fR().
  348. .P
  349. Note that
  350. .IR gl_pathc
  351. and
  352. .IR gl_pathv
  353. have meaning even if
  354. \fIglob\fR()
  355. fails. This allows
  356. \fIglob\fR()
  357. to report partial results in the event of an error. However, if
  358. .IR gl_pathc
  359. is 0,
  360. .IR gl_pathv
  361. is unspecified even if
  362. \fIglob\fR()
  363. did not return an error.
  364. .P
  365. The GLOB_NOCHECK option could be used when an application wants to
  366. expand a pathname if wildcards are specified, but wants to treat the
  367. pattern as just a string otherwise. The
  368. .IR sh
  369. utility might use this for option-arguments, for example.
  370. .P
  371. The new pathnames generated by a subsequent call with GLOB_APPEND are
  372. not sorted together with the previous pathnames. This mirrors the way
  373. that the shell handles pathname expansion when multiple expansions are
  374. done on a command line.
  375. .P
  376. Applications that need tilde and parameter expansion should use
  377. \fIwordexp\fR().
  378. .SH RATIONALE
  379. It was claimed that the GLOB_DOOFFS flag is unnecessary because it
  380. could be simulated using:
  381. .sp
  382. .RS 4
  383. .nf
  384. new = (char **)malloc((n + pglob->gl_pathc + 1)
  385. * sizeof(char *));
  386. (void) memcpy(new+n, pglob->gl_pathv,
  387. pglob->gl_pathc * sizeof(char *));
  388. (void) memset(new, 0, n * sizeof(char *));
  389. free(pglob->gl_pathv);
  390. pglob->gl_pathv = new;
  391. .fi
  392. .P
  393. .RE
  394. .P
  395. However, this assumes that the memory pointed to by
  396. .IR gl_pathv
  397. is a block that was separately created using
  398. \fImalloc\fR().
  399. This is not necessarily the case. An application should make no
  400. assumptions about how the memory referenced by fields in
  401. .IR pglob
  402. was allocated. It might have been obtained from
  403. \fImalloc\fR()
  404. in a large chunk and then carved up within
  405. \fIglob\fR(),
  406. or it might have been created using a different memory allocator. It
  407. is not the intent of the standard developers to specify or imply how
  408. the memory used by
  409. \fIglob\fR()
  410. is managed.
  411. .P
  412. The GLOB_APPEND flag would be used when an application wants to expand
  413. several different patterns into a single list.
  414. .SH "FUTURE DIRECTIONS"
  415. None.
  416. .SH "SEE ALSO"
  417. .IR "\fIexec\fR\^",
  418. .IR "\fIfdopendir\fR\^(\|)",
  419. .IR "\fIfnmatch\fR\^(\|)",
  420. .IR "\fIfstatat\fR\^(\|)",
  421. .IR "\fIreaddir\fR\^(\|)",
  422. .IR "Section 2.6" ", " "Word Expansions"
  423. .P
  424. The Base Definitions volume of POSIX.1\(hy2017,
  425. .IR "Section 7.3.2" ", " "LC_COLLATE",
  426. .IR "\fB<glob.h>\fP"
  427. .\"
  428. .SH COPYRIGHT
  429. Portions of this text are reprinted and reproduced in electronic form
  430. from IEEE Std 1003.1-2017, Standard for Information Technology
  431. -- Portable Operating System Interface (POSIX), The Open Group Base
  432. Specifications Issue 7, 2018 Edition,
  433. Copyright (C) 2018 by the Institute of
  434. Electrical and Electronics Engineers, Inc and The Open Group.
  435. In the event of any discrepancy between this version and the original IEEE and
  436. The Open Group Standard, the original IEEE and The Open Group Standard
  437. is the referee document. The original Standard can be obtained online at
  438. http://www.opengroup.org/unix/online.html .
  439. .PP
  440. Any typographical or formatting errors that appear
  441. in this page are most likely
  442. to have been introduced during the conversion of the source files to
  443. man page format. To report such errors, see
  444. https://www.kernel.org/doc/man-pages/reporting_bugs.html .