int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ // pid_t cpid, tcpid, cpgid; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; char *cwd = getcwd(NULL, 0); fprintf(stdout, "%d [%s]:", lineNum, cwd); free(cwd); while ((s = freadln(stdin))){ // printf("%s\n", s); char *s_copy; asprintf(&s_copy, "%s", s); t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { run_program(t, s_copy); } ++lineNum; cwd = getcwd(NULL, 0); fprintf(stdout, "%d [%s]:", lineNum, cwd); free(cwd); } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ init_shell(); char buf[1024]; getcwd(buf, sizeof(buf)); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); fprintf(stdout, "%d:%s$ ", lineNum, buf); lineNum=1; while ((s = freadln(stdin))){ t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { if(t[0]){ cmd_fork(t); } } getcwd(buf, sizeof(buf)); fprintf(stdout, "%d:%s$ ", lineNum, buf); lineNum++; } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ char cwd[MAX_FILE_SIZE+1]; tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); //getcwd(cwd, MAX_FILE_SIZE); lineNum=0; //fprintf(stdout, "%d - %s: ", cwd, lineNum); while ((s = freadln(stdin))){ t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) { cmd_table[fundex].fun(&t[1]); } else{ cmd_exec(t); } fprintf(stdout, "%d %s: ", ++lineNum, get_current_dir_name()); } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; https://onedrive.live.com/about/en-us/# init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; fprintf(stdout, "%d: ", lineNum); while ((s = freadln(stdin))) { t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { //fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); cmd_fork(t); } fprintf(stdout, "%d: ", lineNum); } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; char cwd[5000]; fprintf(stdout, "%d: %s:", lineNum, getcwd(cwd,sizeof(cwd))); while ((s = freadln(stdin))) { t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { pid = fork(); if( pid == 0 ) { // child process // printf("%d\n", strlen(strstr(s,">"))); int i; for(i=0; i<MAXTOKS && t[i]; i++) { if (strcmp( t[i], ">") == 0) { t[i]=NULL; //printf("hello"); part4(t,t[i+1],">"); } if (strcmp( t[i], "<") == 0) { t[i]=NULL; //printf("hello"); part4(t,t[i+1],"<"); } } part3(t); part2(t); } else if(pid<0) { perror( "Fork failed" ); exit( EXIT_FAILURE ); } } lineNum++; wait(NULL); fprintf(stdout, "%d: %s :", lineNum, getcwd(cwd,sizeof(cwd))); } return 0; }
static Obj oparse(FILE* const file) { const int lines = flns(file); const int size = 128; Vertices vsv = vsnew(size); Vertices vsn = vsnew(size); Vertices vst = vsnew(size); Faces fs = fsnew(size); for(int i = 0; i < lines; i++) { Face f; Vertex v; char* line = freadln(file); if(line[0] == 'v' && line[1] == 'n') { if(vsn.count == vsn.max) vsn.vertex = (Vertex*) realloc(vsn.vertex, sizeof(Vertex) * (vsn.max *= 2)); sscanf(line, "vn %f %f %f", &v.x, &v.y, &v.z); vsn.vertex[vsn.count++] = v; } else if(line[0] == 'v' && line[1] == 't') { if(vst.count == vst.max) vst.vertex = (Vertex*) realloc(vst.vertex, sizeof(Vertex) * (vst.max *= 2)); sscanf(line, "vt %f %f %f", &v.x, &v.y, &v.z); vst.vertex[vst.count++] = v; } else if(line[0] == 'v') { if(vsv.count == vsv.max) vsv.vertex = (Vertex*) realloc(vsv.vertex, sizeof(Vertex) * (vsv.max *= 2)); sscanf(line, "v %f %f %f", &v.x, &v.y, &v.z); vsv.vertex[vsv.count++] = v; } else if(line[0] == 'f') { if(fs.count == fs.max) fs.face = (Face*) realloc(fs.face, sizeof(Face) * (fs.max *= 2)); sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", &f.va, &f.ta, &f.na, &f.vb, &f.tb, &f.nb, &f.vc, &f.tc, &f.nc); const Face indexed = { f.va - 1, f.vb - 1, f.vc - 1, f.ta - 1, f.tb - 1, f.tc - 1, f.na - 1, f.nb - 1, f.nc - 1 }; fs.face[fs.count++] = indexed; } free(line); } rewind(file); const Obj obj = { vsv, vsn, vst, fs }; return obj; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; int status = 0; /* tokens parsed from input */ int lineNum = 0;char *fi1; int fundex = -1; int MAX_NUM = 10000; char nyawa[MAX_NUM]; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; fprintf(stdout, "%d: %s:", lineNum, getcwd(nyawa,sizeof(nyawa))); while ((s = freadln(stdin))){ t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { pid_t pid = fork(); if (pid ==-1) { perror("cmd"); exit(1); } else if(pid == 0) { tok_t *fi; char *fi1=getenv("PATH"); fi = getToks(fi1); for(int i = 0;i<MAXTOKS && fi[i];i++){ char *char1 =concat(fi[i],"/"); char1 =concat(char1,t[0]); if(access(char1,F_OK) != -1){ execve(char1,t, NULL); } } execvp(*t,t); perror(*t); exit(0); } else { while ((pid != wait(&status))); } } fprintf(stdout, "%d: %s :",++lineNum,getcwd(nyawa,sizeof(nyawa))); } return 0; }
int shell(int argc, char *argv[]) { char *input_bytes; tok_t *tokens; int line_num = 0; int fundex = -1; init_shell(); if (shell_is_interactive) /* Please only print shell prompts when standard input is not a tty */ fprintf(stdout, "%d: ", line_num); while ((input_bytes = freadln(stdin))) { tokens = get_toks(input_bytes); fundex = lookup(tokens[0]); if (fundex >= 0) { cmd_table[fundex].fun(&tokens[1]); } else { int pid = fork(); if (pid == 0){ int i; for(i = 1;i < MAXTOKS - 1;i++) if(strcmp(">",tokens[i]) == 0){ FILE *f = fopen(tokens[i + 1],"w"); if(f == NULL) { exit(1); } dup2(fileno(f),STDOUT_FILENO); fclose(f); } else if(strcmp("<",tokens[i]) == 0){ FILE *f = fopen(tokens[i + 1],"r"); if(f == NULL) { exit(1); } dup2(fileno(f),fileno(stdin)); fclose(f); } execvp(tokens[0],tokens + 1); exit(0); } } if (shell_is_interactive) /* Please only print shell prompts when standard input is not a tty */ fprintf(stdout, "%d: ", ++line_num); } return 0; }
int shell( int argc, char *argv[] ) { init_shell(); // initialize the shell pid_t pid = getpid(); // get current processe's PID pid_t ppid = getppid(); // get parent's PID pid_t cpid, tcpid, cpgid; print_welcome( argv[0], pid, ppid ); print_prompt(); char *s = malloc(INPUT_STRING_SIZE + 1); // user input string while( (s = freadln(stdin)) ){ // read in the input string process *p = create_process( s ); if( p != NULL ){ // do we need to run a newly created process? cpid = fork(); // fork off a child if( cpid == 0 ) { // child p->pid = getpid(); launch_process( p ); } // if( cpid == 0 ) else if( cpid > 0 ){ // parent p->pid = cpid; if( shell_is_interactive ){ // are we running shell from a terminal? // put 'p' into its own process group // since our shell is not supporting pipes, we treat // each process by itself as a 'job' to be done if( setpgid( cpid, cpid ) < 0 ) printf( "Failed to put the child into its own process group.\n" ); if( p->background ) put_in_background( p, false ); else put_in_foreground( p, false ); } // if( shell_is_interactive ) else wait_for_process( p ); } // else if( cpid > 0 ) else // fork() failed printf( "fork() failed\n" ); } // if( p != NULL ) print_prompt(); } // while return EXIT_SUCCESS; } // shell
/* somehow get scores for user and put them in stats */ void readdatafile(Vector stats, char *user) { FILE *dfile; char *line,*userstr,*moviestr,*scorestr,*found; double score, *scorep; dfile=fopen(DATAFILE,"r"); if (dfile==NULL) return; while ((line=freadln(dfile))!=NULL) { userstr=line; found=strchr(userstr,':'); if (found==NULL) continue; moviestr=&found[1]; found[0]='\0'; found=strchr(moviestr,':'); if (found==NULL) continue; scorestr=&found[1]; found[0]='\0'; if (!strcmp(userstr,user)) { score=safeatof(scorestr); if (score>=0) { scorep=malloc(sizeof(double)); *scorep=score; Vector_add(stats,scorep); } } free(line); } fclose(dfile); }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; init_shell(); //int funt(); //fuct(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); cwd = get_current_dir_name(); fprintf(stdout, "%d %s: ", lineNum, cwd); while ((s = freadln(stdin))){ //strcpy(command,s); t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { cpid = fork(); if(cpid==0){ fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); } wait(NULL); } lineNum++; fprintf(stdout, "%d %s: ", lineNum,cwd); } return 0; }
int shell (int argc, char *argv[]) { char *s=NULL; //= malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; set_dir(); fprintf(stdout, "%d:%s $ ", lineNum,cdir); while ((s = freadln(stdin))) { t = getToks(s); /* break the line into tokens */ //printf("args s: %s\n",s); fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { if(t[0]) { terminal_cmd(t); } //fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); } free(s); freeToks(t); fprintf(stdout, "%d:%s $ ", lineNum,cdir); } return 0; }
int shell (int argc, char *argv[]) { pid_t pid = getpid(); // get current process's PID pid_t ppid = getppid(); // get parent's PID pid_t cpid; // use this to store a child PID char *s = malloc(INPUT_STRING_SIZE+1); // user input string tok_t *t; // tokens parsed from input // if you look in parse.h, you'll see that tokens are just c-style strings int lineNum = 0; int fundex = -1; // perform some initialisation init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; // change this to print the current working directory before each line as well as the line number. fprintf(stdout, "%d: ", lineNum); while ((s = freadln(stdin))){ t = getToks(s); // break the line into tokens fundex = lookup(t[0]); // Is first token a shell literal if(fundex >= 0){ cmd_table[fundex].fun(&t[1]); lineNum++; } else { // replace this statement to call a function that runs executables fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); } // change this to print the current working directory before each line as well as the line number. fprintf(stdout, "%d: ", lineNum); } return 0; }
int shell(int argc, char *argv[]) { tok_t tok_special_indexes[MAXTOKS]; fprintf(stdout, "Welcome to Shelley v1.0\n...\n"); tok_t *t; /* tokens parsed from input */ int fundex = -1, tok_num = 0, bg = 0; pid_t pid = getpid(), /* get current processes PID */ ppid = getppid(), /* get parents PID */ cpid, tcpid, cpgid; init_shell(); PATH = getToks(getenv("PATH"), &tok_num, tok_special_indexes, &bg); do { t = getToks(s, &tok_num, tok_special_indexes, &bg); /* break the line into tokens */ if (t != NULL) { fundex = lookup(t[0]); /* Is first token a shell literal */ if (fundex >= 0) cmd_table[fundex].fun(&t[1]); else { create_process(t, tok_num, tok_special_indexes, bg); freeToks(t); } } prompter(); } while ((s = freadln(stdin))); return cmd_table[lookup("quit")].fun(&t[1]); }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ bool* part4 = false; tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; char cwd[1024]; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; fprintf(stdout, "%d: ", lineNum); fprintf(stdout,"%s ", getcwd(cwd,sizeof(cwd)));//current working directory while ((s = freadln(stdin))){ if(strstr(s,">") != NULL){ part4 = true; } else if(strstr(s,"<") != NULL){ part4 = true; } t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { //char *temp[] = {s,t[1],t[2]};//whole input stream - part 2 //fprintf(stdout,"%d \n\n", part4); if(part4 == false){ char *temp[] = {s,t[1],t[2]}; execute(s,temp); } else if (part4 == true){ pid_t cpid = fork(); if(cpid < 0){ fprintf(stdout,"Failure in creating child proccess: fork()"); exit(0); } else if(cpid == 0){ exePath(t[0],t); exit(1); } wait(cpid); //wait for child proccess to finish //execute(s,temp); // commented out because of part 3 //fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); part4 = false; } } fprintf(stdout, "%d: ", lineNum++); fprintf(stdout,"%s ", getcwd(cwd,sizeof(cwd))); } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1);/* user input string */ tok_t *t,*PP; /* tokens parsed from input */ int lineNum = 0;int star; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ //pid_t cpid, tcpid, cpgid; //int fd; /* new file descriptor */ printf("%s running as PID %d under %d\n",argv[0],pid,ppid); init_shell(); char *Pph; char tshi[1000]; lineNum=0; int output; char *ptrA, *ptrB; ptrA = strstr(s, "<"); ptrB = strstr(s, ">"); fprintf(stdout, "%d: %s: ", lineNum, getcwd(tshi, sizeof(tshi))); while ((s = freadln(stdin))){ //The file end up here t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { pid = fork(); //int current_out,current_in; if (pid < 0) { printf("cmd"); exit(1); } else if (pid == 0){ if (ptrA) { int fd0 = open(argv[0], O_RDONLY | O_CREAT, 0); dup2(fd0, STDIN_FILENO); // fprintf(stdout,"%s",fd0); close(fd0); } if (ptrB) { int fd1 = creat(output, 0644); dup2(fd1, STDOUT_FILENO); close(fd1); } Pph=getenv("PATH"); PP = getToks(Pph); for(int i = 0;i<MAXTOKS && PP[i];i++){ char *mine =concat(PP[i],"/"); mine =concat(mine,t[0]); if(access(mine,F_OK) != -1){ execve(mine,t, NULL); } } execvp(*t, t); perror(*t); exit(0); } else{ while ((pid != wait(&star))); } } fprintf(stdout, "%d: %s: ", ++lineNum, getcwd(tshi, sizeof(tshi))); } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ //Part 3 - Search $Path for command char *path = getenv("PATH"); tok_t *tpath = getToks(path); // pid_t cpid, tcpid, cpgid; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; prompt(++lineNum); // Part 1 - Add current working directory to path (Startup) while ((s = freadln(stdin))){ t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { // Part 2 - ignore if no command entered if (toks_count(t) > 0) { // Part 3 - Check 1st in current folder if ( (access(t[0], F_OK)) == -1) { //Part 3 - Search $Path for command int pcount = toks_count(tpath); char cmd[1024] = ""; int pathfound = -1; // Part 3 - Search PATH for (int i = 0; i < pcount; i++) { char tempcmd[1024] = ""; //Build path for access check strcat(tempcmd, tpath[i]); strcat(tempcmd, "/"); strcat(tempcmd, t[0]); //Check for access at the path if ( (access(tempcmd, F_OK)) != -1) { strcat(cmd, tempcmd); pathfound = 1; break; } } // Part 3 - if command found, update command PATH, else entered command will run if (pathfound > 0) { t[0] = cmd; } } //Part 2 - Start fork exec_fork(t[0], t); } } prompt(++lineNum); // Part 1 - Add current working directory to path } return (0); }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; char dir[200] = ""; init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; getcwd(dir, 200); fprintf(stdout, "%d %s: ", lineNum,dir); while ((s = freadln(stdin))){ t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { pid=fork(); if(pid<0){ printf("sdfa"); } if(pid==0){ //fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); cpid =getpid(); char*p; p=getenv("PATH"); tok_t *to=getToks(p); int i; for(i=0;i < MAXTOKS && to[i];i++) { char *string =concat(to[i],"/"); string = concat(string,t[0]); if ( (access(string, F_OK)) != -1) { execve(string,t,NULL); } } execv(t[0], t); //int bufsize; //int position = 0; //char *buffer = malloc(sizeof(char) * bufsize); //int c; //if (!buffer) { //fprintf(stderr, "lsh: allocation error\n"); //exit(EXIT_FAILURE); //} //while (1) { // Read a character //c = getchar(); // If we hit EOF, replace it with a null character and return. //if (c == EOF || c == '\n') { //buffer[position] = '\0'; //return buffer; //} else { //buffer[position] = c; //} //position++; // If we have exceeded the buffer, reallocate. //if (position >= bufsize) { //bufsize += LSH_RL_BUFSIZE; //buffer = realloc(buffer, bufsize); //} //else if (!buffer) { //fprintf(stderr, "lsh: allocation error\n"); //exit(EXIT_FAILURE); //} //} //} exit(0); fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); } wait(NULL); } lineNum=lineNum+1; fprintf(stdout, "%d %s: ", lineNum, dir); } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ char *s2 = malloc(INPUT_STRING_SIZE+1); tok_t *t; /* tokens parsed from parsed input */ tok_t *t_input; /* tokesn parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; char cwd[1000]; init_process_list(); init_shell(); int pipe_num = 0; printf("%s running as PID %d under %d\n",argv[0],pid,ppid); process *p; process *next_p; lineNum=0; fprintf(stdout, "%d: ", lineNum); while ((s = freadln(stdin))){ process *pi; p = create_process(s); add_process(p); process* q; q = first_process; shell_is_interactive = isatty(shell_terminal); if(shell_is_interactive){ next_p = first_process; while((next_p->next != NULL)){ if(next_p->pid == 0 ){ break; } else next_p = next_p->next; } if(next_p != NULL){ fundex = lookup(next_p->argv[0]); /* Is first token a shell literal */ if(fundex >= 0){ cmd_table[fundex].fun(&(next_p->argv[1])); next_p->completed = 1; } else if( next_p->argv[0] == NULL){ next_p->completed = 1; } else { launch_process(p); } } for (pi = first_process; pi; pi = pi->next){ if(pi->stopped == 1){ if(pi->background == 1) printf("bg pid: %d stopped\n", pi->pid); else printf("fg pid: %d stopped\n", pi->pid); } if(pi->completed == 1){ if(pi->background == 1) printf("bg pid: %d completed\n", pi->pid); else{} // printf("fg pid: %d completed\n", pi->pid); } } purge_proc_list(); getcwd(cwd,1000); fprintf(stdout, "%s %d: ", cwd, lineNum); } } return 0; }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int lineNum = 0; int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ pid_t cpid, tcpid, cpgid; char cwd[1024]; int status; printf("%s running as PID %d under %d\n",argv[0],pid,ppid); lineNum=0; fprintf(stdout, "%d%s:",lineNum,getcwd(cwd,sizeof(cwd))); init_shell(); while ((s = freadln(stdin))){ char * temp; temp = concat(s,""); t = getToks(s); /* break the line into tokens */ fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) cmd_table[fundex].fun(&t[1]); else { if ((pid = fork()) < 0) { /* fork a child process */ printf("*** ERROR: forking child process failed\n"); exit(1); } else if (pid == 0) { /* for the child process: */ process * new =create_process(temp); add_process(new); launch_process(new); int i; char * command1=">", * command2= "<"; char *amp ="&"; for(i=0;i<MAXTOKS && t[i];i++){ if(strcmp(t[i],amp)== 0){ t[i]=NULL; cmd_and(t[i],amp); } if (strcmp(t[i],command1)==0){ t[i]=NULL; Redirecting_In_Out(t,t[i+1],command1); } if(strcmp(t[i],command2)==0){ t[i]=NULL; Redirecting_In_Out(t,t[i+1],command2); } } path(t); } else { /* for the parent: */ wait(&status); /* wait for completion */ } fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n"); }
int shell (int argc, char *argv[]) { char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */ tok_t *t; /* tokens parsed from input */ int fundex = -1; pid_t pid = getpid(); /* get current processes PID */ pid_t ppid = getppid(); /* get parents PID */ char *buf = NULL; char *cwd = getcwd(buf, PATH_MAX); char *path = getenv("PATH"); tok_t *paths = getToks(path); int k = 0; int isExecutable; int background; struct sigaction sa; sa.sa_handler = handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; first_process = malloc(sizeof(process)); first_process->next = NULL; first_process->prev = NULL; if (sigaction(SIGINT, &sa, NULL) == -1) {fprintf(stdout, "Failed to send request.");} else if (sigaction(SIGTSTP, &sa, NULL) == -1) {fprintf(stdout, "Failed to send request.");} init_shell(); printf("%s running as PID %d under %d\n",argv[0],pid,ppid); fprintf(stdout, "%s: ", cwd); while ((s = freadln(stdin))){ char *newCommand = malloc(INPUT_STRING_SIZE*sizeof(char)); t = getToks(s); /* break the line into tokens */ k = 0; background = 0; while ((t[0][k] != '&') && (t[0][k] != '\0')) { if (t[0][k+1] == '&') background = 1; newCommand[k] = t[0][k]; k++; } t[0] = newCommand; if (background == 1) { shiftTokensRight(1,t); t[1] = "&"; } fundex = lookup(t[0]); /* Is first token a shell literal */ if(fundex >= 0) { if ( (sizeof(t)/sizeof(char**) > 1) && (t[1][0] == '&') ) cmd_table[fundex].fun(&t[2]); else cmd_table[fundex].fun(&t[1]); } else { if (strstr(t[0], "/") != NULL) { //executable with path defined create_process(t[0], t); } else { //executable with no path defined char *pathResolution = malloc(PATH_MAX*sizeof(char)); k = 0; isExecutable = 0; while ( (paths[k] != NULL) && (isExecutable == 0)) { strcpy(pathResolution, paths[k]); strcat(pathResolution, "/"); strcat(pathResolution, t[0]); if (access(pathResolution, F_OK|X_OK) == 0) {isExecutable = 1;} k++; } if (isExecutable == 1) { t[0] = pathResolution; create_process(pathResolution, t); } else { fprintf(stdout, "Couldn't resolve path for executable '%s'.\n", t[0]); } } } cwd = getcwd(buf, PATH_MAX); fprintf(stdout, "%s: ", cwd); } return 0; }