1 /* ISC license. */
2 
3 #include <sys/wait.h>
4 
5 #include <skalibs/sgetopt.h>
6 #include <skalibs/types.h>
7 #include <skalibs/strerr2.h>
8 #include <skalibs/djbunix.h>
9 #include <skalibs/exec.h>
10 
11 #include <execline/execline.h>
12 
13 #define USAGE "if [ -n ] [ -X ] [ -t | -x exitcode ] { command... }"
14 #define dieusage() strerr_dieusage(100, USAGE) ;
15 
main(int argc,char const ** argv,char const * const * envp)16 int main (int argc, char const **argv, char const *const *envp)
17 {
18   int argc1, wstat ;
19   pid_t pid ;
20   int not = 0, fixed = 0, flagnormalcrash = 0 ;
21   unsigned short e = 1 ;
22   PROG = "if" ;
23   {
24     subgetopt_t l = SUBGETOPT_ZERO ;
25     for (;;)
26     {
27       int opt = subgetopt_r(argc, argv, "nXtx:", &l) ;
28       if (opt == -1) break ;
29       switch (opt)
30       {
31         case 'n' : not = 1 ; fixed = 1 ; break ;
32         case 'X' : flagnormalcrash = 1 ; break ;
33         case 't' : e = 0 ; fixed = 1 ; break ;
34         case 'x' : if (!ushort_scan(l.arg, &e)) dieusage() ; fixed = 1 ; break ;
35         default : dieusage() ;
36       }
37     }
38     argc -= l.ind ; argv += l.ind ;
39   }
40   if (e > 255) strerr_dief1x(100, "invalid exit code") ;
41   argc1 = el_semicolon(argv) ;
42   if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ;
43   argv[argc1] = 0 ;
44   pid = el_spawn0(argv[0], argv, envp) ;
45   if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ;
46   if (wait_pid(pid, &wstat) == -1) strerr_diefu1sys(111, "wait_pid") ;
47   if (!flagnormalcrash && WIFSIGNALED(wstat))
48   {
49     char fmt[UINT_FMT] ;
50     fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
51     strerr_dief2x(128 + WTERMSIG(wstat), "child crashed with signal ", fmt) ;
52   }
53   if (not == !wait_estatus(wstat)) return fixed ? e : wait_estatus(wstat) ;
54   xexec0_e(argv+argc1+1, envp) ;
55 }
56