Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "stdio.h"
- #include "err.h"
- #include "sys/wait.h"
- #include "sysexits.h"
- #include "unistd.h"
- #include "sys/types.h"
- int main() {
- char str[1024], command[1024], argument[1024];
- pid_t pid;
- const char * argv[] = {"", "", NULL};
- printf("shell_jr: ");
- fflush(stdout);
- while(fgets (str, 1024, stdin)){
- sscanf(str, "%s %s", command, argument);
- if(!strcmp(command, "exit") || !strcmp(command, "hastalavista")) {
- printf("See you\n");
- return 0;
- }
- if(strcmp(command, "cd") != 0){
- chdir(argument);
- continue;
- }
- argv[0] = command;
- argv[1] = argument;
- if ((pid = fork()) < 0) {
- err(EX_OSERR, "fork error");
- }
- if (pid) { /* parent code */
- wait(NULL); /* waiting for child to finish */
- } else { /* child code */
- execvp(argv[0], (char * const *)argv);
- fflush(stdout);
- err(EX_OSERR, "Failed to execute %s", command);
- }
- printf("shell_jr: ");
- fflush(stdout);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement