int main(int argc, char **argv) { int cas; int cur; scanf("%d", &num_case); getchar(); gets(str); for (cas = 0; cas < num_case; cas++) { memset(mem, 0, sizeof(mem)); memset(reg, 0, sizeof(reg)); cur = 0; while (gets(str)) { if (!(strlen(str))) { break; } mem[cur++] = atoi(str); } do_exe(); printf("%d\n", count); if (cas < num_case - 1) { printf("\n"); } } return 0; }
void do_exe (int no_cmd,char * * * argv_list, int * argc_list, int index) { int pid,status,new_index = index+1; int fds[2]; int i,t; if(index == no_cmd) return; if(pipe(fds) <0) { perror("pipe error"); exit(1); } pid = fork(); if (pid == -1) { perror ("fork error\n"); exit(1); } else if (pid == 0) { //child process... if(dup2(fds[1],1) <0) { perror("dup2 error\n"); exit(1); } close(fds[0]); if(index ==0) { int fd,k,fl=0; char ** cmd; // for first command check for < i=0; for(t=0;t<=argc_list[i];t++) { char * tt=NULL; tt = strchr(argv_list[i][t],'<'); //if '<' symbol found if(tt != NULL) { //perror("< found"); char * file_name; if(strlen(argv_list[i][t]) != 1) { file_name = (char*)malloc(sizeof(char)*strlen(tt)); strcpy(file_name,&tt[1]); } else { file_name = (char*)malloc(sizeof(char)*(strlen(argv_list[i][t+1])+1)); strcpy(file_name,argv_list[i][t+1]); } printf("%s", file_name); fflush(stdout); fd = open(file_name,O_RDONLY); if(fd <0 ) { perror("fd fails");} //dup2(0,temp_stdin); if(dup2(fd,0) != 0) { perror("dup2- file rediraction :"); exit(1); } cmd = (char**)calloc(t+1,sizeof(char*)); for(k =0; k<t ; k++) { cmd[k] = (char*) calloc(strlen(argv_list[i][k])+1,sizeof(char)); strcpy(cmd[k],argv_list[i][k]); } cmd[t] = 0; fl=1; break; } } if(fl) { if(execvp(argv_list[0][0], cmd) <0) { perror("execvp error:"); exit(1); } } else { if(execvp(argv_list[0][0], argv_list[0]) <0) { perror("execvp error:"); exit(1); } } } if(execvp(argv_list[index][0], argv_list[index]) < 0) { perror("execvp error:"); exit(1); } } else { //parent process... int fd1; if(dup2(fds[0],0)<0) { perror("dup2 error\n"); exit(1); } close(fds[1]); // Recurrsive executor call... do_exe(no_cmd,argv_list,argc_list,new_index); /*------------------------ This code is working for 2-3 command with > , but giving segmentation fault for larg string for inputs // for last command check for > if(new_index=no_cmd) { // for last command check for > i=no_cmd; for(t=0;t<=argc_list[i];t++) { char * tt=NULL; tt = strchr(argv_list[i][t],'>'); //if '<' symbol found if(tt != NULL) { char * file_name; //perror("hhhh"); //strcpy(argv_list[i][t],NULL); if(strlen(argv_list[i][t]) != 1) { file_name = (char*)malloc(sizeof(char)*strlen(tt)); strcpy(file_name,&tt[1]); } else { file_name = (char*)malloc(sizeof(char)*(strlen(argv_list[i][t+1])+1)); strcpy(file_name,argv_list[i][t+1]); //strcpy(argv_list[i][t+1],NULL); } fd1 = open(file_name,O_CREAT|O_WRONLY, 0644); //dup2(1,temp_stdout); if(fd1 < 0 ) { perror("fd1 : hi hi");} if(dup2(fd1,1) != 1) { perror("dup2- file rediraction :"); exit(1); } cmd = (char**)calloc(t+1,sizeof(char*)); for(k =0; k<t ; k++) { cmd[k] = (char*) calloc(strlen(argv_list[i][k])+1,sizeof(char)); strcpy(cmd[k],argv_list[i][k]); } cmd[t] = 0; fl =1; break; } } if (fl) { if(execvp(argv_list[new_index][0],cmd) <0) { perror("execvp error:"); exit(1); } } else { if(execvp(argv_list[new_index][0], argv_list[new_index]) <0) { perror("execvp error:"); exit(1); } } }*/ //-----------------------------------end of > logic // for last command check for > if(new_index=no_cmd) { } if(execvp(argv_list[new_index][0], argv_list[new_index]) <0) { perror("execvp error:"); exit(1); } } }
int cmd_executor(int no_cmd, char * * * argv_list, int * argc_list) { int i,t,fd,fd1,pid,status,k,fl=0; int background_flag =0; char * * cmd; // for last command check for & if(argv_list[no_cmd][argc_list[no_cmd]] == "&") { background_flag =1; } ///////////////////////////////////////////////////////////// pid = fork(); if(pid < 0 ) { perror("fork failed..."); exit(1); } else if(pid == 0) { //perror("hi hi hi"); if(no_cmd == 0) { //perror("hi hi"); /*const int argc= argc_list[0]+1; char * cmd[argc]; for(t =0; t<=argc ; t++) { strcpy(cmd[t],argv_list[0][t]); } cmd[t] = 0; */ // for last command check for > i=no_cmd; for(t=0;t<=argc_list[i];t++) { char * tt=NULL; tt = strchr(argv_list[i][t],'>'); //if '<' symbol found if(tt != NULL) { char * file_name; //perror("hhhh"); //strcpy(argv_list[i][t],NULL); if(strlen(argv_list[i][t]) != 1) { file_name = (char*)malloc(sizeof(char)*strlen(tt)); strcpy(file_name,&tt[1]); } else { file_name = (char*)malloc(sizeof(char)*(strlen(argv_list[i][t+1])+1)); strcpy(file_name,argv_list[i][t+1]); //strcpy(argv_list[i][t+1],NULL); } fd1 = open(file_name,O_CREAT|O_WRONLY, 0644); //dup2(1,temp_stdout); if(fd1 < 0 ) { perror("fd1 : hi hi");} if(dup2(fd1,1) != 1) { perror("dup2- file rediraction :"); exit(1); } cmd = (char**)calloc(t+1,sizeof(char*)); for(k =0; k<t ; k++) { cmd[k] = (char*) calloc(strlen(argv_list[i][k])+1,sizeof(char)); strcpy(cmd[k],argv_list[i][k]); } cmd[t] = 0; fl =1; break; } } // for first command check for < i=0; for(t=0;t<=argc_list[i];t++) { char * tt=NULL; tt = strchr(argv_list[i][t],'<'); //if '<' symbol found if(tt != NULL) { //perror("< found"); char * file_name; if(strlen(argv_list[i][t]) != 1) { file_name = (char*)malloc(sizeof(char)*strlen(tt)); strcpy(file_name,&tt[1]); } else { file_name = (char*)malloc(sizeof(char)*(strlen(argv_list[i][t+1])+1)); strcpy(file_name,argv_list[i][t+1]); } printf("%s", file_name); fflush(stdout); fd = open(file_name,O_RDONLY); if(fd <0 ) { perror("fd fails");} //dup2(0,temp_stdin); if(dup2(fd,0) != 0) { perror("dup2- file rediraction :"); exit(1); } cmd = (char**)calloc(t+1,sizeof(char*)); for(k =0; k<t ; k++) { cmd[k] = (char*) calloc(strlen(argv_list[i][k])+1,sizeof(char)); strcpy(cmd[k],argv_list[i][k]); } cmd[t] = 0; fl=1; break; } } if(fl) { if(execvp(argv_list[0][0], cmd) <0) { perror("execvp error:"); exit(1); } } else { if(execvp(argv_list[0][0], argv_list[0]) <0) { perror("execvp error:"); exit(1); } } } else { //perror("hi hi hi"); // First time executor call with 0 index do_exe(no_cmd,argv_list,argc_list,0); } } else { if(!background_flag) wait(&status); } return 0; }