views:

55

answers:

1

I am currently trying to count the number of bytes consumed by files in a certain directory. It recursively goes through all the folders on the current directory and counts the bytes of the files.

When I recursively call the function rec_bytes, I print off "Go in"... but when it returns the value... It segfaults.

I labeled the problematic line in the code below.

I think the problem has to do with open/closing directories.

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

int rec_Bytes(char path[])
{
    int bytesSum = 0;
    printf("PathX %s\n", path);
    DIR *mydir = opendir(path); // Look in current directory

    struct dirent *entry = NULL;

    while((entry = readdir(mydir))) /* If we get EOF, the expression is 0 and
                                     * the loop stops. */
        {
        if (!isDir(entry->d_name))  // Check to see if the entry is a directory of a file
        {
            char tempPath[] = "";
            strcat(tempPath, path);
            strcat(tempPath,"/");
            strcat(tempPath,entry->d_name);
            int tempSum = fileSize(tempPath);   // Get file size
            bytesSum += tempSum;        // Add to sum
                printf("%s\t%d\n", entry->d_name, tempSum);
        }
        else    // The current entry is a directory
        {
            if ((strcmp((entry->d_name),"..") != 0) && (strcmp((entry->d_name),".")) != 0)
            {
                printf("Directory%s\n", entry->d_name);
                char tempPath[] = "";
                strcat(tempPath, path);
                strcat(tempPath,"/");
                strcat(tempPath,entry->d_name);
                printf("Go in\n");


                int tempSum = rec_Bytes(tempPath); <<<<<< Get segmentation fault here.


                printf("Come Out%d\n", tempSum);
                bytesSum += tempSum;
                    printf("%s\t%d\n", entry->d_name, tempSum);
            }
        }
        }
        closedir(mydir);
    printf("XXXX\t%s\t%d\n", path, bytesSum);
    return bytesSum;
}

// Thanks to : http://cboard.cprogramming.com/cplusplus-programming/117431-how-tell-if-file-directory.html
int isDir(const char* target)
{
   struct stat statbuf;
   stat(target, &statbuf);
   return S_ISDIR(statbuf.st_mode);
}
+4  A: 

Your problem is with lines like this...

char tempPath[] = "";

This will allocate a buffer with one byte, that byte being a null character. There is no room in that buffer for any longer string.

Basically, C does not have dynamically resizable strings. It has null terminated strings that live within fixed-size arrays of characters. This creates an issue when you won't know the length of the string until you've finished building it, of course.

Try something like...

char tempPath[5000] = "";

as a quick fix. Also, look up strncat - it's less likely to segfault. There's a printf variant as well, but I use too much C++ these days.

EDIT

Actually, the segfault is probably due to those strcats and printfs corrupting the stack. The segfault is probably when a function tries to return. The basic issue is the too-small string buffers thing, though.

Whoops!

The real quick fix is...

char tempPath[5000];
tempPath [0] = 0;

Otherwise, it won't always get initialised to an empty string when you expect it to.

Steve314
Cool quick fix ftw, Thanks !!
confusedEj