This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
d4b453c39d5d513f74152a443c3f29ee05b0578b
[perl5.git] / ext / File-Glob / Glob.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "bsd_glob.h"
6
7 #define MY_CXT_KEY "File::Glob::_guts" XS_VERSION
8
9 typedef struct {
10     int         x_GLOB_ERROR;
11 } my_cxt_t;
12
13 START_MY_CXT
14
15 #define GLOB_ERROR      (MY_CXT.x_GLOB_ERROR)
16
17 #include "const-c.inc"
18
19 #ifdef WIN32
20 #define errfunc         NULL
21 #else
22 static int
23 errfunc(const char *foo, int bar) {
24   return !(bar == EACCES || bar == ENOENT || bar == ENOTDIR);
25 }
26 #endif
27
28 MODULE = File::Glob             PACKAGE = File::Glob
29
30 int
31 GLOB_ERROR()
32     PREINIT:
33         dMY_CXT;
34     CODE:
35         RETVAL = GLOB_ERROR;
36     OUTPUT:
37         RETVAL
38
39 void
40 doglob(pattern,...)
41     char *pattern
42 PROTOTYPE: $;$
43 PREINIT:
44     glob_t pglob;
45     int i;
46     int retval;
47     int flags = 0;
48     SV *tmp;
49 PPCODE:
50     {
51         dMY_CXT;
52         dXSI32;
53
54         /* allow for optional flags argument */
55         if (items > 1) {
56             flags = (int) SvIV(ST(1));
57         } else if (ix) {
58             flags = (int) SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
59         }
60
61         /* call glob */
62         retval = bsd_glob(pattern, flags, errfunc, &pglob);
63         GLOB_ERROR = retval;
64
65         /* return any matches found */
66         EXTEND(sp, pglob.gl_pathc);
67         for (i = 0; i < pglob.gl_pathc; i++) {
68             /* printf("# bsd_glob: %s\n", pglob.gl_pathv[i]); */
69             tmp = newSVpvn_flags(pglob.gl_pathv[i], strlen(pglob.gl_pathv[i]),
70                                  SVs_TEMP);
71             TAINT;
72             SvTAINT(tmp);
73             PUSHs(tmp);
74         }
75
76         bsd_globfree(&pglob);
77     }
78
79 BOOT:
80 {
81     CV *cv = newXS("File::Glob::bsd_glob", XS_File__Glob_doglob, __FILE__);
82     XSANY.any_i32 = 1;
83 }
84
85 BOOT:
86 {
87     MY_CXT_INIT;
88 }
89
90 INCLUDE: const-xs.inc