views:

38

answers:

2

Ok I have something like this:

struct dirent *dp;
DIR *dir;
char fullname[MAXPATHLEN];
char** tmp_paths = argv[1]; //Not the exact code but you get the idea.

...

while ((dp = readdir(dir)) != NULL)
{
    struct stat stat_buffer;

    sprintf(fullname, "%s/%s", *tmp_paths, dp->d_name);

    if (stat(fullname, &stat_buffer) != 0)
        perror(opts.programname);

    /* Processing files */
    if (S_ISDIR(stat_buffer.st_mode))
    {
        nSubdirs++;
        DIRECTORYINFO* subd = malloc(BUFSIZ);
    }

    /* Processing subdirs */
    if (S_ISREG(stat_buffer.st_mode))
    {
        nFiles++;
        FILEINFO *f = malloc(BUFSIZ);
    }
}

How do I go about reading in the file names and subdirectory names into my own structure DIRECTORYINFO and FILEINFO? I've gone through stat.h and haven't found anything useful.

+1  A: 

Take a look at this question and its answers. You probably want to use dirent->d_name.

Nathon
+1  A: 

In the UNIX world the name is not part of the file, so stat(2) cannot retrieve information about it. But in your code you have the name as dp->d_name, so you can copy that string into your own data structure. That should be fairly simple.

If this is not your problem I didn't understand the question.

Roland Illig
Yup that works thanks. It's just that I was under the impression that d_name only works for directories.
jon2512chua
It is very common for the `struct` types in UNIX that the fields are prefixed with a common *something* and an underscore. For example `struct dirent` has `d_*`, `struct stat` has `st_*`, `struct time` has `tm_*`.
Roland Illig