/* * Upraveno z Pavel Herout * * s317.c v.2.0 * * Udaje o souborech a adresarich ve Windows * ========================================= */ #include #include #include #include #include #define JMENO_SOUBORU "DirListRecursive.txt" #define MASKA_VSE "*.*" /* Rekurzivni verze predcasne koncila po vice volanich prikazu "return 1", proto je navratovy typ funkce "vypis()" "void". */ void vypis(FILE *f, char *adr, char *mask, bool r, long *p_a, long *p_s) { struct _finddata_t data; struct tm cas; char adresar[FILENAME_MAX], adresar_s_maskou[FILENAME_MAX]; intptr_t vysledek; sprintf(adresar_s_maskou, "%s\\%s", adr, MASKA_VSE); vysledek = _findfirst(adresar_s_maskou, &data); if (vysledek == -1) { fprintf(f, "No file matches the mask '%s'\n", adresar_s_maskou); } else { do { if ((data.attrib & _A_SUBDIR) && data.name[0] != '.') { if (r) { sprintf(adresar, "%s\\%s", adr, data.name); vypis(f, adresar, mask, r, p_a, p_s); } } } while (_findnext(vysledek, &data) == 0); } _findclose(vysledek); sprintf(adresar_s_maskou, "%s\\%s", adr, mask); vysledek = _findfirst(adresar_s_maskou, &data); if (vysledek != -1) { do { fprintf(f, "%s\\%s", adr, data.name); fprintf(f, "\t%lu", data.size); cas = *localtime(&data.time_write); fprintf(f, "\t%02d.%02d.%4d %02d:%02d:%02d", cas.tm_mday, cas.tm_mon + 1, cas.tm_year + 1900, cas.tm_hour, cas.tm_min, cas.tm_sec); if (data.attrib == _A_NORMAL) { fprintf(f, "\tNormal"); } else { if ((data.attrib & _A_RDONLY) != 0) { fprintf(f, "\tReadOnly"); } if ((data.attrib & _A_HIDDEN) != 0) { fprintf(f, "\tHidden"); } if ((data.attrib & _A_SYSTEM) != 0) { fprintf(f, "\tSystem"); } if ((data.attrib & _A_VOLID) != 0) { fprintf(f, "\tVolumeID"); } if ((data.attrib & _A_SUBDIR) != 0) { fprintf(f, "\tDirectory"); } if ((data.attrib & _A_ARCH) != 0) { fprintf(f, "\tArchive"); } } fprintf(f, "\n"); ((data.attrib & _A_SUBDIR) != 0) ? (*p_a)++ : (*p_s)++; } while (_findnext(vysledek, &data) == 0); } _findclose(vysledek); } int precti_retezec(FILE *f, char *s) { int d; if (fgets(s, FILENAME_MAX, f) == NULL) { return 1; } else { d = strlen(s) - 1; if (s[d] == '\n') s[d] = '\0'; }; return 0; } void zapis_zpravu(FILE *f) { fprintf(f, "You have run Recursive Directory Listing Program.\n"); fprintf(f, "This program must have a text file %s in the same directory where it is placed.\n", JMENO_SOUBORU); fprintf(f, "Copy a path of the folder that you want to list from the Address bar to the first line of this file.\n"); fprintf(f, "Write the file mask, i.e. \"*.*\" for all files, without quotation marks \" \" to the second line of this file.\n"); fprintf(f, "Write the character \"r\" without quotation marks \" \" to the third line of this file if you want recursive list (i.e. including subdirectories), otherwise you can leave it empty.\n"); fprintf(f, "Save this file, close it and run this program.\n"); } int main() { FILE *f; char adresar[FILENAME_MAX], maska[FILENAME_MAX], r; bool neuspech = 0, rekurzivne; long pocet_adresaru, pocet_souboru; pocet_adresaru = pocet_souboru = 0; if ((f = fopen(JMENO_SOUBORU, "r")) == NULL) { f = fopen(JMENO_SOUBORU, "a"); zapis_zpravu(f); fclose(f); return 1; }; if (precti_retezec(f, adresar) == 1) { neuspech = true; }; if (precti_retezec(f, maska) == 1) { neuspech = true; }; if (!neuspech) { r = fgetc(f); } fclose(f); f = fopen(JMENO_SOUBORU, "a"); if (neuspech) { zapis_zpravu(f); fclose(f); return neuspech; } rekurzivne = (r == 'r') || (r == 'R'); fprintf(f, "\nPathName\tBytes\tModified\tAttributes\n"); vypis(f, adresar, maska, rekurzivne, &pocet_adresaru, &pocet_souboru); fprintf(f, "\n%ld directories\n", pocet_adresaru); fprintf(f, "%ld files\n", pocet_souboru); fclose(f); return 0; }