scc

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

posix.c (769B)


      1 #include <sys/stat.h>
      2 #include <sys/wait.h>
      3 #include <unistd.h>
      4 
      5 #include <errno.h>
      6 #include <string.h>
      7 
      8 #include "make.h"
      9 
     10 time_t
     11 stamp(char *name)
     12 {
     13 	struct stat st;
     14 
     15 	if (stat(name, &st) < 0)
     16 		return -1;
     17 
     18 	return st.st_mtime;
     19 }
     20 
     21 int
     22 launch(char *cmd, int ignore)
     23 {
     24 	int st;
     25 	pid_t pid;
     26 	char *name, *shell;
     27 	char *args[] = {NULL, "-ec" , cmd, (char *) 0};
     28 	extern char **environ;
     29 
     30 	switch (pid = fork()) {
     31 	case -1:
     32 		return -1;
     33 	case 0:
     34 		shell = getmacro("SHELL");
     35 
     36 		if (ignore)
     37 			args[1] = "-c";
     38 		if ((name = strrchr(shell, '/')) != NULL)
     39 			++name;
     40 		else
     41 			name = shell;
     42 		args[0] = name;
     43 		execve(shell, args, environ);
     44 		_exit(127);
     45 	default:
     46 		while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
     47                         ;
     48                 return st;
     49 	}
     50 }