fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
pathexec.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "alloc.h"
3#include "error.h"
4#include "stralloc.h"
5#include "str.h"
6#include "byte.h"
7#include "env.h"
8#include "pathexec.h"
9
17static stralloc plus;
18static stralloc tmp;
19
20int pathexec_env(const char *s,const char *t)
21{
22 if (!s) return 1;
23 if (!stralloc_copys(&tmp,s)) return 0;
24 if (t) {
25 if (!stralloc_cats(&tmp,"=")) return 0;
26 if (!stralloc_cats(&tmp,t)) return 0;
27 }
28 if (!stralloc_0(&tmp)) return 0;
29 return stralloc_cat(&plus,&tmp);
30}
31
33{
34 if (!sa) return 1;
35 return stralloc_cat(&plus,sa);
36}
37
38void pathexec(char * const *argv)
39{
40 char **e;
41 unsigned int elen;
42 unsigned int i;
43 unsigned int j;
44 unsigned int split;
45 unsigned int t;
46
47 if (!stralloc_cats(&plus,"")) return;
48
49 elen = 0;
50 for (i = 0; environ[i]; ++i)
51 ++elen;
52 for (i = 0; i < plus.len; ++i)
53 if (!plus.s[i])
54 ++elen;
55
56 e = (char **) alloc((elen + 1) * sizeof(char *));
57 if (!e) return;
58
59 elen = 0;
60 for (i = 0; environ[i]; ++i)
61 e[elen++] = environ[i];
62
63 j = 0;
64 for (i = 0; i < plus.len; ++i)
65 if (!plus.s[i]) {
66 split = str_chr(plus.s + j,'=');
67 for (t = 0; t < elen; ++t)
68 if (byte_equal(plus.s + j,split,e[t]))
69 if (e[t][split] == '=') {
70 --elen;
71 e[t] = e[elen];
72 break;
73 }
74 if (plus.s[j + split])
75 e[elen++] = plus.s + j;
76 j = i + 1;
77 }
78 e[elen] = 0;
79
80 pathexec_run(*argv,argv,e);
81 alloc_free(e);
82}
83
84void pathexec_run(const char *file,char *const *argv,char *const *envp)
85{
86 char *path;
87 unsigned int split;
88 int savederrno;
89
90 if (file[str_chr(file,'/')]) {
91 execve(file,argv,envp);
92 return;
93 }
94
95 path = env_get("PATH");
96 if (!path) path = "/bin:/usr/bin";
97
98 savederrno = 0;
99 for (;;) {
100 split = str_chr(path,':');
101 if (!stralloc_copyb(&tmp,path,split)) return;
102 if (!split)
103 if (!stralloc_cats(&tmp,".")) return;
104 if (!stralloc_cats(&tmp,"/")) return;
105 if (!stralloc_cats(&tmp,file)) return;
106 if (!stralloc_0(&tmp)) return;
107
108 execve(tmp.s,argv,envp);
109 if (errno != ENOENT) {
110 savederrno = errno;
111 if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR)) return;
112 }
113
114 if (!path[split]) {
115 if (savederrno) errno = savederrno;
116 return;
117 }
118 path += split;
119 path += 1;
120 }
121}
int pathexec_env(const char *s, const char *t)
Definition: pathexec.c:20
void pathexec(char *const *argv)
Definition: pathexec.c:38
int pathexec_multienv(stralloc *sa)
Definition: pathexec.c:32
void pathexec_run(const char *file, char *const *argv, char *const *envp)
Definition: pathexec.c:84
void alloc_free(void *)
Definition: alloc.c:45
void * alloc(unsigned int)
Definition: alloc.c:27
char * env_get(char *)
Definition: env.c:12
char ** environ
#define byte_equal(s, n, t)
Definition: byte.h:18
unsigned int str_chr(const char *, int)
Definition: str.c:81
int stralloc_copyb(stralloc *, const char *, unsigned int)
Definition: stralloc.c:70
int stralloc_cat(stralloc *, stralloc *)
Definition: stralloc.c:21
#define stralloc_0(sa)
Definition: stralloc.h:37
int stralloc_cats(stralloc *, const char *)
Definition: stralloc.c:36
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
size_t len
Definition: stralloc.h:19
char * s
Definition: stralloc.h:18