scc

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

posix.c (969B)


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