Last active
September 23, 2023 14:17
-
-
Save viruskizz/7540a9d156afe081e73d67df5b941066 to your computer and use it in GitHub Desktop.
42Bangkok Exam Rank04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
static void perr(char *s) { | |
while (*s) | |
write(2, s++, 1); | |
} | |
static int cd(char **argv, int i) { | |
if (i != 2) | |
return (perr("error: cd: bad arguments\n"), 1); | |
else if (chdir(argv[1]) == -1) | |
return (perr("error: cd: cannot change directory to "), perr(argv[1]), perr("\n"), 1); | |
return 0; | |
} | |
static int exec(char **argv, char **envp, int i) { | |
int status; | |
int fds[2]; | |
int pip = (argv[i] && !strcmp(argv[i], "|")); | |
if (pip && pipe(fds) == -1) | |
return (perr("error: fatal\n"), 1); | |
int pid = fork(); | |
if (!pid) { | |
argv[i] = 0; | |
if (pip && (dup2(fds[1], 1) == -1 || close(fds[0]) == -1 || close(fds[1]) == -1)) | |
return (perr("error: fatal\n"), 1); | |
execve(*argv, argv, envp); | |
return (perr("error: cannot execute "), perr(*argv), perr("\n"), 1); | |
} | |
waitpid(pid, &status, 0); | |
if (pip && (dup2(fds[0], 0) == -1 || close(fds[0]) == -1 || close(fds[1]) == -1)) | |
return (perr("error: fatal\n"), 1); | |
return WIFEXITED(status) && WEXITSTATUS(status); | |
} | |
int main(int argc, char **argv, char **envp) { | |
(void) argc; | |
int status = 0; | |
while(*argv && *(argv + 1)) { | |
argv++; | |
int i = 0; | |
while (argv[i] && strcmp(argv[i], "|") && strcmp(argv[i], ";")) | |
i++; | |
if (!strcmp(*argv, "cd")) | |
status = cd(argv, i); | |
else if (i) | |
status = exec(argv, envp, i); | |
argv += i; | |
} | |
return (status); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
int fd; | |
static void perr(char *s) { | |
while (*s) | |
write(2, s++, 1); | |
} | |
static int cd(char **argv, int i) { | |
if (i != 2) | |
return (perr("error: cd: bad arguments\n"), 1); | |
else if (chdir(argv[1]) == -1) | |
return (perr("error: cd: cannot change directory to "), perr(argv[1]), perr("\n"), 1); | |
return 0; | |
} | |
static int exec(char **argv, char **envp, int i) { | |
int status; | |
int fds[2]; | |
int pip = (argv[i] && !strcmp(argv[i], "|")); | |
if (pip && pipe(fds) == -1) | |
return (perr("error: fatal\n"), 1); | |
int pid = fork(); | |
if (!pid) { | |
argv[i] = 0; | |
if (dup2(fd, 0) == -1 || close(fd) == -1) | |
return (perr("error: fatal\n"), 1); | |
if (pip && (dup2(fds[1], 1) == -1 || close(fds[0]) == -1 || close(fds[1]) == -1)) | |
return (perr("error: fatal\n"), 1); | |
execve(*argv, argv, envp); | |
return (perr("error: cannot execute "), perr(*argv), perr("\n"), 1); | |
} | |
waitpid(pid, &status, 0); | |
if (!pip && dup2(0, fd) == -1) | |
return (perr("error: fatal\n"), 1); | |
if (pip && (dup2(fds[0], fd) == -1 || close(fds[0]) == -1 || close(fds[1]) == -1)) | |
return (perr("error: fatal\n"), 1); | |
return WIFEXITED(status) && WEXITSTATUS(status); | |
} | |
int main(int argc, char **argv, char **envp) { | |
(void) argc; | |
int status = 0; | |
fd = dup(0); | |
while(*argv && *(argv + 1)) { | |
argv++; | |
int i = 0; | |
while (argv[i] && strcmp(argv[i], "|") && strcmp(argv[i], ";")) | |
i++; | |
if (!strcmp(*argv, "cd")) | |
status = cd(argv, i); | |
else if (i) | |
status = exec(argv, envp, i); | |
argv += i; | |
} | |
if (dup2(0, fd) == -1) | |
perr("error: fatal\n"); | |
return (status); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment