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; }
char* split(tok_t arg[]) //for the "/" { tok_t *t; char* pathname = getenv("PATH"); t = getToks(pathname); char* split = NULL; FILE* file; int k; for (k = 0; k < MAXTOKS;k++) { split = concat(t[k],"/"); split = concat(split, arg[0]); file = fopen(split, "r"); if (file == NULL) { continue; } else{ break; } fclose(file); } if (split == NULL){ return arg[0]; } return split; }
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; 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 */ 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; }
/** * Creates a process given the inputString from stdin */ process* create_process(char* inputString) { // create process on heap process *p = malloc(sizeof(process)); // look for background flag char *amp = strchr(inputString, '&'); if (amp) { int i = (int)(amp - inputString); p->background = true; printf("%s\n", "Process marked background"); char *old_string = inputString; inputString = malloc(sizeof(char)*(i+1)); inputString[i] = '\0'; strncpy(inputString, old_string, sizeof(char)*i); } // tokenize command line input tok_t *t = getToks(inputString); /* break the line into tokens */ // figure out argv, argc and io int argc; bool io_found = false; p->argv = t; p->stdin = STDIN_FILENO; p->stdout = STDOUT_FILENO; p->stderr = STDERR_FILENO; for (argc=0; t[argc]; ++argc) { if (!io_found) { // figure out argv if (strcmp(t[argc], ">")==0 || strcmp(t[argc], "<")==0) { // copy every token before the pipe sign p->argv = malloc(sizeof(tok_t)*argc); memcpy(p->argv, t, sizeof(tok_t)*argc); io_found = true; } // figure out io if (strcmp(t[argc], ">")==0) { tok_t outfile = t[argc+1]; p->stdout = fileno(fopen(outfile, "w")); } else if (strcmp(t[argc], "<")==0) { tok_t infile = t[argc+1]; p->stdin = fileno(fopen(infile, "r")); } } } // set argument count p->argc = argc; // resolve file path bool access_ok = (access(p->argv[0], F_OK) != -1); if (!access_ok) { p->argv[0] = resolve_path(p->argv[0]); if (!p->argv[0]) { return NULL; } } return p; }
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; }
//finds the path...actually implements ls void part3(tok_t *t) { char *poi=getenv("PATH"); tok_t * pois = getToks(poi); int i; for(i = 0; i<MAXTOKS && pois[i]; i++) { //char *( char *pois, *t); char *fi=concat(pois[i],"/"); fi=concat(fi,t[0]); if(access(fi,F_OK) != -1) { execve(fi,t, NULL); printf("%s \n",fi); } //perror(*t); } }
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]); }
char* resolve_path(char *fname) { if (!fname) return NULL; char *ORIGINAL_PATH = getenv("PATH"); char *PATH; asprintf(&PATH, "%s", ORIGINAL_PATH); tok_t *t = getToks(PATH); int i = 0; for (i=0; t[i]; i++) { char *full_path; asprintf(&full_path, "%s/%s", t[i], fname); if (access(full_path, F_OK) != -1) { // file exists return full_path; } } printf("No command '%s' found.\n", fname); return NULL; }
void path(tok_t *t){ char * val; val = getenv("PATH"); tok_t *paths =getToks(val); int i; for(i =0 ; i < MAXTOKS && paths[i];i++ ){ char *path =concat(paths[i], "/"); path =concat(path,t[0]); if(access(path,F_OK)!=-1){ execve(path,t,NULL); } } execv(*t,t); perror(*t); exit(0); }
/** * Creates a process given the inputString from stdin */ process* create_process(char* inputString) { process* p;// Declaring a new process p = malloc(sizeof(process));//allocating memory for the process tok_t *t; t = getToks(inputString); //getting the tokens from standard input p->argv = t; int i, n =0; for(i =0; i < MAXTOKS && t[i]; i++){ n++;//the number of tokens } p->argc = n; p->pgid = getpgrp(); printf("%d", p->pgid); return p; }
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; }
char* resolve_path(char* inputString){ char path[1000]; strcpy(path, getenv("PATH")); tok_t *path_dirs; char file_buf[1000]; char file[1000]; path_dirs = getToks(path); int i = 0; FILE* f; struct stat st; int num_paths=0; while(*(path_dirs+i) != NULL){ // printf("in resolve path %s\n", *(path_dirs+i)); strcpy(file_buf,path_dirs[i]); sprintf(file, "%s/%s", file_buf, inputString); // printf("file actually %s\n",file); if(f=fopen(file,"r")){ fclose(f); // printf("returning %s\n",file); return file; } else{ //printf("%s didnt work \n",file); } i=i+1; } if(f=fopen(inputString,"r")){ fclose(f); return inputString; } else return inputString; }
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; }
// create a process given 'inputString' from stdin // if a process is created, add the process to the list of processes // and return a point to it // if 'inputString' spesifies a built-in command, // execute the command and return NULL // if can't open file for input/output redirection, return NULL process* create_process( char* inputString ){ // first, check if 'inputString' contains '>' or '<' // if so, redirect input/output // this has to be done before we parse the string; // parsing will treat these characters as separators int outfile = STDOUT_FILENO; // by default, output goes to STDOUT // check if we need to redirect output for this processs char *out_redirect = strstr( inputString, ">" ); if( out_redirect != NULL ){ // redirect output *out_redirect = NUL; // prune 'inputString' char *output = out_redirect + 1; // cut off the portion with the file name // open a file for writing char *file = getToks(output)[0]; outfile = open( file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH); // check that the file was opened if( outfile < 0 ) { perror( file ); return NULL; } } int infile = STDIN_FILENO; // by default, input comes from STDIN // check if we need to redirect input for this processs char *in_redirect = strstr(inputString, "<"); if( in_redirect != NULL ){ // redirect input *in_redirect = NUL; // prune 'inputString' char *input = in_redirect + 1; // cut off the portion with the file name // open a file for reading char *file = getToks(input)[0]; infile = open( file, O_RDONLY ); // check that the file was opened if( infile < 0 ){ perror( file ); return NULL; } } // check for '&' at the end; // if it's present, the process should run in the background bool is_background = false; char* bg = strchr(inputString, '&'); if( bg != NULL ){ is_background = true; bg = NUL; // take '&' out } // now, we can tokenize (possibly, truncated) 'inputString' tok_t *t = getToks( inputString ); // an array of tokens parsed from input int fundex = lookup(t[0]); if (fundex >= 0) { // is the first token a shell literal? // execute a built-in command cmd_table[fundex].fun(&t[1]); return NULL; } else { // no, treat it as a process to be executed process* p = (process*) calloc( 1, sizeof(process) ); p->stdin = infile; p->stdout = outfile; p->stderr = STDERR_FILENO; p->background = is_background; p->next = p->prev = NULL; int argc = 0; for( tok_t *tok = t; *tok != NUL; ++tok ) ++argc; p->argc = argc; p->argv = t; add_process( p ); return p; } }
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; }
exePath(char *s, tok_t *tokWork){ //PART3 - PART4 char *pathTemp = getenv("PATH"); //Gets the path tok_t *t = getToks(pathTemp); //Calls getToks from parse.c char work[PATH_MAX]; int loop; int loop2; FILE *f; bool read = false, write = false; bool standard = true; char *fileName; bool flag = false; for(loop2=0;loop2<MAXTOKS;loop2++){ if(strcmp(tokWork[loop2],">") == 0) { tokWork[loop2] = NULL; fileName = tokWork[loop2+1]; write = true; standard = false; break; } else if (strcmp(tokWork[loop2],"<") == 0){ tokWork[loop2] = NULL; fileName = tokWork[loop2+1]; f = fopen(fileName,"a+"); read = true; standard = false; break; } } //printf("%d",standard); if(standard == true){ EXECCALL: for(loop = 2; loop<MAXTOKS ;loop++){ //MAXTOKS assumed to be a set max token length if(execv(work,tokWork)<0){ strcpy(work,t[loop]); // copies the part of path strcat(work,"/");//concates a "/" onto the string to complete path strcat(work,s); //conacates exe file name } } } else{ if(write == true){ f = fopen(fileName,"a+"); dup2(fileno(f),STDOUT_FILENO); goto EXECCALL; fclose(fileName); } else if(read == true){ dup2(fileno(f),STDIN_FILENO); goto EXECCALL; fclose(f); } } }
int cmd_fork(tok_t argl[]){ pid_t cpid, tcpid, cpgid; cpid = fork(); if (cpid == -1){ perror("fork error"); exit(EXIT_FAILURE); } else if( cpid > 0 ) { // parent process int status; pid_t tcpid = wait(&status); if (tcpid == -1){ perror("wait error"); } } else if( cpid == 0 ){ // child process int tokNum; char file[48]; exFile(argl,'>',&tokNum,file); if(tokNum != -1){ int fd; fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); if(fd>-1){ printf("Redirecting output to %s.\n",file); if(dup2(fd,STDOUT_FILENO)==-1){ printf("Redirect stdout problem.\n"); } } else{printf("Opening redirect file problem.\n");} } exFile(argl,'<',&tokNum,file); if(tokNum != -1){ int fd; fd = open(file, O_RDONLY, S_IRUSR | S_IWUSR); if(fd>-1){ printf("Redirecting input from %s.\n",file); if(dup2(fd,STDIN_FILENO)==-1){ printf("Redirect stdin problem.\n"); } } else{printf("Opening redirect file problem.\n");} } int position = checkTok4char(argl,'/',&tokNum); if(position == 0){ execv(argl[0],argl); } else{ tok_t *paths = getToks(getenv("PATH")); int i; char fullpathname[40]; for (i=0 ; paths[i] != NULL ; i++){ strcpy(fullpathname,paths[i]); strcat(fullpathname,"/"); strcat(fullpathname,argl[0]); if((execv(fullpathname,argl))!=-1){ exit(EXIT_SUCCESS); } } perror("Error"); exit(EXIT_SUCCESS); } } }
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; }
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; }
/** * Creates a process given the inputString from stdin */ process* create_process(char* inputString) { process *p; p = malloc(sizeof(process)); (*p).stdin = 0; (*p).stdout = 1; (*p).stderr = 2; char so[1000]; char si[1000]; char se[1000]; char *po; char *pi; char *pe; char *ptr; int i = 0; char local_s[1000]; strcpy(local_s, inputString); if( *(local_s+strlen(local_s)-2) == '&'){ (*p).background = 1; *(local_s+strlen(local_s)-2) = 0x20; } else{ (*p).background = 0; } ptr = strstr(local_s, "2>"); i=2; if(ptr){ *(ptr) = 0x20; *(ptr+1) = 0x20; while( (*(ptr+i) != '<') && (*(ptr + i) != '>') && (*(ptr + i) != '\0') && (*(ptr + i) != 10)) { se[i-2] = *(ptr + i); *(ptr + i) = 0x20; i = i + 1; } se[i-2] = NULL; pe = trimwhitespace(se); printf("stderr %s\n", pe); (*p).stderr = open(pe, O_WRONLY|O_CREAT, 0666); } ptr = strstr(local_s,">"); i =1; if(ptr){ *ptr = 0x20; while( (*(ptr+i) != '<') && (*(ptr + i) != '2') && (*(ptr + i) != '\0') && (*(ptr + i) != 10) ){ so[i-1] = *(ptr + i); *(ptr + i) = 0x20; i = i + 1; } so[i-1] = NULL; po = trimwhitespace(so); (*p).stdout = open(po, O_WRONLY|O_CREAT, 0666); } ptr = strstr(local_s,"<"); i =1; if(ptr){ *ptr = 0x20; while( (*(ptr+i) != '>') && (*(ptr + i) != '2') && (*(ptr + i) != '\0') && (*(ptr + i) != 10) ){ si[i-1] = *(ptr + i); *(ptr + i) = 0x20; i = i + 1; } si[i-1] = NULL; pi = trimwhitespace(si); (*p).stdin = open(pi, O_RDONLY); } tok_t *t = getToks(local_s); int arg_count =0; i=0; while(t[arg_count] != NULL) arg_count = arg_count + 1; (*p).argv = (char**)malloc(sizeof(char*)*(arg_count+1)) ; //resolve path for first arguement if((t[0] == NULL)){ (*p).argv[0] = NULL; } else{ (*p).argv[0] = (char*)malloc(sizeof(char)*strlen(resolve_path(t[0]))); strcpy((*p).argv[0],resolve_path(t[0])); } //copy over rest of arguments for(i=1; i<arg_count; i++){ (*p).argv[i] = (char*)malloc(sizeof(char)*strlen(t[i])); strcpy((*p).argv[i],t[i]); } (*p).argv[arg_count] = NULL; (*p).argc = arg_count; (*p).pid = 0; (*p).completed = 0; (*p).stopped = 0; // (*p).background = 0; (*p).next = NULL; (*p).prev = NULL; return p; }
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,*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); }