Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <wait.h>
- #include <string.h>
- #include <limits.h>
- enum
- {
- P_LEN = 5,
- };
- int
- main(int argc, char **argv)
- {
- if (argc <= 2) {
- return 0;
- }
- char *sep = argv[1];
- for (int i = 2; i < argc; ++i) {
- if (!strcmp(sep, argv[i])) {
- argv[i] = NULL;
- }
- }
- argv[1] = NULL;
- for (int i = argc - 1; i > 1;) {
- int j = i;
- while (argv[j]) {
- --j;
- }
- ++j;
- int arg_cnt = i - j;
- char **args;
- pid_t pid = fork();
- if (pid < 0) {
- _exit(1);
- } else if (!pid) {
- char *fname = argv[j];
- args = calloc(arg_cnt + 2, sizeof(*args));
- if (!args) {
- _exit(1);
- }
- args[0] = fname;
- for (int k = 1; k <= arg_cnt; ++k) {
- args[k] = argv[k + j];
- }
- args[arg_cnt + 1] = NULL;
- execvp(fname, args);
- _exit(1);
- } else {
- int status;
- wait(&status);
- if (!WIFEXITED(status) || WEXITSTATUS(status)) {
- exit(1);
- }
- i = j - 2;
- }
- }
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement