1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/* ISC license. */
#include <skalibs/bytestr.h>
#include <skalibs/sgetopt.h>
#include <skalibs/env.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#include <skalibs/skamisc.h>
#include <skalibs/uint.h>
#include <execline/execline.h>
#define USAGE "elgetopt optstring prog..."
int main (int argc, char const *const *argv, char const *const *envp)
{
unsigned int n, nbak ;
unsigned int envlen = env_len(envp) ;
stralloc modif = STRALLOC_ZERO ;
char const *x = env_get2(envp, "#") ;
PROG = "elgetopt" ;
if (argc < 3) strerr_dieusage(100, USAGE) ;
if (!x) strerr_dienotset(100, "#") ;
if (!uint0_scan(x, &n)) strerr_dieinvalid(100, "#") ;
nbak = n++ ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
char const *args[n+1] ;
register unsigned int i = 0 ;
for ( ; i < n ; i++)
{
char fmt[UINT_FMT] ;
fmt[uint_fmt(fmt, i)] = 0 ;
args[i] = env_get2(envp, fmt) ;
if (!args[i]) strerr_dienotset(100, fmt) ;
}
args[n] = 0 ;
for (;;)
{
char hmpf[11] = "ELGETOPT_?" ;
register int opt = sgetopt_r(n, args, argv[1], &l) ;
if (opt == -1) break ;
if (opt == '?') return 1 ;
hmpf[9] = opt ;
if (!env_addmodif(&modif, hmpf, l.arg ? l.arg : "1")) goto err ;
}
n -= l.ind ;
for (i = 0 ; i < nbak ; i++)
{
char fmt[UINT_FMT] ;
fmt[uint_fmt(fmt, i+1)] = 0 ;
if (!env_addmodif(&modif, fmt, (i < n) ? args[l.ind + i] : 0)) goto err ;
}
}
{
char fmt[UINT_FMT] ;
fmt[uint_fmt(fmt, n)] = 0 ;
if (!env_addmodif(&modif, "#", fmt)) goto err ;
}
{
char const *const list[1] = { "ELGETOPT_" } ;
char const *v[envlen] ;
if (el_pushenv(&satmp, envp, envlen, list, 1) < 0) goto err ;
if (!env_make(v, envlen, satmp.s, satmp.len)) goto err ;
pathexec_r(argv+2, v, envlen, modif.s, modif.len) ;
strerr_dieexec(111, argv[2]) ;
}
err:
strerr_diefu1sys(111, "update environment") ;
}
|