void execute(char *cmd) { /* Arguments: *cmd --- the program to run Purpose: this function takes a string and forks off a new process with the global args array this will first check to see if aliases exist for the command. check_alias will deal with changing arg[0] if an alias exists, so always call execvp with arg[0] (instead of cmd arg) in case check_alias changes the cmd it will then check to see if the cmd is a built in command. if it is, then this doesnt fork any process */ int i; check_alias(cmd); i = check_builtin(args[0]); if(!i) { return;} //cmd was a built in (like "cd") if(fork() == 0) { //now in child process check_redir(); //check for redirections i = execvp(args[0], args); //using execvp to avoid PATH crap if(i < 0) { printf("-fash: %s: command not found\n", cmd); //error, we'll just say not found exit(1); } } else { handleSig = 1; wait(NULL); } handleSig = 0; }
void pipe_execute(char *cmd, int readWrite) { /* Arguments: *cmd --- the program to run *fds --- array of two file descriptors for pipe readWrite --- 0 = read, 1 = write, 2 = both Purpose: this function takes a string and forks off a new process with the global args array this will first check to see if aliases exist for the command. check_alias will deal with changing arg[0] if an alias exists, so always call execvp with arg[0] (instead of cmd arg) in case check_alias changes the cmd it will then check to see if the cmd is a built in command. if it is, then this doesnt fork any process */ int i; check_alias(cmd); i = check_builtin(args[0]); if(!i) { return;} //cmd was a built in (like "cd") if(fork() == 0) { //in child check_redir(); int newfd, readfd, writefd; if(readWrite == 2){ //this is a middle command, no closing, dup both stdin and stdout readfd = open("pipe", O_RDONLY); writefd = open("pipe_middle", O_WRONLY | O_CREAT, S_IRWXU | S_IRGRP | S_IROTH); close(0); dup(readfd); close(1); dup(writefd); }else if (readWrite == 1){ //first command, purely writing newfd = open("pipe", O_WRONLY | O_CREAT, S_IRWXU | S_IRGRP | S_IROTH); dup2(newfd,1); close(newfd); }else{ //last command, purely reading newfd = open("pipe", O_RDONLY); close(0); dup(newfd); close(newfd); } i = execvp(args[0], args); if(i < 0) { printf("-fash: %s: command not found\n", cmd); //error, we'll just say not found exit(1); } if(readWrite == 0){ close(0);close(newfd);} else if(readWrite ==1){close(1);close(newfd);} else{ close(0);close(1);close(readfd);close(writefd);system("mv pipe_middle pipe");} } else {wait(NULL);} //wait for everyone }
int main(void) { char line[MAXLINE]; int i; char ** arglist; struct sigaction act; signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGTSTP, SIG_IGN); act.sa_handler = child_handler; sigfillset(&act.sa_mask); act.sa_flags = SA_RESTART; sigaction(SIGCHLD,&act,NULL); fputs("minishell> ",stdout); while(fgets(line,MAXLINE,stdin)) { if(!strncmp(line,"exit",4)) { exit(0); } if(arglist=command_parse(line)) { if(!check_builtin(arglist)) { // for(i=0;arglist[i];i++) // { // printf("[%d] : %s\n", i,arglist[i]); exec_command(arglist); // } } command_freelist(arglist); } fputs("minishell> ",stdout); } return 0; }
int main(int argc, char *argv[]) { read_history(history_file); char* line_read = (char *)NULL; while (true) { const std::string prompt = "$ "; if (line_read) { free(line_read); line_read = (char *)NULL; } line_read = readline(prompt.c_str()); if (line_read && *line_read) add_history(line_read); std::string command(line_read); clear_line(command); int result = check_builtin(command); if (result == 0) { break; } else if (result == -1) { /* command is not builtin */ pid_t pid = fork(); /* code of parent process */ if (pid != 0) { int status = 0; waitpid(pid, &status, 0); /* code of child process */ } else { if (command.find('|') != std::string::npos) { make_pipe(command); } else { Command cmd(command); execvp(cmd.cmd, cmd.params); } } } } write_history(history_file); free(line_read); return 0; }
int why_im_stupid(char **ftab, char **env) { char **tab; int i; i = 0; while (ftab[i]) { if ((check_redirection(ftab[i], env) == 0)) { if (ftab[i + 1] != NULL) i++; else return (0); } tab = str_to_word_tab(ftab[i], ' '); if ((check_builtin(tab, env)) == 1) if ((cmd(tab, env)) == 1) return (0); i++; } return (0); }