L
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.