scc

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

system.c (482B)


      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 extern const char **_environ;
     13 
     14 int
     15 system(const char *cmd)
     16 {
     17 	int st;
     18 	pid_t pid;
     19 
     20 	if (!cmd)
     21 		return _access(SHELL, X_OK);
     22 
     23 	switch ((pid = _fork())) {
     24 	case -1:
     25 		return -1;
     26 	case 0:
     27 		_execve(SHELL, (const char*[]) {"sh", "-c", cmd, NULL}, _environ);
     28 		_exit(127);
     29 	default:
     30 		while (_waitpid(pid, &st, 0) < 0 && errno == EINTR)
     31 			;
     32 		return st;
     33 	}
     34 }