1 #include "stralloc.h"
2 #include "alloc.h"
3 #include "str.h"
4 #include "byte.h"
5 #include "env.h"
6 #include "pathexec.h"
7
8 static stralloc plus;
9 static stralloc tmp;
10
pathexec_env(char * s,char * t)11 int pathexec_env(char *s,char *t)
12 {
13 if (!s) return 1;
14 if (!stralloc_copys(&tmp,s)) return 0;
15 if (t) {
16 if (!stralloc_cats(&tmp,"=")) return 0;
17 if (!stralloc_cats(&tmp,t)) return 0;
18 }
19 if (!stralloc_0(&tmp)) return 0;
20 return stralloc_cat(&plus,&tmp);
21 }
22
pathexec(char ** argv)23 void pathexec(char **argv)
24 {
25 char *path;
26 char **e;
27 unsigned int elen;
28 unsigned int i;
29 unsigned int j;
30 unsigned int split;
31 unsigned int t;
32
33 if (!stralloc_cats(&plus,"")) return;
34
35 elen = 0;
36 for (i = 0;environ[i];++i)
37 ++elen;
38 for (i = 0;i < plus.len;++i)
39 if (!plus.s[i])
40 ++elen;
41
42 e = (char **) alloc((elen + 1) * sizeof(char *));
43 if (!e) return;
44
45 elen = 0;
46 for (i = 0;environ[i];++i)
47 e[elen++] = environ[i];
48
49 j = 0;
50 for (i = 0;i < plus.len;++i)
51 if (!plus.s[i]) {
52 split = str_chr(plus.s + j,'=');
53 for (t = 0;t < elen;++t)
54 if (byte_equal(plus.s + j,split,e[t]))
55 if (e[t][split] == '=') {
56 --elen;
57 e[t] = e[elen];
58 break;
59 }
60 if (plus.s[j + split])
61 e[elen++] = plus.s + j;
62 j = i + 1;
63 }
64 e[elen] = 0;
65
66 pathexec_run(*argv,argv,e);
67 alloc_free(e);
68 }
69