Khan9797
Khan9797

Reputation: 660

Displaying folder contents in c - Run time error

I'm trying to show what folder contains as an output. When I run this program in my harddisk after 1-2 minutes it crashes, beside of crashing part it works just fine. I dont know how I can prevent this. Can anyone help me ?

#include <string.h>
#include <stdio.h>
#include <dirent.h>

void showingFiles(DIR *, char *);

int main(void) {
    DIR *folder;
    char path[350];

    sprintf(path, ".");
    folder = opendir(path);
    showingFiles(folder, path);
    closedir(folder);
    printf("\n\nEnter a key to close this program ");
    getch();
    return 0;
}

void showingFiles(DIR *currentFolder, char *path){
    struct dirent *nextFile;
    DIR *subFolder;
    char copyPath[350];
    strcpy(copyPath, path);
    while ((nextFile = readdir(currentFolder)) != NULL) {
        sprintf(copyPath, "%s//%s", path, nextFile->d_name);
        printf("%s\n", (*nextFile).d_name);
        if ((strcmp(nextFile->d_name, "..")) &&
            strcmp(nextFile->d_name ,".") &&
            (subFolder = opendir(copyPath)) != NULL) {
            deletingFiles(subFolder, copyPath);
        }
    }
    closedir(currentFolder);
}

Upvotes: 0

Views: 125

Answers (1)

chqrlie
chqrlie

Reputation: 144780

There are at least 3 problems in your code that can explain the crash:

  • the buffers used to store the complete pathnames pay be too short and you use an unsafe sprintf to construct them, potentially causing buffer overflows.
  • you never close the subFolder directory handles that you open in the recursive function showingFiles, potentially running out of system handles.
  • you do close the directory handle currentFolder in the function showingFiles(), but is is also closed in the main() function. This causes undefined behavior. As a rule of thumb, always close the handle in the function that opened it and only there.

Less important but issues:

  • To name showingFiles a function that performs a recursive removal of a complete directory tree is a bit misleading.

  • separating directory and pathnames with double slashes // is useless and not portable. You may have been thinking of \\ and converted this Windows specific directory separator into // for Unix portability, but be aware that single forward slashes are supported by the Windows file system handlers, to you should always use / as a directory separator for programs aimed for both Unix and Windows.

Here is a modified version:

#include <dirent.h>
#include <stdio.h>
#include <string.h>

void deleteTree(DIR *, const char *);

int main(void) {
    char path[350] = ".";
    DIR *folder = opendir(path);

    if (folder != NULL) {
        deleteTree(folder, path);
        closedir(folder);
    }
    printf("\n\nEnter a key to close this program ");
    getch();
    return 0;
}

void deleteTree(DIR *currentFolder, const char *path) {
    char copyPath[1024];
    struct dirent *nextFile;
    DIR *subFolder;

    while ((nextFile = readdir(currentFolder)) != NULL) {
        snprintf(copyPath, sizeof(copyPath), "%s/%s", path, nextFile->d_name);
        printf("%s\n", nextFile->d_name);
        if (strcmp(nextFile->d_name,"..")
        &&  strcmp(nextFile->d_name,".")
        &&  (subFolder = opendir(copyPath)) != NULL) {
            deletingFiles(subFolder, copyPath);
            closedir(subFolder);
        }
    }
}

Upvotes: 1

Related Questions