stabbish

:)
git clone https://git.sr.ht/~ashymad/stabbish
Log | Files | Refs | README | LICENSE

utils.c (887B)


      1 #define _GNU_SOURCE
      2 
      3 #include <stdarg.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <sys/wait.h>
      7 #include <unistd.h>
      8 
      9 #include "utils.h"
     10 
     11 char *pasprintf(string __restrict str, ...) {
     12   va_list ap;
     13   va_start(ap, str);
     14 
     15   char *out;
     16   if (vasprintf(&out, str, ap) == -1)
     17     out = NULL;
     18 
     19   va_end(ap);
     20   return out;
     21 }
     22 
     23 int memexecv(fd exe, char **argv, char **envp, struct stdstream std) {
     24   int try_return = -1;
     25   pid_t pid = try(fork());
     26 
     27   if (pid) {
     28     int wstatus;
     29     waitpid(pid, &wstatus, 0);
     30     if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) {
     31       fprintf(stderr, "Child process exited with code %i\n",
     32               WEXITSTATUS(wstatus));
     33       return -1;
     34     }
     35   } else {
     36     if (std.in >= 0)
     37       dup2(std.in, 0);
     38     if (std.out >= 0)
     39       dup2(std.out, 1);
     40     if (std.err >= 0)
     41       dup2(std.err, 2);
     42 
     43     exit(fexecve(exe, argv, envp));
     44   }
     45 
     46   return 0;
     47 }