glob.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. glob.h
  8. Abstract:
  9. This header contains definitions for glob functions, which allow expanding
  10. of a pattern to valid paths.
  11. Author:
  12. Evan Green 10-Feb-2015
  13. --*/
  14. #ifndef _GLOB_H
  15. #define _GLOB_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <libcbase.h>
  20. #include <dirent.h>
  21. #include <sys/stat.h>
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. //
  29. // Define flags that can be passed to the glob function.
  30. //
  31. //
  32. // Set this flag to return on read errors.
  33. //
  34. #define GLOB_ERR 0x00000001
  35. //
  36. // Set this flag to append a slash to each name.
  37. //
  38. #define GLOB_MARK 0x00000002
  39. //
  40. // Set this flag to skip sorting the results.
  41. //
  42. #define GLOB_NOSORT 0x00000004
  43. //
  44. // Set this flag to insert NULL array slots, the number of which is specified
  45. // by the gl_offs member.
  46. //
  47. #define GLOB_DOOFFS 0x00000008
  48. //
  49. // Set this flag to return the pattern itself if nothing matches the pattern.
  50. //
  51. #define GLOB_NOCHECK 0x00000010
  52. //
  53. // Set this flag to append the results to a previous call to glob.
  54. //
  55. #define GLOB_APPEND 0x00000020
  56. //
  57. // Set this flag to indicate that backslashes aren't escape characters.
  58. //
  59. #define GLOB_NOESCAPE 0x00000040
  60. //
  61. // Set this flag to indicate that leading periods can be matched by wildcards.
  62. //
  63. #define GLOB_PERIOD 0x00000080
  64. //
  65. // This flag is set if any wildcard characters were seen.
  66. //
  67. #define GLOB_MAGCHAR 0x00000100
  68. //
  69. // Set this flag to use the alternate function pointers in the glob_t structure.
  70. //
  71. #define GLOB_ALTDIRFUNC 0x00000200
  72. //
  73. // Set this flag to expand brace options.
  74. //
  75. #define GLOB_BRACE 0x00000400
  76. //
  77. // Set this flag to simply return the pattern if there were no wildcards.
  78. //
  79. #define GLOB_NOMAGIC 0x00000800
  80. //
  81. // Set this flag to enable expanding of ~user to their home directory.
  82. //
  83. #define GLOB_TILDE 0x00001000
  84. //
  85. // Set this flag to match only directories.
  86. //
  87. #define GLOB_ONLYDIR 0x00002000
  88. //
  89. // Set this flag to enable the same thing as GLOB_TILDE, but fail if the
  90. // given user name does not exist.
  91. //
  92. #define GLOB_TILDE_CHECK 0x00004000
  93. //
  94. // Set this flag to limit the results to sane values.
  95. //
  96. #define GLOB_LIMIT 0x00008000
  97. //
  98. // Old definition for compatibility.
  99. //
  100. #define GLOB_MAXPATH GLOB_LIMIT
  101. #define GLOB_ABEND GLOB_ABORTED
  102. //
  103. // Define error values returned from glob.
  104. //
  105. //
  106. // Memory allocation failure
  107. //
  108. #define GLOB_NOSPACE 1
  109. //
  110. // Read error
  111. //
  112. #define GLOB_ABORTED 2
  113. //
  114. // No matches were found
  115. //
  116. #define GLOB_NOMATCH 3
  117. //
  118. // Not implemented
  119. //
  120. #define GLOB_NOSYS 4
  121. //
  122. // ------------------------------------------------------ Data Type Definitions
  123. //
  124. /*++
  125. Structure Description:
  126. This structure defines the glob structure type.
  127. Members:
  128. gl_pathc - Stores the number of paths matched by the given pattern.
  129. gl_pathv - Stores the array of matched paths.
  130. gl_matchc - Stores the count of matches desired.
  131. gl_offs - Stores the number of null array entries to leave at the beginning
  132. of the path array.
  133. gl_flags - Stores the flags governing the glob operation. See GLOB_*
  134. definitions.
  135. gl_errfunc - Stores a pointer to the error function passed in to glob.
  136. gl_closedir - Stores an optional pointer to a function used to close a
  137. directory.
  138. gl_readdir - Stores an optional pointer to a function used to read from a
  139. directory.
  140. gl_opendir - Stores an optional pointer to a function used to open a
  141. directory.
  142. gl_lstat - Stores an optional pointer to a function used to get information
  143. about a path entry, not following symbolic links.
  144. gl_stat - Stores an optional pointer to a function used to get information
  145. about a path entry, following symbolic linkds.
  146. --*/
  147. typedef struct {
  148. size_t gl_pathc;
  149. char **gl_pathv;
  150. size_t gl_matchc;
  151. size_t gl_offs;
  152. int gl_flags;
  153. int (*gl_errfunc) (const char *, int);
  154. void (*gl_closedir) (void *);
  155. struct dirent *(*gl_readdir) (void *);
  156. void *(*gl_opendir) (const char *);
  157. int (*gl_lstat) (const char *, struct stat *);
  158. int (*gl_stat) (const char *, struct stat *);
  159. } glob_t;
  160. //
  161. // -------------------------------------------------------------------- Globals
  162. //
  163. //
  164. // -------------------------------------------------------- Function Prototypes
  165. //
  166. LIBC_API
  167. int
  168. glob (
  169. const char *Pattern,
  170. int Flags,
  171. int (*ErrorFunction) (const char *, int),
  172. glob_t *Glob
  173. );
  174. /*++
  175. Routine Description:
  176. This routine is a pathname generator that will expand a pattern out to all
  177. matching path names.
  178. Arguments:
  179. Pattern - Supplies a null terminated string containing the pattern to
  180. match.
  181. Flags - Supplies a bitfield of flags governing the operation. See GLOB_*
  182. definitions.
  183. ErrorFunction - Supplies an optional pointer to an error function that is
  184. called if a directory cannot be read. It receives the path that failed,
  185. and the error number set by the operation. If this routine returns
  186. non-zero, the GLOB_ERR flag is set in the flags, and this routine stops
  187. and returns GLOB_ABORTED after setting gl_pathc and gl_pathv to
  188. reflect the paths already scanned. If the routine returns 0, the error
  189. is ignored.
  190. Glob - Supplies a pointer to the state where paths are returned.
  191. Return Value:
  192. 0 on success. The gl_pathc and gl_pathv members will be filled out with the
  193. number of matches.
  194. Returns one of the GLOB_* return values on failure.
  195. --*/
  196. LIBC_API
  197. void
  198. globfree (
  199. glob_t *Glob
  200. );
  201. /*++
  202. Routine Description:
  203. This routine frees allocated data inside of a glob state structure.
  204. Arguments:
  205. Glob - Supplies a pointer to the state to free.
  206. Return Value:
  207. None.
  208. --*/
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. #endif