#include #include #include #include #include #include enum {PATH_LENGTH=256}; #define STR255 "%255s" void convert_path_to_full(char *full_path, const char *dir) { if(dir[0]=='/') { strcpy(full_path, dir); } else if (dir[0]=='.') { getcwd(full_path,PATH_LENGTH); } else { getcwd(full_path,PATH_LENGTH); strcat(full_path, "/"); strcat(full_path, dir); } if(full_path[strlen(full_path)-1] !='/') strcat(full_path,"/");// добавляем / в конце } /* void print_filetype(int type) { switch (type) { case DT_BLK: printf("b "); break; case DT_CHR: printf("c "); break; case DT_DIR: printf("d "); break; //directory case DT_FIFO: printf("p "); break; //fifo case DT_LNK: printf("l "); break; //Sym link case DT_SOCK: printf("s "); break; //Filetype isn't identified default: printf(" "); break; } } */ /** Расширить строку пробелами. @print_lenth длина до которой надо расширить */ void print_space(int print_lenth, int str_lenth) { while( (print_lenth - str_lenth)>0 ) { putchar(' '); str_lenth++; } } void print_tab(int tab_number) { for(int t=1; td_name[0]=='.' )// пропускаем поддиректории continue; char full_filename[PATH_LENGTH]= {0}; files_number++; print_tab(tab_count);//отступы при рекурсии printf("%4d : ",files_number); //print_filetype(entry->d_type); //не работает для Windows strcpy(full_filename, full_path); strcat(full_filename, entry->d_name); printf("%s", entry->d_name); print_space(20, entry->d_namlen); if (!stat(full_filename, &file_stats)) { print_file_size(file_stats.st_size); printf("\n"); } else { printf("stat failed for this file\n"); perror(0); } } closedir(folder); tab_count--; } int main(int argc, char *argv[]) { char dir[PATH_LENGTH], buf[PATH_LENGTH]; int rez=0; // opterr=0; while ( (rez = getopt(argc,argv,"hf:")) != -1) { switch (rez) { case 'h': printf("This is example of list directory\n"); printf("Usage: clear [options]\n\ -h This help text\n\ -f Specify folder.\n"); printf("Example: %s -f /tmp\n",argv[0]); return 0; case 'f': //printf("folder is \"f = %s\".\n",optarg); strcpy(dir, optarg); convert_path_to_full(buf, dir); printf("ls for folder %s\n",buf); ls(dir); return 0; break; case '?': printf("Unknown argument: %s Try -h for help\n", argv[optind-1]); return 1; }; }; printf("-h help text\n"); return 0; }