xref: /plan9-contrib/sys/src/ape/lib/ap/plan9/system.c (revision 7c6618bf6b2c83b7dd9aa2962a3d6065a08b854b)
1 #include "lib.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/wait.h>
5 #include <unistd.h>
6 
7 int
system(const char * s)8 system(const char *s)
9 {
10 	int w, status;
11 	pid_t pid;
12 	char cmd[30], *oty;
13 
14 	oty = getenv("cputype");
15 	if(!oty)
16 		return -1;
17 	if(!s)
18 		return 1; /* a command interpreter is available */
19 	pid = fork();
20 	snprintf(cmd, sizeof cmd, "/%s/bin/ape/sh", oty);
21 	if(pid == 0) {
22 		execl(cmd, "sh", "-c", s, NULL);
23 		_exit(1);
24 	}
25 	if(pid < 0){
26 		_syserrno();
27 		return -1;
28 	}
29 	for(;;) {
30 		w = wait(&status);
31 		if(w == -1 || w == pid)
32 			break;
33 	}
34 
35 	if(w == -1){
36 		_syserrno();
37 		return w;
38 	}
39 	return status;
40 }
41