|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <ctype.h> |
| 6 | +#include <sys/wait.h> |
| 7 | + |
| 8 | +#include <fcntl.h> |
| 9 | + |
| 10 | +#include <dirent.h> |
| 11 | + |
| 12 | +static char *trim(char *s) { |
| 13 | + while (isspace(*s)) s++; |
| 14 | + if (*s == 0) return s; |
| 15 | + char *end = s + strlen(s) - 1; |
| 16 | + while (end > s && isspace(*end)) end--; |
| 17 | + end[1] = '\0'; |
| 18 | + return s; |
| 19 | +} |
| 20 | + |
| 21 | +// split a string like "cmd arg1 arg2" into argv[] |
| 22 | +static char **make_argv(char *cmd) { |
| 23 | + int cap = 8, n = 0; |
| 24 | + char **argv = malloc(sizeof(char*) * cap); |
| 25 | + char *tok = strtok(cmd, " \t"); |
| 26 | + while (tok) { |
| 27 | + if (n + 1 >= cap) { |
| 28 | + cap *= 2; |
| 29 | + argv = realloc(argv, sizeof(char*) * cap); |
| 30 | + } |
| 31 | + argv[n++] = tok; |
| 32 | + tok = strtok(NULL, " \t"); |
| 33 | + } |
| 34 | + argv[n] = NULL; |
| 35 | + return argv; |
| 36 | +} |
| 37 | + |
| 38 | +void list_dir(const char *path) { |
| 39 | + DIR *d = opendir(path); |
| 40 | + struct dirent *entry; |
| 41 | + printf("Contents of %s:\n", path); |
| 42 | + while ((entry = readdir(d)) != NULL) { |
| 43 | + printf(" %s\n", entry->d_name); |
| 44 | + } |
| 45 | + closedir(d); |
| 46 | + printf("\n"); |
| 47 | +} |
| 48 | + |
| 49 | +int main(int argc, char **argv) { |
| 50 | + if (argc < 3 || strcmp(argv[1], "-c") != 0) { |
| 51 | + fprintf(stderr, "usage: %s -c \"command\"\n", argv[0]); |
| 52 | + return 1; |
| 53 | + } |
| 54 | + |
| 55 | + int fd = open("etest.wasm", O_WRONLY, 0); |
| 56 | + printf("FD etest: %d\n", fd); |
| 57 | + close(fd); |
| 58 | + |
| 59 | + int nchildren=0; |
| 60 | + |
| 61 | + char *cmd = strdup(argv[2]); // full string |
| 62 | + char *saveptr; |
| 63 | + char *segment = strtok_r(cmd, "&", &saveptr); |
| 64 | + |
| 65 | + while (segment) { |
| 66 | + char *t = trim(segment); |
| 67 | + if (*t) { |
| 68 | + pid_t pid = fork(); |
| 69 | + if (pid == 0) { |
| 70 | + // child: exec command |
| 71 | + char *copy = strdup(t); |
| 72 | + char **cmd_argv = make_argv(copy); |
| 73 | + |
| 74 | + execv(cmd_argv[0], cmd_argv); |
| 75 | + char errbuf[256]; |
| 76 | + snprintf(errbuf, sizeof(errbuf), "execv(%s)", cmd_argv[0]); |
| 77 | + perror(errbuf); |
| 78 | + exit(1); |
| 79 | + } else if (pid < 0) { |
| 80 | + perror("fork"); |
| 81 | + }else { |
| 82 | + nchildren++; |
| 83 | + } |
| 84 | + // parent: do not wait (background) |
| 85 | + } |
| 86 | + segment = strtok_r(NULL, "&", &saveptr); |
| 87 | + } |
| 88 | + |
| 89 | + // behave like bash when all jobs are background: |
| 90 | + // exit immediately, but reap zombies |
| 91 | + for (int i = 0; i < nchildren; i++) { |
| 92 | + int status; |
| 93 | + pid_t w = wait(&status); |
| 94 | + if (w > 0) { |
| 95 | + printf("[mash] terminated pid %d, status %d\n", w, status); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
0 commit comments