int main(int argc, char *argv[]) { if (argc > 1) { int i; for (i = 0; i < argc - 1; ++i) { // Iterate over the given directory display_dir(argv[i + 1]); } } else { // Iterate over the current directory display_dir("."); } return 0; }
int main(int argc, char *argv[]) { int i, j, k, num; //num统计“-”的个数 char path[PATH_MAX +1]; char param[32]; //保存命令行参数,目标文件和目录名不在此 int flag_param = PARAM_NONE; //参数种类,即是否有-l和-a选项 struct stat buf; //获取命令参数,并将它们存储在param数组中,以及获得'-'的数目 j = 0; num = 0; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { //此时需要存储参数,统计'-'参数的个数 for (k = 1; k < strlen (argv[i]); k++) { param[j++] = argv[i][k]; } num++; //保存'-'的个数 } } /* 根据param数组中的参数来获得系统中相应的宏表示,将他们用'|'连接起来 */ for (i = 0; i < j; i++) { if (param[i] == 'a') { flag_param |= PARAM_A; continue; } else if (param[i] == 'l') { flag_param |= PARAM_L; continue; } else { printf ("my_ls: invalid option -%c\n", param[i]); exit (1); } } param[j] = '\0'; //为参数字符数组添加结束标志 /* 如果没有输入具体的文件名,则就是显示当前目录下文件信息 */ if (num + 1 == argc) { strcpy (path, "./"); //将表示当前目录的字符串写入path中 path[2] = '\0'; //为path添加结束标志 //调用函数,显示文件信息 display_dir (flag_param, path); //将ls参数和显示目录名传过去 return 0; } return EXIT_SUCCESS; }
int main(int argc, char ** argv) { // printf("xiao\n"); char path[PATH_MAX+1]; if( argc == 1 ) { // strcpy(path, "./"); if( getcwd(path, 512) == -1) { my_err("getcwd", __LINE__); } strcat( path, "/" ); // printf(" %s\n",path); // path[9] = '\0'; // printf("sfaaa\n"); display_dir(path); } return 0; }
int cmd_news(string file) { string *dir; dir = get_dir(NEWS_DIR); if(!wizardp(this_player())) dir = filter_array(dir, "filter_news", this_object()); if( !file || file == "") { write("Topics are:\n\n"); write(display_dir(dir) + "\n"); write("Type news <topic> to see more specific information.\n"); } else { if(member_array(file, dir) == -1) { write("There is no such news article.\n"); return 1; } previous_object()->more(NEWS_DIR + "/" + file, 1); } return 1; }
int judge_file_type(char *path_name) { struct file *fp = NULL; /*判断是否为目录*/ fp = filp_open(path_name, O_DIRECTORY, 0); if (IS_ERR(fp)) { /*判断是否为文件*/ fp = filp_open(path_name, O_RDONLY, 0644); if (IS_ERR(fp)) { printk("%s is not a file or a directory\n", path_name); return -1; } display_file(fp); filp_close(fp, NULL); } else { display_dir(fp); filp_close(fp, NULL); } return 0; }
int main( int argc, char **argv) { int i, j, k, num; char path[PATH_MAX+1]; char param[32]; //保存命令行参数,目标文件名和目录名不在此列 int flag_param = PARAM_NONE; //参数种类即是否有-l和-a选项 struct stat buf; /*命令行参数的解析,分析-l,-a,-al,-la,-i选项*/ j = 0; num = 0; for (i=1; i<argc; i++) { if(argv[i][0] == '-') { for (k=i; k<strlen(argv[i]); k++,j++) { param[j] = argv[i][k]; //获取-后面的参数保存到数组param中 } num++; } } for(i=0; i<j; i++) { if (param[i] =='a') { flag_param |= PARAM_A; continue; } else if (param[i] == 'l') { flag_param |= PARAM_L; continue; } else if (param[i] == 'i') { flag_param |= PARAM_I; continue; } else if (param[i] == 'r') { flag_param |= PARAM_R; continue; } else { printf("my_ls : invalid option -%c\n", param[i]); exit(1); } } param[j] = '\0'; //如果没有输入文件名或目录,就显示当前目录 if ((num+1) == argc) { strcpy(path, "./"); path[2] = '\0'; if (flag_param == 5) { display_dirn(flag_param, path); } else { display_dir(flag_param, path); } return 0; } i=1; do{ //如果不是目标文件名或目录,解析下一个命令行参数 if (argv[i][0] == '-') { i++; continue; } else { strcpy(path, argv[i]); //如果目标文件或目录不存在,报错并退出程序 if (stat(path, &buf) == -1) my_err("stat",__LINE__); if (S_ISDIR(buf.st_mode)) { //如果目录的最后一个字符不是'/',就加上'/' if ( path[ strlen(argv[i]) -1] != '/') { path [ strlen(argv[i]) ] !='/'; path [ strlen(argv[i])+1] = '\0'; } else path[ strlen(argv[i])] = '\0'; display_dir(flag_param, path); i++; } } }while (i<argc); return 0; }
int main(int argc, char **argv) { int i,j,k; int num;//记录-的个数 char path[PATH_MAX + 1]; char param[32]; // 保存命令行参数 int flag_param = PARAM_NONE; struct stat buf; j = 0; num = 0; for(i=1;i<argc;i++) { if(argv[i][0] == '-') { for(k=1;k<strlen(argv[i]);k++) { param[j] = argv[i][k]; j++; } num++; } } //现在只支持-a和-l参数 for(i=0;i<j;i++) { if(param[i] == 'a') { flag_param |= PARAM_A; } else if(param[i] == 'l') { flag_param |= PARAM_L; } else { printf("错误的参数:%c\n",param[i]); exit(1); } } param[j] = 0; //如果没有输入文件名或者目录,就显示当前目录 if((num + 1) == argc) { strcpy(path,"./"); path[2] = 0; display_dir(flag_param,path); return 0; } i = 1; for(i=1;i<argc;i++) { if(argv[i][0] != '-') { strcpy(path,argv[i]); if(stat(path,&buf) == -1) my_error("stat",__LINE__); if(S_ISDIR(buf.st_mode)) { //判断目录是否以/结尾 if(path[strlen(argv[i]) - 1] != '/') { path[strlen(argv[i])] = '/'; path[strlen(argv[i] + 1)] = 0; } else path[strlen(argv[i])] = 0; display_dir(flag_param,path); } else { display(flag_param,path); } } } return 0; }
int main(int argc, char *argv[]) { std::vector<std::string> history; int fd = STDIN_FILENO; struct termios savedAttribtes; int dir_len; char RXChar; std::string temp_hist; int curr_vec = -1; char back = 0x7F; //set non-canonical mode SetNonCanonicalMode(fd, &savedAttribtes); while(1){ //shows the current directory at each command line dir_len = display_dir(); while(1){ read(fd, &RXChar, 1); if(0x0A == RXChar){ // newline write(STDOUT_FILENO, "\n", 1); break; }else if(0x04 == RXChar){ return 0; }else if(0x7F == RXChar){ if (temp_hist.length() == 0){ write(STDOUT_FILENO, "\a", 1); //output bell }else{ write(STDOUT_FILENO, &RXChar, 1); temp_hist.pop_back(); } }else if(0x1B == RXChar){//first for up/down/delete read(fd, &RXChar, 1); if (0x5B == RXChar){ //2nd check for up/down/delete read(fd, &RXChar, 1); if(0x33 == RXChar){ //delete read(fd, &RXChar, 1); if (0x7E == RXChar){ //delete part 2 if (temp_hist.length() == 0){ write(STDOUT_FILENO, "\a", 1); //output bell }else{ write(STDOUT_FILENO, &back, 1); temp_hist.pop_back(); } } } if (0x41 == RXChar){ //up arrow if(curr_vec == 9 || (int)(history.size()) == 0 || curr_vec == (int)(history.size())-1){ //out of bounds write(STDOUT_FILENO, "\a", 1); //output bell }else{ write(STDOUT_FILENO, "\r", 1); //return to beginning for (int i = 0; i < (int)(temp_hist.length())+dir_len; ++i){ write(STDOUT_FILENO, " ", 1); //blank out what was there before } write(STDOUT_FILENO, "\r", 1); //return to beginning display_dir(); curr_vec++; write(STDOUT_FILENO, history[curr_vec].c_str(), (int)(history[curr_vec].length())); temp_hist = history[curr_vec]; } }else if (0x42 == RXChar){ //down arrow if(curr_vec == -1 || (int)(history.size()) == 0){ //out of bounds write(STDOUT_FILENO, "\a", 1); //output bell } else if(curr_vec == 0){ //wiping to blank write(STDOUT_FILENO, "\r", 1); //return to beginning for (int i = 0; i < (int)(temp_hist.length())+dir_len; ++i){ write(STDOUT_FILENO, " ", 1); //blank out what was there before } write(STDOUT_FILENO, "\r", 1); //return to beginning display_dir(); temp_hist = ""; curr_vec --; }else{ write(STDOUT_FILENO, "\r", 1); //return to beginning for (int i = 0; i < (int)(temp_hist.length()) + dir_len; ++i){ write(STDOUT_FILENO, " ", 1); //blank out what was there before } write(STDOUT_FILENO, "\r", 1); display_dir(); curr_vec--; write(STDOUT_FILENO, history[curr_vec].c_str(), (int)(history[curr_vec].length())); temp_hist = history[curr_vec]; } } } }else{ write(STDOUT_FILENO, &RXChar, 1); temp_hist.push_back(RXChar); } } if (temp_hist == "") continue; if(history.size() == 10) history.pop_back(); history.insert(history.begin(), temp_hist); //load last entry into vector curr_vec = -1; //reset curr_vec bool temp_hist_bool; if(temp_hist.substr(temp_hist.size()- 1) == "&"){ temp_hist_bool = true; temp_hist.pop_back(); }else{ temp_hist_bool = false; } std::stringstream ss(temp_hist); std::string item; std::vector<std::string> args; while (std::getline(ss, item, ' ')) { args.push_back(item); } temp_hist = ""; //reset temp_hist //check if command contains piping int pipecount = std::count(args.begin(), args.end(), "|"); if(pipecount >0 || std::count(args.begin(), args.end(), "<") > 0 || std::count(args.begin(), args.end(), ">") > 0){ pipeFunction(args, pipecount, history, temp_hist_bool); continue; }else if(args[0] == "pwd" || args[0] == "ls" || args[0] == "history") { call_fork(args, history, temp_hist_bool); }else if(args[0] == "cd") { if(args.size() > 1) cd(args[1].c_str()); else cd(); }else if(args[0] == "exit") { break; }else{ std::string command = ""; command.append(args[0]); std::vector<char *> arguments(args.size() + 1); for(int i = 0; i < (int)args.size(); i++) { arguments[i] = &args[i][0]; } //pid_t parent = getpid(); pid_t pid = fork(); if (pid == -1) { // error, failed to fork() write(STDOUT_FILENO, "Error\n", 6); exit(-1); }else if (pid > 0){ if (!temp_hist_bool) { int status; wait(&status); } }else{ // we are the child execvp(command.c_str(), arguments.data()); std::string error = "Failed to execute "; error += args[0] + "\n"; //std::cout << error << std::endl; write(STDOUT_FILENO, error.c_str(), error.length()); exit(-1); } } } ResetCanonicalMode(fd, &savedAttribtes); return 0; }
int main(int argc, char **argv) { int ret, level; int stat, type, display; int chcat; int has_color, has_fcolor; struct color_rgb color, fcolor; double size; int default_width; double width_scale; double minreg, maxreg, reg; char map_name[GNAME_MAX]; struct GModule *module; struct Option *map_opt; struct Option *color_opt, *fcolor_opt, *rgbcol_opt, *zcol_opt; struct Option *type_opt, *display_opt; struct Option *icon_opt, *size_opt, *sizecolumn_opt, *rotcolumn_opt; struct Option *where_opt; struct Option *field_opt, *cat_opt, *lfield_opt; struct Option *lcolor_opt, *bgcolor_opt, *bcolor_opt; struct Option *lsize_opt, *font_opt, *enc_opt, *xref_opt, *yref_opt; struct Option *attrcol_opt, *maxreg_opt, *minreg_opt; struct Option *width_opt, *wcolumn_opt, *wscale_opt; struct Option *leglab_opt; struct Option *icon_line_opt, *icon_area_opt; struct Flag *id_flag, *cats_acolors_flag, *sqrt_flag, *legend_flag; char *desc; struct cat_list *Clist; LATTR lattr; struct Map_info Map; struct Cell_head window; struct bound_box box; double overlap; stat = 0; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("display")); G_add_keyword(_("graphics")); G_add_keyword(_("vector")); module->description = _("Displays user-specified vector map " "in the active graphics frame."); map_opt = G_define_standard_option(G_OPT_V_MAP); field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL); field_opt->answer = "1"; field_opt->guisection = _("Selection"); display_opt = G_define_option(); display_opt->key = "display"; display_opt->type = TYPE_STRING; display_opt->required = YES; display_opt->multiple = YES; display_opt->answer = "shape"; display_opt->options = "shape,cat,topo,vert,dir,zcoor"; display_opt->description = _("Display"); desc = NULL; G_asprintf(&desc, "shape;%s;cat;%s;topo;%s;vert;%s;dir;%s;zcoor;%s", _("Display geometry of features"), _("Display category numbers of features"), _("Display topology information (nodes, edges)"), _("Display vertices of features"), _("Display direction of linear features"), _("Display z-coordinate of features (only for 3D vector maps)")); display_opt->descriptions = desc; /* Query */ type_opt = G_define_standard_option(G_OPT_V_TYPE); type_opt->answer = "point,line,area,face"; type_opt->options = "point,line,boundary,centroid,area,face"; type_opt->guisection = _("Selection"); cat_opt = G_define_standard_option(G_OPT_V_CATS); cat_opt->guisection = _("Selection"); where_opt = G_define_standard_option(G_OPT_DB_WHERE); where_opt->guisection = _("Selection"); /* Colors */ color_opt = G_define_standard_option(G_OPT_CN); color_opt->label = _("Feature color"); color_opt->guisection = _("Colors"); fcolor_opt = G_define_standard_option(G_OPT_CN); fcolor_opt->key = "fill_color"; fcolor_opt->answer = "200:200:200"; fcolor_opt->label = _("Area fill color"); fcolor_opt->guisection = _("Colors"); rgbcol_opt = G_define_standard_option(G_OPT_DB_COLUMN); rgbcol_opt->key = "rgb_column"; rgbcol_opt->guisection = _("Colors"); rgbcol_opt->label = _("Colorize features according color definition column"); rgbcol_opt->description = _("Color definition in R:G:B form"); zcol_opt = G_define_standard_option(G_OPT_M_COLR); zcol_opt->key = "zcolor"; zcol_opt->description = _("Colorize point or area features according to z-coordinate"); zcol_opt->guisection = _("Colors"); /* Lines */ width_opt = G_define_option(); width_opt->key = "width"; width_opt->type = TYPE_INTEGER; width_opt->answer = "0"; width_opt->guisection = _("Lines"); width_opt->description = _("Line width"); wcolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN); wcolumn_opt->key = "width_column"; wcolumn_opt->guisection = _("Lines"); wcolumn_opt->label = _("Name of numeric column containing line width"); wcolumn_opt->description = _("These values will be scaled by width_scale"); wscale_opt = G_define_option(); wscale_opt->key = "width_scale"; wscale_opt->type = TYPE_DOUBLE; wscale_opt->answer = "1"; wscale_opt->guisection = _("Lines"); wscale_opt->description = _("Scale factor for width_column"); /* Symbols */ icon_opt = G_define_option(); icon_opt->key = "icon"; icon_opt->type = TYPE_STRING; icon_opt->required = NO; icon_opt->multiple = NO; icon_opt->guisection = _("Symbols"); icon_opt->answer = "basic/x"; /* This could also use ->gisprompt = "old,symbol,symbol" instead of ->options */ icon_opt->options = icon_files(); icon_opt->description = _("Point and centroid symbol"); size_opt = G_define_option(); size_opt->key = "size"; size_opt->type = TYPE_DOUBLE; size_opt->answer = "5"; size_opt->guisection = _("Symbols"); size_opt->label = _("Symbol size"); size_opt->description = _("When used with the size_column option this becomes the scale factor"); sizecolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN); sizecolumn_opt->key = "size_column"; sizecolumn_opt->guisection = _("Symbols"); sizecolumn_opt->description = _("Name of numeric column containing symbol size"); rotcolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN); rotcolumn_opt->key = "rotation_column"; rotcolumn_opt->guisection = _("Symbols"); rotcolumn_opt->label = _("Name of numeric column containing symbol rotation angle"); rotcolumn_opt->description = _("Measured in degrees CCW from east"); icon_area_opt = G_define_option(); icon_area_opt->key = "icon_area"; icon_area_opt->type = TYPE_STRING; icon_area_opt->required = NO; icon_area_opt->multiple = NO; icon_area_opt->guisection = _("Legend"); icon_area_opt->answer = "legend/area"; icon_area_opt->options = icon_files(); icon_area_opt->description = _("Area/boundary symbol for legend"); icon_line_opt = G_define_option(); icon_line_opt->key = "icon_line"; icon_line_opt->type = TYPE_STRING; icon_line_opt->required = NO; icon_line_opt->multiple = NO; icon_line_opt->guisection = _("Legend"); icon_line_opt->answer = "legend/line"; icon_line_opt->options = icon_files(); icon_line_opt->description = _("Line symbol for legend"); leglab_opt = G_define_option(); leglab_opt->key = "legend_label"; leglab_opt->type = TYPE_STRING; leglab_opt->guisection = _("Legend"); leglab_opt->description = _("Label to display after symbol in vector legend"); /* Labels */ lfield_opt = G_define_standard_option(G_OPT_V_FIELD); lfield_opt->key = "label_layer"; lfield_opt->required = NO; lfield_opt->guisection = _("Labels"); lfield_opt->label = _("Layer number for labels (default: the given layer number)"); attrcol_opt = G_define_standard_option(G_OPT_DB_COLUMN); attrcol_opt->key = "attribute_column"; attrcol_opt->multiple = NO; /* or fix attr.c, around line 102 */ attrcol_opt->guisection = _("Labels"); attrcol_opt->description = _("Name of column to be displayed as a label"); lcolor_opt = G_define_standard_option(G_OPT_C); lcolor_opt->key = "label_color"; lcolor_opt->answer = "red"; lcolor_opt->label = _("Label color"); lcolor_opt->guisection = _("Labels"); bgcolor_opt = G_define_standard_option(G_OPT_CN); bgcolor_opt->key = "label_bgcolor"; bgcolor_opt->answer = "none"; bgcolor_opt->guisection = _("Labels"); bgcolor_opt->label = _("Label background color"); bcolor_opt = G_define_standard_option(G_OPT_CN); bcolor_opt->key = "label_bcolor"; bcolor_opt->type = TYPE_STRING; bcolor_opt->answer = "none"; bcolor_opt->guisection = _("Labels"); bcolor_opt->label = _("Label border color"); lsize_opt = G_define_option(); lsize_opt->key = "label_size"; lsize_opt->type = TYPE_INTEGER; lsize_opt->answer = "8"; lsize_opt->guisection = _("Labels"); lsize_opt->description = _("Label size (pixels)"); font_opt = G_define_option(); font_opt->key = "font"; font_opt->type = TYPE_STRING; font_opt->guisection = _("Labels"); font_opt->description = _("Font name"); enc_opt = G_define_option(); enc_opt->key = "encoding"; enc_opt->type = TYPE_STRING; enc_opt->guisection = _("Labels"); enc_opt->description = _("Text encoding"); xref_opt = G_define_option(); xref_opt->key = "xref"; xref_opt->type = TYPE_STRING; xref_opt->guisection = _("Labels"); xref_opt->answer = "left"; xref_opt->options = "left,center,right"; xref_opt->description = _("Label horizontal justification"); yref_opt = G_define_option(); yref_opt->key = "yref"; yref_opt->type = TYPE_STRING; yref_opt->guisection = _("Labels"); yref_opt->answer = "center"; yref_opt->options = "top,center,bottom"; yref_opt->description = _("Label vertical justification"); minreg_opt = G_define_option(); minreg_opt->key = "minreg"; minreg_opt->type = TYPE_DOUBLE; minreg_opt->required = NO; minreg_opt->description = _("Minimum region size (average from height and width) " "when map is displayed"); maxreg_opt = G_define_option(); maxreg_opt->key = "maxreg"; maxreg_opt->type = TYPE_DOUBLE; maxreg_opt->required = NO; maxreg_opt->description = _("Maximum region size (average from height and width) " "when map is displayed"); /* Colors */ cats_acolors_flag = G_define_flag(); cats_acolors_flag->key = 'c'; cats_acolors_flag->guisection = _("Colors"); cats_acolors_flag->description = _("Random colors according to category number " "(or layer number if 'layer=-1' is given)"); /* Query */ id_flag = G_define_flag(); id_flag->key = 'i'; id_flag->guisection = _("Selection"); id_flag->description = _("Use values from 'cats' option as feature id"); sqrt_flag = G_define_flag(); sqrt_flag->key = 'r'; sqrt_flag->label = _("Use square root of the value of size_column"); sqrt_flag->description = _("This makes circle areas proportionate to the size_column values " "instead of circle radius"); sqrt_flag->guisection = _("Symbols"); legend_flag = G_define_flag(); legend_flag->key = 's'; legend_flag->label = _("Do not show this layer in vector legend"); legend_flag->guisection = _("Legend"); /* Check command line */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); D_open_driver(); G_get_set_window(&window); /* Check min/max region */ reg = ((window.east - window.west) + (window.north - window.south)) / 2; if (minreg_opt->answer) { minreg = atof(minreg_opt->answer); if (reg < minreg) { G_important_message(_("Region size is lower than minreg, nothing displayed")); exit(EXIT_SUCCESS); } } if (maxreg_opt->answer) { maxreg = atof(maxreg_opt->answer); if (reg > maxreg) { G_important_message(_("Region size is greater than maxreg, nothing displayed")); exit(EXIT_SUCCESS); } } strcpy(map_name, map_opt->answer); default_width = atoi(width_opt->answer); if (default_width < 0) default_width = 0; width_scale = atof(wscale_opt->answer); if (cats_acolors_flag->answer && rgbcol_opt->answer) { G_warning(_("The -%c flag and <%s> option cannot be used together, " "the -%c flag will be ignored!"), cats_acolors_flag->key, rgbcol_opt->key, cats_acolors_flag->key); cats_acolors_flag->answer = FALSE; } color = G_standard_color_rgb(WHITE); has_color = option_to_color(&color, color_opt->answer); fcolor = G_standard_color_rgb(WHITE); has_fcolor = option_to_color(&fcolor, fcolor_opt->answer); size = atof(size_opt->answer); /* if where_opt was specified select categories from db * otherwise parse cat_opt */ Clist = Vect_new_cat_list(); Clist->field = atoi(field_opt->answer); /* open vector */ level = Vect_open_old2(&Map, map_name, "", field_opt->answer); chcat = 0; if (where_opt->answer) { if (Clist->field < 1) G_fatal_error(_("Option <%s> must be > 0"), field_opt->key); chcat = 1; option_to_where(&Map, Clist, where_opt->answer); } else if (cat_opt->answer) { if (Clist->field < 1 && !id_flag->answer) G_fatal_error(_("Option <%s> must be > 0"), field_opt->key); chcat = 1; ret = Vect_str_to_cat_list(cat_opt->answer, Clist); if (ret > 0) G_warning(n_("%d error in cat option", "%d errors in cat option", ret), ret); } type = Vect_option_to_types(type_opt); display = option_to_display(display_opt); /* labels */ options_to_lattr(&lattr, lfield_opt->answer, lcolor_opt->answer, bgcolor_opt->answer, bcolor_opt->answer, atoi(lsize_opt->answer), font_opt->answer, enc_opt->answer, xref_opt->answer, yref_opt->answer); D_setup(0); D_set_reduction(1.0); G_verbose_message(_("Plotting...")); if (level >= 2) Vect_get_map_box(&Map, &box); if (level >= 2 && (window.north < box.S || window.south > box.N || window.east < box.W || window.west > G_adjust_easting(box.E, &window))) { G_warning(_("The bounding box of the map is outside the current region, " "nothing drawn")); } else { overlap = G_window_percentage_overlap(&window, box.N, box.S, box.E, box.W); G_debug(1, "overlap = %f \n", overlap); if (overlap < 1) Vect_set_constraint_region(&Map, window.north, window.south, window.east, window.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX); /* default line width */ if (!wcolumn_opt->answer) D_line_width(default_width); if (display & DISP_SHAPE) { stat += display_shape(&Map, type, Clist, &window, has_color ? &color : NULL, has_fcolor ? &fcolor : NULL, chcat, icon_opt->answer, size, sizecolumn_opt->answer, sqrt_flag->answer ? TRUE : FALSE, rotcolumn_opt->answer, id_flag->answer ? TRUE : FALSE, cats_acolors_flag->answer ? TRUE : FALSE, rgbcol_opt->answer, default_width, wcolumn_opt->answer, width_scale, zcol_opt->answer); if (wcolumn_opt->answer) D_line_width(default_width); } if (has_color) { D_RGB_color(color.r, color.g, color.b); if (display & DISP_DIR) stat += display_dir(&Map, type, Clist, chcat, size); } if (!legend_flag->answer) { write_into_legfile(&Map, type, leglab_opt->answer, map_name, icon_opt->answer, size_opt->answer, color_opt->answer, fcolor_opt->answer, width_opt->answer, icon_area_opt->answer, icon_line_opt->answer, sizecolumn_opt->answer); } /* reset line width: Do we need to get line width from display * driver (not implemented)? It will help restore previous line * width (not just 0) determined by another module (e.g., * d.linewidth). */ if (!wcolumn_opt->answer) D_line_width(0); if (display & DISP_CAT) stat += display_label(&Map, type, Clist, &lattr, chcat); if (attrcol_opt->answer) stat += display_attr(&Map, type, attrcol_opt->answer, Clist, &lattr, chcat); if (display & DISP_ZCOOR) stat += display_zcoor(&Map, type, &lattr); if (display & DISP_VERT) stat += display_vert(&Map, type, &lattr, size); if (display & DISP_TOPO) stat += display_topo(&Map, type, &lattr, size); } D_save_command(G_recreate_command()); D_close_driver(); Vect_close(&Map); Vect_destroy_cat_list(Clist); if (stat != 0) { G_fatal_error(_("Rendering failed")); } G_done_msg(" "); exit(EXIT_SUCCESS); }
int main(void) { /* enable interrupts (on the CPU) */ init_interrupts(); /* Initialize audio and video */ audio_init(44100,2); console_init(); /* Initialize key detection */ controller_init(); MikMod_RegisterAllDrivers(); MikMod_RegisterAllLoaders(); md_mode |= DMODE_16BITS; md_mode |= DMODE_SOFT_MUSIC; md_mode |= DMODE_SOFT_SNDFX; //md_mode |= DMODE_STEREO; md_mixfreq = audio_get_frequency(); MikMod_Init(""); if(dfs_init( DFS_DEFAULT_LOCATION ) != DFS_ESUCCESS) { printf("Filesystem failed to start!\n"); } else { direntry_t *list; int count = 0; int page = 0; int cursor = 0; console_set_render_mode(RENDER_MANUAL); console_clear(); list = populate_dir(&count); while(1) { console_clear(); display_dir(list, cursor, page, MAX_LIST, count); console_render(); controller_scan(); struct controller_data keys = get_keys_down(); if(keys.c[0].up) { cursor--; new_scroll_pos(&cursor, &page, MAX_LIST, count); } if(keys.c[0].down) { cursor++; new_scroll_pos(&cursor, &page, MAX_LIST, count); } if(keys.c[0].C_right && list[cursor].type == DT_REG) { /* Module playing loop */ MODULE *module = NULL; /* Concatenate to make file */ char path[512]; strcpy( path, dir ); strcat( path, list[cursor].filename ); module = Player_Load(path, 256, 0); /* Ensure that first part of module doesn't get cut off */ audio_write_silence(); audio_write_silence(); if(module) { char c = '-'; int sw = 0; Player_Start(module); while(1) { if(sw == 5) { console_clear(); display_dir(list, cursor, page, MAX_LIST, count); sw = 0; switch(c) { case '-': c = '\\'; break; case '\\': c = '|'; break; case '|': c = '/'; break; case '/': c = '-'; break; } printf("\n\n\n%c Playing module", c); console_render(); } else { sw++; } MikMod_Update(); controller_scan(); struct controller_data keys = get_keys_down(); if(keys.c[0].C_left || !Player_Active()) { /* End playback */ audio_write_silence(); audio_write_silence(); audio_write_silence(); audio_write_silence(); break; } } Player_Stop(); Player_Free(module); } } if(keys.c[0].L) { /* Open the SD card */ strcpy( dir, "sd://" ); /* Populate new directory */ free_dir(list); list = populate_dir(&count); page = 0; cursor = 0; } if(keys.c[0].R) { /* Open the ROM FS card */ strcpy( dir, "rom://" ); /* Populate new directory */ free_dir(list); list = populate_dir(&count); page = 0; cursor = 0; } if(keys.c[0].A && list[cursor].type == DT_DIR) { /* Change directories */ chdir(list[cursor].filename); /* Populate new directory */ free_dir(list); list = populate_dir(&count); page = 0; cursor = 0; } if(keys.c[0].B) { /* Up! */ chdir(".."); /* Populate new directory */ free_dir(list); list = populate_dir(&count); page = 0; cursor = 0; } } } while(1); return 0; }
void display_dir(const char *dirname) { printf("%s:\n", dirname); // Local parameters int i, j; // Loop indices struct stat buf; // Check if the provided path leads to a directory or not if (lstat(dirname, &buf) < 0) { fprintf(stderr, "stat error: %s\n", strerror(errno)); return; } // Ensure the provided argument is a directory if (S_ISDIR(buf.st_mode) == 0) { fprintf(stderr, "ERROR: The given argument is not a directory.\n"); return; } DIR *dp = opendir(dirname); if (!dp) { fprintf(stderr, "ERROR: Unable to open directory.\n"); return; } // Retreive the contents of the directory struct dirent **file_list; int file_count = scandir(dirname, &file_list, 0, alphasort); // Build list of filenames in the directory dynamic_string_array_t string_array; dynamic_string_array_init(&string_array, 10, MAX_STRING_SIZE); // TODO: Refactor out constant size_t total_blocks = 0; // Build an array of the directory contents for (i = 0; i < file_count; ++i) { // Build a string containing the fill path to the next file char full_path[MAX_PATH_LEN]; strncpy(full_path, dirname, strlen(dirname)); full_path[ strlen(dirname) ] = 0; // Prevent buffer overrun // Append a forward slash to the given directory name if needed if (full_path[ strlen(dirname) - 1 ] != '/') { strcat(full_path, "/"); } strcat(full_path, file_list[i]->d_name); if (lstat(full_path, &buf) < 0) { fprintf(stderr, "stat error: %s\n", strerror(errno)); return; } total_blocks += (size_t) buf.st_blocks / 2; dynamic_string_array_push(&string_array, full_path); } printf("total %lu\n", total_blocks); // Print the directory contents for (i = 0; i < string_array.size; ++i) { display_file_info(string_array.array[i]); } printf("\n"); // Recurse through subdirectories for (i = 0; i < string_array.size; ++i) { // Create a string copy of the next file name char temp[MAX_STRING_SIZE]; for (j = 0; j < strlen(string_array.array[i]); ++j) { temp[j] = string_array.array[i][j]; } temp[ strlen(string_array.array[i]) ] = 0; // Prevent buffer overrun // Get the last '/' delimited string char *token = strtok(temp, "/"), *prev_token = 0; while ((token = strtok(0, "/")) != 0) { prev_token = token; } // Do not recurse into the '.' or '..' directories if ((strcmp(prev_token, ".") == 0) || (strcmp(prev_token, "..") == 0)) { continue; } if (lstat(string_array.array[i], &buf) < 0) { fprintf(stderr, "stat error: %s\n", strerror(errno)); return; } if (S_ISDIR(buf.st_mode)) { display_dir(string_array.array[i]); } } // Free the contents of the dynamic array dynamic_string_array_clear(&string_array); // Free the contents of the file list for (i = 0; i < file_count; ++i) { free(file_list[i]); } free(file_list); closedir(dp); }
int main(int argc, char **argv) { int i, j, k, num; char path[PATH_MAX+1]; char param[32]; int flag_param = PARAM_NONE; struct stat buf; j = 0; num = 0; for(i=1; i<argc; i++) { if(argv[i][0] == '-') { for(k = 1; k < strlen(argv[i]); k++, j++) { param[j] = argv[i][k]; } num++; } } for(i=0; i<j; i++) { if(param[i] == 'a') { flag_param = PARAM_A; continue; } else if(param[i] == 'l') { flag_param = PARAM_L; continue; } else { printf("my_ls:invalid option -%c\n", param[i]); exit(1); } } param[j] = '\0'; if((num + 1) == argc) { strcpy(path,"./"); path[2] = '\0'; display_dir(flag_param, path); return 0; } i = 1; do { if(argv[i][0] == '-') { i++; continue; } else { strcpy(path, argv[i]); if(stat(path, &buf) == -1) my_err("stat", __LINE__); if(S_ISDIR(buf.st_mode)) { if(path[strlen(argv[i])-1] != '/') { path[strlen(argv[i])] = '/'; path[strlen(argv[i]) + 1] = '\0'; } else { path[strlen(argv[i])] = '\0'; } display_dir(flag_param,path); i++; } else { display(flag_param,path); i++; } } } while (i < argc); return 0; }
int main(int argc, char **argv) { int i, j, k, num; char path[PATH_MAX + 1]; char param[32]; //保存命令行参数,目标文件名和目录名不在此列 int flag_param = PARAM_NONE; //参数种类, -l,-a struct stat buf; //保存文件状态信息的结构体 //命令行参数解析,-l, -a, -al, -la j = 0; num = 0; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { for (k = 1; k < strlen(argv[i]); k++, j++) { param[j] = argv[i][k]; //获取"-"后面的参数保存到数组param中 } num++; //保存"-"的个数 } } //只支持参数 -a 和 -l,若有其他的,报错 for (i = 0; i < j; i++) { if (param[i] == 'a') { flag_param |= PARAM_A; continue; } if (param[i] == 'l') { flag_param |= PARAM_L; continue; } else { printf ("my_ls: invalid option -%c\n", param[i]); exit (1); } } param[j] = '\0'; //如果没有输入文件名或目录,就显示当前目录 if ((num + 1) == argc) { strcpy (path, "./"); path[2] = '\0'; display_dir (flag_param, path); return 0; } i = 1; do { //如果不是目标文件名或目录,解析下一个命令行参数 if (argv[i][0] == '-') { i++; continue; } else { strcpy (path, argv[i]); //如果目标文件名或目录不存在,报错并退出 if (stat (path, &buf) == -1) { my_err ("stat", errno); } if (S_ISDIR (buf.st_mode)) { //argv[i]是个目录 //如果目录的最后一个字符不是'/', 就加上'/' if (path[strlen(argv[i] - 1)] != '/') { path[strlen(argv[i])] = '/'; path[strlen(argv[i]) + 1] = '\0'; } else { path[strlen(argv[i])] = '\0'; } display_dir (flag_param, path); } } } while (i < argc); return EXIT_SUCCESS; }
//--------- Begin of function Sprite::cur_sprite_frame ---------// // // Return the current frame of the sprite // SpriteFrame* Sprite::cur_sprite_frame(int *needMirror) { UCHAR curDir = display_dir(); if( needMirror) *needMirror = need_mirror(curDir); // do not update cur_dir as curDir err_when(curDir<0 || curDir>=3*MAX_SPRITE_DIR_TYPE); switch( cur_action ) { case SPRITE_MOVE: //### begin alex 14/4 ###// case SPRITE_SHIP_EXTRA_MOVE: //#### end alex 14/4 ####// if( guard_count) { if( curDir >= MAX_SPRITE_DIR_TYPE) { err_here(); curDir %= MAX_SPRITE_DIR_TYPE; } return sprite_frame_res[sprite_info->guard_move_array[curDir].first_frame_recno+cur_frame-1]; } else return sprite_frame_res[sprite_info->move_array[curDir].first_frame_recno+cur_frame-1]; case SPRITE_ATTACK: err_when(curDir<0 || curDir>=MAX_SPRITE_DIR_TYPE); if( guard_count ) { SpriteGuardStop *guardStopAction = sprite_info->guard_stop_array + curDir; return sprite_frame_res[guardStopAction->first_frame_recno+ MIN(guard_count,guardStopAction->frame_count)-1]; } else return sprite_frame_res[sprite_info->attack_array[cur_attack][curDir].first_frame_recno+cur_frame-1]; case SPRITE_TURN: case SPRITE_IDLE: case SPRITE_WAIT: // air unit needs it own stop frames to float on air { if( guard_count ) { if( curDir >= MAX_SPRITE_DIR_TYPE) { // if the sprite is turning, adjust direction to next if( turn_delay > 0) curDir ++; curDir %= MAX_SPRITE_DIR_TYPE; } SpriteGuardStop *guardStopAction = sprite_info->guard_stop_array + curDir; return sprite_frame_res[guardStopAction->first_frame_recno+ MIN(guard_count,guardStopAction->frame_count)-1]; } else { SpriteStop *stopAction= sprite_info->stop_array +curDir; if(cur_frame > stopAction->frame_count) return sprite_frame_res[stopAction->frame_recno]; // first frame else // only few sprite has stopAction->frame_count > 1 return sprite_frame_res[stopAction->frame_recno+cur_frame-1]; } } case SPRITE_DIE: if(sprite_info->die.first_frame_recno) // only if this sprite has dying frame { if( needMirror) *needMirror = 0; // no need to mirror at any direction return sprite_frame_res[sprite_info->die.first_frame_recno+cur_frame-1]; } default: return sprite_frame_res[sprite_info->move_array[curDir].first_frame_recno+cur_frame-1]; } }
void display_dir( char * path ) { int i, j,count=0; DIR * dir; struct stat buf; struct dirent * ptr; char filenames[256][PATH_MAX+1]; char mulunames[256][PATH_MAX+1]; char str[PATH_MAX+1]; // strcpy( mulunames[], path ); if( ( dir = opendir( path )) == -1 ) { my_err("opendir", __LINE__); } i = 0; printf("%s\n",path); while( (ptr = readdir(dir)) != NULL) { // printf("xingbuxing\n"); // strcpy( filenames[count], ptr->d_name ); // filenames[count][strlen( ptr->d_name )] = '\0'; // if( (strcmp( ptr->d_name, '.' ) != 0) && (strcmp( ptr->d_name , "..")!= 0 )) // { char filename[200]; strcpy(filename, path); strcat(filename, ptr->d_name); // printf("-=-%s--=%s+++\n", ptr->d_name, filename); if( stat(filename, &buf) ==-1) // ) { my_err("stat", __LINE__); } // printf("sfd\n"); if( S_ISDIR( buf.st_mode ) && (strcmp( ptr->d_name, "." ) !=0) && ( strcmp( ptr->d_name , "..")!=0) ) { mulunames[count][strlen(filename)] = '\0'; // strcat( mulunames[count],'/' ); if( filename[strlen( filename ) -1] != '/' ) { // puts(filename); strcat(filename, "/"); // printf("!!!! %s\n",ptr->d_name)a // puts(filename); } else { filename[ strlen( filename ) ] = '\0'; } strcat( mulunames[count],filename ); // strcat( mulunames[count], ptr->d_name ); mulunames[count][strlen( mulunames[count])] = '\0'; // display_dir( mulunames[count] ); // printf(" $$$ %s\n", mulunames[count]); count++; // printf("\n"); } else{ strcpy( filenames[i], ptr->d_name ); filenames[i][strlen( ptr->d_name )] = '\0'; // i++; printf(" %s",filenames[i]); i++; } // } } // closedir(dir); // printf(" %s \n", mulunames[0]); //printf(" %s\n", path); /* for( j = 0; j< i; j++ ) { printf(" %s", filenames[j]); } */ printf("\n"); if( count != 0 ) { // printf("sf\n"); for( j = 0; j<count; ++j ) { // printf("!! %s!!\n", mulunames[j]); display_dir( mulunames[j] ); } } }
int main (int argc, char * argv[]) { /* Parse arguments */ char * p = "."; if (argc > 1) { int c; while ((c = getopt(argc, argv, "ahl?")) != -1) { switch (c) { case 'a': show_hidden = 1; break; case 'h': human_readable = 1; break; case 'l': long_mode = 1; break; case '?': show_usage(argc, argv); return 0; } } if (optind < argc) { p = argv[optind]; } if (optind + 1 < argc) { print_dir = 1; } } stdout_is_tty = isatty(STDOUT_FILENO); if (long_mode) { struct tm * timeinfo; struct timeval now; gettimeofday(&now, NULL); //time(NULL); timeinfo = localtime((time_t *)&now.tv_sec); this_year = timeinfo->tm_year; } if (stdout_is_tty) { TRACE("getting display size"); struct winsize w; ioctl(1, TIOCGWINSZ, &w); term_width = w.ws_col; term_height = w.ws_row; term_width -= 1; /* And this just helps clean up our math */ } int out = 0; if (argc == 1 || optind == argc) { TRACE("no file to look up"); if (display_dir(p) == 2) { fprintf(stderr, "%s: %s: %s\n", argv[0], p, strerror(errno)); } } else { list_t * files = list_create(); while (p) { struct tfile * f = malloc(sizeof(struct tfile)); f->name = p; int t = lstat(p, &f->statbuf); if (t < 0) { fprintf(stderr, "%s: %s: %s\n", argv[0], p, strerror(errno)); free(f); out = 2; } else { if (S_ISLNK(f->statbuf.st_mode)) { stat(p, &f->statbufl); f->link = malloc(4096); readlink(p, f->link, 4096); } list_insert(files, f); } optind++; if (optind >= argc) p = NULL; else p = argv[optind]; } if (!files->length) { /* No valid entries */ return out; } struct tfile ** file_arr = malloc(sizeof(struct tfile *) * files->length); int index = 0; foreach(node, files) { file_arr[index++] = (struct tfile *)node->value; } list_free(files); qsort(file_arr, index, sizeof(struct tfile *), filecmp); int first_directory = index; for (int i = 0; i < index; ++i) { if (S_ISDIR(file_arr[i]->statbuf.st_mode)) { first_directory = i; break; } } if (first_directory) { display_tfiles(file_arr, first_directory); } for (int i = first_directory; i < index; ++i) { if (i != 0) { printf("\n"); } if (display_dir(file_arr[i]->name) == 2) { fprintf(stderr, "%s: %s: %s\n", argv[0], file_arr[i]->name, strerror(errno)); } } } return out; }
int face(int argc, char ** argv) { int i, j=0, k, num=0; char path[PATH_MAX+1]; char param[32]; //保存命令行参数,目标文件和目录名不在此列 int flag_param=PARAM_NONE; //参数种类,即是否有-l和-a存在 struct stat buf; int time=0; int file=0; int sT_INO=0; for(i=1; i<argc; i++){ if(argv[i][0]=='-'){ for(k=1; k<strlen(argv[i]); k++, j++){ param[j]=argv[i][k]; //获取-后面的参数保存到param中 } num++; //保存-的个数 } } //只支持参数a和l如果有其他参数就报错 for(i=0; i<j; i++){ if(param[i]=='a'){ flag_param |= PARAM_A; continue; }else if(param[i]=='l'){ file=1; flag_param |= PARAM_L; continue; }else if(param[i]=='i'){ sT_INO=1; continue; }else if(param[i]=='t'){ time=1; continue; } else if(param[i]=='r'){ file=2; }else if(param[i]=='R'){ print_dir("./\0"); return 0; } else{ printf("my_ls:invalid optio -%c\n", param[1]); exit(1); } } param[j]='\0'; //如果没有输入文件名或目录就显示当前目录 if( argc==2){ strcpy(path, "./"); path[2]='\0'; display_dir(flag_param, path,time, file, sT_INO ); return 0; } i=1; do{//如果不是目标文件或目录,解析下一个命令参数 if(argv[i][0]=='-'){ i++; continue; }else{ strcpy(path, argv[i]); //如果目标文件或目录不存在,则报错并退出程序 if( stat(path, &buf)==-1 ){ my_err("stat",__LINE__); } if(S_ISDIR(buf.st_mode)){ //argv[i]是一个目录 //如果目录的最后一个字符不是'/'就加上'/' if( path[strlen(argv[i])-1] != '/'){ path[strlen(argv[i])]='/'; path[strlen(argv[i])+1]='\0'; } else path[ strlen(argv[i])] ='\0'; display_dir(flag_param, path,time, file, sT_INO); i++; }else{ //argv[i]是一个文件 display(flag_param, path,sT_INO); i++; } }; }while(i<argc); return 0; }
int main(int argc,char** argv) { int i,j,k,num; char path[PATH_MAX+1]; char param[32];//save the arguments of command and it doesn't include the target file and directory name int flag_param=PARAM_NONE;//the sort of argument struct stat buf;//the struct receive all information of a file j=0; num=0; for(i=1;i<argc;i++) { if(argv[i][0]=='-') { for(k=1;k<strlen(argv[i]);k++) { param[j++]=argv[i][k]; } num++;//count the number of '-' } } //check the argument because of only supporting -a and -l for(i=0;i<j;i++) { if(param[i]=='a') { flag_param|=PARAM_A; continue; } else if(param[i]=='l') { flag_param|=PARAM_L; continue; } else { printf("error:my_ls invalid option -%c\n",param[i]); exit(1); } } param[j]='\0'; //print the information of current directory if the command without the name of target file and current directory if((num+1)==argc) { strcpy(path,"./"); path[2]='\0'; display_dir(flag_param,path); return 0; } //extract the target file name and dirctory name form command i=1; do { //the current argument doesn't comprise the target file name and dirctory name if(argv[i][0]=='-') { i++; continue; } else { strcpy(path,argv[i]); //detect if the "path" exsit if(stat(path,&buf)==-1) { my_err("stat",errno,__LINE__); } //detect the "path" is a file or a directory if(S_ISDIR(buf.st_mode)) { //directory //detect if the last character of the "path" is '/' if(path[strlen(path)-1]=='/') { path[strlen(path)]=='\0'; } else { path[strlen(path)]='/'; path[strlen(path)]='\0'; } display_dir(flag_param,path); i++; } else { //file display(flag_param,path); i++; } } }while(i<argc); return 0; }