ucspi-ssl  0.99e
TLS encryption for IPv6 communication
pathexec_run.c
Go to the documentation of this file.
1 /* Public domain. */
2 
3 #include <unistd.h>
4 #include "error.h"
5 #include "stralloc.h"
6 #include "str.h"
7 #include "env.h"
8 #include "pathexec.h"
9 
10 static stralloc tmp;
11 
12 void pathexec_run(const char *file,char * const *argv,char * const *envp)
13 {
14  const char *path;
15  unsigned int split;
16  int savederrno;
17 
18  if (file[str_chr(file,'/')]) {
19  execve(file,argv,envp);
20  return;
21  }
22 
23  path = env_get("PATH");
24  if (!path) path = "/bin:/usr/bin";
25 
26  savederrno = 0;
27  for (;;) {
28  split = str_chr(path,':');
29  if (!stralloc_copyb(&tmp,path,split)) return;
30  if (!split)
31  if (!stralloc_cats(&tmp,".")) return;
32  if (!stralloc_cats(&tmp,"/")) return;
33  if (!stralloc_cats(&tmp,file)) return;
34  if (!stralloc_0(&tmp)) return;
35 
36  execve(tmp.s,argv,envp);
37  if (errno != error_noent) {
38  savederrno = errno;
39  if ((errno != error_acces) && (errno != error_perm) && (errno != error_isdir)) return;
40  }
41 
42  if (!path[split]) {
43  if (savederrno) errno = savederrno;
44  return;
45  }
46  path += split;
47  path += 1;
48  }
49 }
int error_acces
Definition: error.c:92
int stralloc_cats(stralloc *, const char *)
Definition: stralloc_cats.c:7
#define stralloc_0(sa)
Definition: stralloc.h:21
unsigned int str_chr(const char *, int)
void pathexec_run(const char *file, char *const *argv, char *const *envp)
Definition: pathexec_run.c:12
int error_noent
Definition: error.c:22
int stralloc_copyb(stralloc *, const char *, unsigned int)
Definition: stralloc_opyb.c:6
char * env_get(const char *s)
Definition: env.c:6
int error_perm
Definition: error.c:85
int error_isdir
Definition: error.c:113