Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Coded by Jeffrey Garcia
- // Everything should work fine, fully supports cd
- #include <stdio.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/wait.h>
- void parseline(char *line, char **args) {
- while(*line != '\0') {
- while(*line == ' ' || *line == '\n' || *line == '\t')
- *line++ = '\0';
- *args++ = line;
- while(*line != ' ' && *line != '\n' && *line != '\t' &&
- *line != '\0')
- line++;
- }
- *args--;
- *args = '\0';
- }
- void exec(char **args) {
- int pid;
- if((pid = fork()) < 0) {
- printf( "The fork failed.\n");
- exit(1);
- }
- else if(pid > 0) {
- if((strcmp(args[0], "cd") == 0)) { // || strcmp(args[0], "chdir") == 0 || strcmp(args[0], "dir") == 0)) {
- if(!(chdir(args[1])==0)) printf("chdir failed.\n");
- }
- wait(NULL);
- }
- else if(pid == 0) {
- if((strcmp(args[0], "cd") == 0)) exit(0); // || strcmp(args[0], "chdir") == 0 || strcmp(args[0], "dir") == 0)) {
- else if(execvp(args[0], args) < 0) {
- printf("Exec failed.\n");
- exit(1);
- }
- }
- }
- int main() {
- char buffer[256], *cptr;
- char *args[64];
- while(1) {
- printf("[jeffshell]: ");
- cptr = fgets(buffer, 256, stdin);
- parseline(cptr, args);
- if(strcmp(args[0], "exit") == 0) exit(0);
- exec(args);
- }
- return(0);
- }
Add Comment
Please, Sign In to add comment