void cmd_handler(int cmd, char *arg) { if (cmd < 6) /* handles first lot of commands as they all can be implemented by forking a child process easily */ { pid_t pid = fork(); if (pid > CHILD_PID) /* parent process wait for child to finish before exiting function and going back to prompt */ { wait(&pid); } else if (pid == CHILD_PID) /* forked child process runs entered command */ { switch (cmd) { case 0: run_clr(); break; case 1: run_dir(arg); break; case 2: run_echo(arg); break; case 3: run_environ(); break; case 4: run_help(); break; case 5: run_pause(); break; default: printf("Congratulations! You've broken my code."); /* I really don't think it's possible to get here... */ } } else { fprintf(stderr, "Failed to fork process. Fatal error, exiting program.\n"); exit(1); } } else /* handles last commands: because of the way I've implemented the execution of the shell, it would be very inconvenient to handle them in a child process (without using shared memory) */ { switch (cmd) { case 6: run_quit(); break; case 7: run_cd(arg); break; default: printf("Again! You did it again!"); /* again, not really possible */ } } }
int run_chdir(char *parameter){ if (strcmp(parameter,"..")==0){ system("cd .."); } else if(strcmp(parameter,"")==0) { run_help(CD_ERROR); return -1; } else { chdir(parameter); } return 0; }
int run_ls(char *parameter){ struct stat myFile; //printw("\nparameter: %s\n",parameter); if(strlen(parameter)==0) { // Wenn der Parameter leer ist strcpy(parameter,"./"); // Parameter auf das aktuelle Verzeichnis setzen } if (stat(parameter, &myFile) < 0) { printw(UNKNOWN_FILE_MESSAGE); refresh(); run_help(LS_ERROR); return -1; } else if (!S_ISDIR(myFile.st_mode)) { // Exists but is not a directory endwin(); fprintf(stderr,FILE_EXISTS_BUT_NOT_DIR_MESSAGE); exit(1); } else { DIR *dp; struct dirent *ep; dp = opendir (parameter); printw("\n"); if (dp != NULL) { while ((ep = readdir (dp))!=NULL ) printw("%s\n",ep->d_name); (void) closedir (dp); } else { printw(FILE_OPEN_MESSAGE); //perror(FILE_OPEN_MESSAGE); } } return 0; }
int main(int argc, char** argv) { ZT_ENABLE(); z_zipex x; zout << sizeof(x); //z_trace_enable(); //___________________________________________________________ // // Load/Save debug arguments //___________________________________________________________ z_debug_load_save_args(&argc,&argv); //___________________________________________________________ // //assign args //___________________________________________________________ int i; for (i=1;i<argc;i++) { size_t j; z_string name; z_string val; zp_text_parser argp(argv[i]); if(argp.test_any_identifier()==zs_matched) { argp.get_match(name); if(argp.test_char('=')==zs_matched) { argp.test_to_eob(); argp.get_match(val); } for(j=0;j<arg_list_count;j++) { argument& a= arg_list[j]; if(name==a.name) { if(a.num_valid_options) { option* opt=get_option(a,val); if(!opt) { zout<<"\nERROR! Invalid option \"" <<val<<"\" for argument \""<<name<<"\"\n\n"; show_valid_options(a); return -1; break; } } a.ref_variable=val; break; } } if(j==arg_list_count) { zout<<"\nERROR! Invalid argument \"" <<name<<"\"\n\n"; run_help(); exit(0); } } } //___________________________________________________________ // //Check Data file //___________________________________________________________ if(g_arg_file_input_data) { z_file f; if(f.open(g_arg_file_input_data,"rb")==-1) { zout<<"\nERROR! Cannot open \"" <<g_arg_file_input_data<<"\"\n\n"; return -1; } f.read_all(g_file_buffer); g_arg_data_in=g_file_buffer.c_str(); } //___________________________________________________________ // //Check Class type //___________________________________________________________ if(g_arg_obj_type) { } //___________________________________________________________ // //Check Test Type //___________________________________________________________ option* opt=get_option(arg_list[2],g_arg_test_type); if(!opt) { zout << "Invalid test type \""<<g_arg_test_type <<"\"\n"; return -1; } g_test_type_function_to_run=opt->pfunc; if(!g_test_type_function_to_run) { zout << "No function for \""<<opt->name <<"\"\n"; return -1; } //___________________________________________________________ // //execute operation //___________________________________________________________ int ret; opt=get_option(arg_list[0],g_arg_operation); if(!opt) { ret= run_help(); } else { opt_func fp=opt->pfunc; if(fp) { ret= (*fp)(); } else zout << "No action for "<< g_arg_operation << "\n"; } //char* ax=z_new char[4]; _CrtDumpMemoryLeaks(); return ret; }