Rekursive Dateiauflistung
-
Hallo,
Ich habe mir ein Script geschrieben, welches Ordner für Ordner durchgehen soll und mir die Pfade ausgeben soll. Hier der Code:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdlib.h> #include <pthread.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <pthread.h> #include <time.h> #include <sys/stat.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> char* read_dir (char* path) { char ab_pfad[1000]; DIR *dir; struct dirent *dirzeiger; if((dir = opendir(path)) != NULL) { while((dirzeiger=readdir(dir)) != NULL) { //usleep(100000); // Get absolute path to file memset(ab_pfad, "\0", sizeof(ab_pfad)); strcpy (ab_pfad, path); /* prüfen auf schrägstrich am ende! */ //strcat (ab_pfad, "/"); strcat (ab_pfad, (*dirzeiger).d_name); struct stat tester; FILE* f = fopen(ab_pfad, "r"); if(f == NULL) { perror("\033[1m\033[31mFehler"); printf("\033[0m"); return -1; } if (stat(ab_pfad, &tester) == -1) { printf("stat error\n"); } if (S_ISDIR(tester.st_mode) && strstr(ab_pfad, "..") == NULL && strstr(ab_pfad, ".") == NULL) { printf("%s\n", ab_pfad); strcat (ab_pfad, "/"); fclose (f); read_dir(ab_pfad); } if (!S_ISDIR(tester.st_mode)) { printf("F: %s\n", ab_pfad); fclose(f); } } } closedir (dir); } int main (int argc, char* argv[]) { for (;;) { read_dir(argv[1]); sleep(1); } }
Fehler:
- Too many open files (obwohl fopen() ausgeführt wird)
- Wenn sich anscheinend sehr viele Dateien im Verzeichnis befinden, so wird der Pfad wiederholt. Sprich:
/sys/X11/X11/X11
/sys/X11/X11/X11/X11
/sys/X11/X11/X11/X11
Wenn ich aber mein /opt/lampp/htdocs Ordner durchscanne passiert dies nicht!
Habt Ihr mir vll einen Code ?
Oder könnt Ihr mir weiterhelfen ?Ich danke
-
EDIT:
- Wenn sich anscheinend sehr viele Dateien im Verzeichnis befinden, so wird der Pfad wiederholt. Sprich:
/sys/X11/X11/X11
/sys/X11/X11/X11/X11
/sys/X11/X11/X11/X11
Dies ist kein Fehler!
Diese Ordner existieren auf dem System, wirklich
- Wenn sich anscheinend sehr viele Dateien im Verzeichnis befinden, so wird der Pfad wiederholt. Sprich:
-
Du öffnest Dateien und schließt sie nicht immer.
Zudem versteh ich nicht ganz deinen check auf "." und "..".#include <stdio.h> /* printf, perror */ #include <sys/types.h> /* opendir, readdir, stat */ #include <dirent.h> /* opendir, readdir */ #include <linux/limits.h> /* PATH_MAX */ #include <sys/stat.h> /* stat */ #include <unistd.h> /* stat */ #include <string.h> /* strncpy, strncat, strcmp, strerror */ #include <errno.h> /* errno */ void print_rec_dir(const char *path) { struct dirent *entry; struct stat entrystat; char fullpath[PATH_MAX]; DIR *dir = opendir(path); if (!dir) { fprintf(stderr, "opendir(\"%s\"): %s", path, strerror(errno)); return; } while ((entry = readdir(dir))) { strncpy(fullpath, path, PATH_MAX); strncat(fullpath, "/", PATH_MAX); strncat(fullpath, entry->d_name, PATH_MAX); if (stat(fullpath, &entrystat)) { /* permission denied, invalid links... */ fprintf(stderr, "stat(\"%s\"): %s\n", fullpath, strerror(errno)); continue; } if (S_ISDIR(entrystat.st_mode)) { if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) { printf("D: %s\n", fullpath); print_rec_dir(fullpath); } } else /* Dateien, Sockel, Fifo, Links, zeichen- und blockorientierte Geräte */ printf("F: %s\n", fullpath); } closedir(dir); } int main(int argc, char *argv[]) { int i; for (i = 1; i < argc; ++i) print_rec_dir(argv[i]); return 0; }
Bei Fragen dazu kannst du mal in FAQ gucken.