scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

system.c (451B)


      1 #include <sys.h>
      2 
      3 #include <errno.h>
      4 #include <stdlib.h>
      5 
      6 #include "../../syscall.h"
      7 
      8 #define SHELL "/bin/sh"
      9 
     10 #undef system
     11 
     12 int
     13 system(const char *cmd)
     14 {
     15 	int st;
     16 	pid_t pid;
     17 
     18 	if (!cmd)
     19 		return _access(SHELL, X_OK);
     20 
     21 	switch ((pid = _fork())) {
     22 	case -1:
     23 		return -1;
     24 	case 0:
     25 		_execve(SHELL, (const char*[]) {"sh", "-c", cmd, NULL}, _environ);
     26 		_exit(127);
     27 	default:
     28 		while (_waitpid(pid, &st, 0) < 0 && errno == EINTR)
     29 			;
     30 		return st;
     31 	}
     32 }