// FIXME: idk how to keep track of the current path on the tty int cd_cmd(int argc, char **argv) { if (argc == 1) { TTY* tty = tty_getCurrentTTY(); u32int currentiNode = tty->currDirectory; fs_node_t current; fs_getFsNode(¤t, currentiNode); fs_node_t *node = finddir_fs(¤t, argv[0]); if (node != NULL) { if (FILE_TYPE(node->mask) == FS_SYMLINK) { char name[MAX_NAME_LENGTH]; read_fs(node, 0, MAX_NAME_LENGTH, (u8int*) name); char* n = name; return cd_cmd(1, (char**) &n);; } if (!permission_file_hasAccess(node, R_BIT)) { printf("cd: You don't have read access to %s\n", argv[0]); return -1; } if (FILE_TYPE(node->mask) == FS_DIRECTORY) { tty->currDirectory = node->inode; memcpy(tty->currPath, node->name, strlen(node->name) + 1); } else { printf("cd: %s is not a directory\n", argv[0]); } kfree(node); } else { printf("cd: The directory \"%s\" does not exist\n", argv[0]); } } return 0; }
void cd_close(void) { cd_cmd(CLOSE_TRAY); paused = 0; }
void cd_eject(void) { cd_cmd(EJECT_TRAY); paused = 0; }
int main(){ init_environ(); //chushihua while(1){ char c,*arg[20],*temp,*temp1,*argv,exec_temp[BUFF_SIZE]; int i=0,j=1,is_bg=0,pid=0,k=0,status=0; int is_pr=0; if(printf("linux@%s>",get_current_dir_name())==EOF) { fflush(stdout); continue; } fflush(stdout); memset(buff,'\0',BUFF_SIZE); memset(buff1,'\0',BUFF_SIZE); memset(input,'\0',BUFF_SIZE); memset(exec_temp,'\0',BUFF_SIZE); arg[0]=(char*)malloc(BUFF_SIZE); //获取用户命令 while((c=getchar())==' '||c=='\t'||c==EOF); if(c=='\n') continue; while(c!='\n'){ buff[i]=c; i++; c=getchar(); } buff[i]='\0'; strcpy(input,buff); strcpy(arg[0],input); temp=(char*)malloc(BUFF_SIZE); strcpy(temp,arg[0]); temp1=(char*)malloc(BUFF_SIZE); //strcpy(temp1,temp); temp1=strtok(temp," "); while(temp1!=NULL){ arg[j]=(char*)malloc(BUFF_SIZE); strcpy(arg[j],temp1); temp1=strtok(NULL," "); j++; } strcpy(exec_temp,(char*)get_current_dir_name()); if(strcmp((char *)get_current_dir_name(),"/")!=0) strcat(exec_temp,"/"); ////////////////////////////////////////////////////////////////////////////////////// for(k=0;k<=i;k++) { if(arg[0][k]=='<'||arg[0][k]=='>'||arg[0][k]=='|') { if (arg[0][k]=='|') { pipel(arg[0],i); //printf("检测%c/n",input[i]); add_history(arg[0]); //add_history加入链表 } else { redirect(arg[0],i); //printf("检测%c/n",input[i]); add_history(arg[0]); //add_history加入链表 } is_pr=1; //将标志管道和重定向的设置为1 break; } } if(is_pr==1) { continue; } for(k=0;k<=i;k++){ if(arg[1][k]=='&'&&arg[1][k+1]=='\0'){ is_bg=1; arg[1][k]='\0'; break; } } ////////////////////////////////////////////////////////////////////////////////////// strcat(exec_temp,arg[1]); ////////////////////////////////////////////////////////////////////////////////////// if(strcmp("jobs",arg[1])==0){ add_history(arg[0]); jobs_cmd(); continue; }else if(strcmp("history",arg[0])==0){ history_cmd(); add_history(arg[0]); continue; }else if(strcmp("history",arg[1])==0&&strcmp("-c",arg[2])==0){ envhis.start=0; envhis.end=0; add_history(arg[0]); continue; }else if(strcmp("exit",arg[0])==0||strcmp("logout",arg[0])==0){ add_history(arg[0]); printf("good bye\n"); break; }else if(strcmp("cd",arg[1])==0){ cd_cmd(arg[2]); add_history(arg[0]); continue; }else if(exec(exec_temp)==1){ add_history(arg[0]); if((pid=fork())==0){ execl(exec_temp,exec_temp,NULL); } else if(pid>0){ wait(NULL); } continue; } if(is_found(arg[1])==0){ printf("the commond can not find\n"); for(i=0;i<j;i++) free(arg[i]); continue; } //命令执行 add_history(arg[0]); if((pid=fork())==0){ if(is_bg==1) while(flag==0) signal(SIGUSR1,setflag); flag=0; if(j==2) execl(buff1,buff1,NULL); else if(j==3) execl(buff1,buff1,arg[2],NULL); else if(j==4) execl(buff1,buff1,arg[2],arg[3],NULL); else if(j==5) execl(buff1,buff1,arg[2],arg[3],arg[4],NULL); }else{ pid1=pid; if(is_bg==1){ add_node(input,pid1); kill(pid,SIGUSR1); pid1=0; } if(is_bg==0) waitpid(pid,&status,0); } if(is_bg==1) sleep(1); for(i=0;i<j;i++) free(arg[i]); } }
int main(void) { char argstr[MAX_argv][STR_LEN]; // storage for command line arguments char *argv[MAX_argv]; // ptr array to command line arguments char *token; // ptr to command line token char buffer[STR_LEN]; // character buffer for command line char delimiter[] = " |\n\t"; // delimiter for the commands int idx; // index variable pid_t PID; // process identifier int fdw; // write file descriptor int fdr; // read file descriptor int ret; // ret value char pwd_buffer[PWD_LEN]; // buffer for the current directory char *fname_in_ptr = NULL; // ptr to the input file name char *fname_out_ptr = NULL; // ptr to the output file name /* reset screen */ fprintf(stdout, "\E[0;0H"); // set cursor to 0;0 fprintf(stdout, "\E[2J"); // clear screen /* get current directory as default */ getcwd(pwd_buffer, PWD_LEN); while (1) { printf("# "); // set prompt readline(buffer, STR_LEN); // read one line from stdin idx = 0; // set index back /* get token */ token = strtok(buffer, delimiter); while (token != NULL) { strcpy(&(argstr[idx][0]), token); argv[idx] = argstr[idx]; idx++; token = strtok(NULL, delimiter); } argv[idx] = NULL; // terminate arguments with NULL pointer /* get input name if set */ fname_in_ptr = get_input_name(argv, idx); /* get output name if is set */ fname_out_ptr = get_output_name(argv, idx); /* check if logout or exit */ exit_logout(argv); /* check if cd */ ret = cd_cmd(argv, pwd_buffer); /* execute command */ if (ret) { if ((PID = fork()) == 0) { // fork child process /* check if write ptr is set */ if (fname_out_ptr != NULL) { close(1); // close stdout, redirect to file fdw = open(fname_out_ptr, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (fdw < 0 ){ fprintf(stderr, "Could not write to file %s\n", fname_out_ptr); exit(-1); } } if (fname_in_ptr != NULL) { close(0); fdr = open(fname_in_ptr, O_RDONLY); if (fdr < 0 ){ fprintf(stderr, "Could not read file %s\n", fname_in_ptr); exit(-1); } } strcpy(buffer, "/bin/"); // "compose" path of command (1st arg) strcat(buffer, argv[0]); execv(buffer, &argv[0]); strcpy(buffer, "/usr/bin/"); strcat(buffer, argv[0]); execv(buffer, &argv[0]); printf("%s: command not found\n", argv[0]); exit(0); // kill process } else if (PID < 0) { printf("fork failed\n"); // fork didn't succeed exit(-1); // terminate sishell } else // here we are parents wait(0); // wait for child process to terminate } } }
void execute_command(char *cmd) { char *token = NULL; int state = 0; token = strtok(cmd,"\n, ,\t"); if(token != NULL) { /********** Measure Time ***********/ if(strcmp("csdTime",token) == 0) { getTimes = TRUE; token = strtok(NULL,"\n, ,\t"); } /********* exit command *********/ if(strcmp("exit",token) == 0) { exit(EXIT_SUCCESS); } /********* setenv command *********/ else if(strcmp("setenv",token) == 0) { state = setenv_cmd(token); if(state == 1) { return; } else { fprintf(stderr,"Command not found! - setenv() failed!\n"); return; } } /********* unsetenv command *********/ else if(strcmp("unsetenv",token) == 0) { state = unsetenv_cmd(token); if(state == 1) { return; } else { fprintf(stderr,"Command not found! - unsetenv() failed!\n"); return; } } /********* cd command *********/ else if(strcmp("cd",token) == 0) { state = cd_cmd(token); if(state == 1) { return; } else { fprintf(stderr,"Command not found! - chdir() failed!\n"); return; } } else { /********* commands with fork being used *********/ state = fork_cmd(token); if(state == 1) { return; } else { fprintf(stderr,"Command not found!\n"); return; } } } else { fprintf(stderr,"Command not found!\n"); return; } }