runit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

pathexec_run.c (1060B)


      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,const char * const *argv,const 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 }