home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / glob / glob.txh < prev    next >
Encoding:
Text File  |  1996-08-31  |  6.5 KB  |  215 lines

  1. @node glob, shell
  2. @subheading Syntax
  3.  
  4. @example
  5. #include <glob.h>
  6.  
  7. int  glob(const char *pattern, int flags,
  8.           int (*errfunc)(const char *epath, int eerrno), glob_t *pglob);
  9. @end example
  10.  
  11. @subheading Description
  12.  
  13. This function expands a filename wildcard which is passed as
  14. @var{pattern}.  The pattern may include these special characters:
  15.  
  16. @table @code
  17.  
  18. @item *
  19.  
  20. Matches zero of more characters.
  21.  
  22. @item ?
  23.  
  24. Matches exactly one character (any character).
  25.  
  26. @item [...]
  27.  
  28. Matches one character from a group of characters.  If the first
  29. character is @code{!}, matches any character @emph{not} in the group.  A
  30. group is defined as a list of characters between the brackets,
  31. e.g. @code{[dkl_]}, or by two characters separated by @code{-} to
  32. indicate all characters between and including these two.  For example,
  33. @code{[a-d]} matches @code{a}, @code{b}, @code{c}, or @code{d}, and
  34. @code{[!a-zA-Z0-9]} matches any character that is not alphanumeric. 
  35.  
  36. @item ...
  37.  
  38. Matches all the subdirectories, recursively (VMS aficionados,
  39. rejoice!).
  40.  
  41. @item \
  42.  
  43. Causes the next character to not be treated as special.  For example,
  44. @code{\[} matches a literal @samp{[}.  If @var{flags} includes
  45. @code{GLOB_NOESCAPE}, this quoting is disabled and @samp{\} is handled
  46. as a simple character. 
  47.  
  48. @end table
  49.  
  50. The variable @var{flags} controls certain options of the expansion
  51. process.  Possible values for @var{_flags} are as follows:
  52.  
  53. @table @code
  54.  
  55. @item GLOB_APPEND
  56.  
  57. Append the matches to those already present in the array
  58. @code{pglob->gl_pathv}.  By default, @code{glob} discards all previous
  59. contents of @code{pglob->gl_pathv} and allocates a new memory block for
  60. it.  If you use @code{GLOB_APPEND}, @code{pglob} should point to a
  61. structure returned by a previous call to @code{glob}.
  62.  
  63. @item GLOB_DOOFFS
  64.  
  65. Skip @code{pglob->gl_offs} entries in @code{gl_pathv} and put new
  66. matches after that point.  By default, @code{glob} puts the new matches
  67. beginning at @code{pglob->gl_pathv[0]}.  You can use this flag both with
  68. @code{GLOB_APPEND} (in which case the new matches will be put after the
  69. first @code{pglob->gl_offs} matches from previous call to @code{glob}),
  70. or without it (in which case the first @code{pglob->gl_offs} entries in
  71. @code{pglob->gl_pathv} will be filled by @code{NULL} pointers).
  72.  
  73. @item GLOB_ERR
  74.  
  75. Stop when an unreadable directory is encountered and call user-defined
  76. function @var{errfunc}.  This cannot happen under DOS (and thus
  77. @var{errfunc} is never used).
  78.  
  79. @item GLOB_MARK
  80.  
  81. Append a slash to each pathname that is a directory.
  82.  
  83. @item GLOB_NOCHECK
  84.  
  85. If no matches are found, return the pattern itself as the only match.
  86. By default, @code{glob} doesn't change @code{pglob} if no matches are
  87. found.
  88.  
  89. @item GLOB_NOESCAPE
  90.  
  91. Disable blackslash as an escape character.  By default, backslash quotes
  92. special meta-characters in wildcards described above.
  93.  
  94. @item GLOB_NOSORT
  95.  
  96. Do not sort the returned list.  By default, the list is sorted
  97. alphabetically.  This flag causes the files to be returned in the order
  98. they were found in the directory.
  99.  
  100. @end table
  101.  
  102. Given the pattern and the flags, @code{glob} expands the pattern and
  103. returns a list of files that match the pattern in a structure a pointer
  104. to which is passed via @var{pglob}.  This structure is like this:
  105.  
  106. @example
  107. typedef struct @{
  108.   size_t gl_pathc;
  109.   char **gl_pathv;
  110.   size_t gl_offs;
  111. @} glob_t;
  112. @end example
  113.  
  114. In the structure, the @code{gl_pathc} field holds the number of
  115. filenames in @code{gl_pathv} list; this includes the filenames produced
  116. by this call, plus any previous filenames if @code{GLOB_APPEND} or
  117. @code{GLOB_DOOFFS} were set in @var{flags}.  The list of matches is
  118. returned as an array of pointers to the filenames; @code{gl_pathv} holds
  119. the address of the array.  Thus, the filenames which match the pattern
  120. can be accessed as @code{gl_pathv[0]}, @code{gl_pathv[1]}, etc.  If
  121. @code{GLOB_DOOFFS} was set in @var{flags}, the new matches begin at
  122. offset given by @code{gl_offs}.
  123.  
  124. @subheading Return Value
  125.  
  126. Zero on success, or one of these codes:
  127.  
  128. @table @code
  129.  
  130. @item GLOB_ABORTED
  131.  
  132. Not used in DJGPP implementation.
  133.  
  134. @item GLOB_NOMATCH
  135.  
  136. No files matched the given pattern.
  137.  
  138. @item GLOB_NOSPACE
  139.  
  140. Not enough memory to accomodate expanded filenames.
  141.  
  142. @item GLOB_ERR
  143.  
  144. Never happens on MSDOS, see above.
  145.  
  146. @end table
  147.  
  148. @subheading Notes
  149.  
  150. @code{glob} will not match names of volume labels.
  151.  
  152. On MSDOS, filenames are always matched case-insensitively.  On
  153. filesystems that preserve letter-case in filenames (such as Windows 9x),
  154. matches are case-insensitive unless the pattern includes uppercase
  155. characters.
  156.  
  157. On MSDOS, the list of expanded filenames will be returned in lower case,
  158. if all the characters of the pattern (except those between brackets
  159. [...]) are lower-case; if some of them are upper-case, the expanded
  160. filenames will be also in upper case.  On filesystems that preserve
  161. letter-case in filenames, long filenames are returned as they are found
  162. in the directory entry; DOS-style 8+3 filenames are returned as on MSDOS
  163. (in lower case if the pattern doesn't include any upper-case letters, in
  164. upper case otherwise).
  165.  
  166. When the environment variable @samp{LFN} is set to @kbd{n}, @code{glob}
  167. behaves on Windows 9x exactly as it does on MSDOS.
  168.  
  169. Setting the environment variable @samp{FNCASE} to @kbd{y}, or setting
  170. the @code{_CRT0_FLAG_PRESERVE_FILENAME_CASE} bit in the
  171. @code{_crt0_startup_flags} variable (@pxref{_crt0_startup_flags})
  172. suppresses any letter-case conversions in filenames and forces
  173. case-sensitive filename matching.  @xref{_preserve_fncase}.
  174.  
  175. @subheading Example
  176.  
  177. @example
  178.  
  179. #include <stdlib.h>
  180. #include <string.h>
  181. #include <glob.h>
  182.  
  183. /* Convert a wildcard pattern into a list of blank-separated
  184.    filenames which match the wildcard.  */
  185.  
  186. char * glob_pattern(char *wildcard)
  187. @{
  188.   char *gfilename;
  189.   size_t cnt, length;
  190.   glob_t glob_results;
  191.   char **p;
  192.  
  193.   glob(wildcard, GLOB_NOCHECK, 0, &glob_results);
  194.  
  195.   /* How much space do we need?  */
  196.   for (p = glob_results.gl_pathv, cnt = glob_results.gl_pathc;
  197.        cnt; p++, cnt--)
  198.     length += strlen(*p) + 1;
  199.  
  200.   /* Allocate the space and generate the list.  */
  201.   gfilename = (char *) calloc(length, sizeof(char));
  202.   for (p = glob_results.gl_pathv, cnt = glob_results.gl_pathc;
  203.        cnt; p++, cnt--)
  204.     @{
  205.       strcat(gfilename, *p);
  206.       if (cnt > 1)
  207.         strcat(gfilename, " ");
  208.     @}
  209.  
  210.   globfree(&glob_results);
  211.   return gfilename;
  212. @}
  213.  
  214. @end example
  215.