1 /* ISC license. */
2
3 #include <errno.h>
4 #include <glob.h>
5 #include <string.h>
6 #include <skalibs/sgetopt.h>
7 #include <skalibs/strerr2.h>
8 #include <skalibs/stralloc.h>
9 #include <skalibs/genalloc.h>
10 #include <execline/execline.h>
11 #include "exlsn.h"
12
elgloberrfunc(char const * s,int e)13 static int elgloberrfunc (char const *s, int e)
14 {
15 errno = e ;
16 strerr_warnw2sys("while globbing, error reading ", s) ;
17 return 0 ;
18 }
19
exlsn_elglob(int argc,char const ** argv,char const * const * envp,exlsn_t * info)20 int exlsn_elglob (int argc, char const **argv, char const *const *envp, exlsn_t *info)
21 {
22 glob_t pglob ;
23 subgetopt_t localopt = SUBGETOPT_ZERO ;
24 elsubst_t blah ;
25 int flags = GLOB_NOSORT | GLOB_NOCHECK ;
26 unsigned int i = 0 ;
27 int verbose = 0 ;
28 blah.var = info->vars.len ;
29 blah.value = info->values.len ;
30 for (;;)
31 {
32 int opt = subgetopt_r(argc, argv, "vwsme0", &localopt) ;
33 if (opt < 0) break ;
34 switch (opt)
35 {
36 case 'v' : verbose = 1 ; break ;
37 case 'w' : flags |= GLOB_ERR ; break ;
38 case 's' : flags &= ~GLOB_NOSORT ; break ;
39 case 'm' : flags |= GLOB_MARK ; break ;
40 case 'e' : flags |= GLOB_NOESCAPE ; break ;
41 case '0' : flags &= ~GLOB_NOCHECK ; break ;
42 default : return -3 ;
43 }
44 }
45 argc -= localopt.ind ; argv += localopt.ind ;
46
47 if (argc < 2) return -3 ;
48 if (!*argv[0] || el_vardupl(argv[0], info->vars.s, info->vars.len)) return -2 ;
49 if (!stralloc_catb(&info->vars, argv[0], strlen(argv[0]) + 1)) return -1 ;
50
51 pglob.gl_offs = 0 ;
52 switch (glob(argv[1], flags, verbose ? &elgloberrfunc : 0, &pglob))
53 {
54 case 0 : break ;
55 case GLOB_NOMATCH:
56 {
57 pglob.gl_pathc = 0 ;
58 pglob.gl_pathv = 0 ;
59 break ;
60 }
61 default: goto err ;
62 }
63 for ( ; i < (unsigned int)pglob.gl_pathc ; i++)
64 if (!stralloc_catb(&info->values, pglob.gl_pathv[i], strlen(pglob.gl_pathv[i]) + 1))
65 goto globerr ;
66 blah.n = pglob.gl_pathc ;
67 globfree(&pglob) ;
68 if (!genalloc_append(elsubst_t, &info->data, &blah)) goto err ;
69 (void)envp ;
70 return localopt.ind + 2 ;
71
72 globerr:
73 globfree(&pglob) ;
74 err:
75 info->vars.len = blah.var ;
76 info->values.len = blah.value ;
77 return -1 ;
78 }
79