Read files in a directory using readdir_r and sort using qsort

1.9k views Asked by At

I am trying to write a program in C that reads files from a directory and detects the name, user, group, and size of each file. The information for each file is stored in a struct array and sorted by file name using qsort. The sorted files are then printed to the screen. This program must use readdir_r, getpwuid_r and getgrgid_r. I do not understand how the multiple arguments that must be supplied for the "_r" versions of these functions must be implemented to achieve my goal. I am also receiving several "request for member ‘name’ in something not a structure or union" errors (also occur for 'size','user' and 'group').

Can anyone help me understand how I can utilize the "_r" functions properly in this situation? The man pages were not clear enough for me to understand.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <stddef.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>

int main(int argc, char* argv[])
{

DIR *mydir;
struct dirent *myfile;
struct stat mystat;
struct passwd  *pwd;
struct group   *grp;
struct dirent *result;


struct entry
{
    char *name;
    char *user;
    char *group;
    int size;
};

struct entry entries[1024];

if(argc != 2)
{
    perror("must supply a directory");
    return -1;
}

mydir = opendir(argv[1]);
if(mydir==NULL)
{
    perror("Cannot find directory");
    return -1;
}

char buf[1024];
while((myfile = readdir_r(mydir, myfile, )) != NULL)
{
    entries.name = myfile->d_name;
    stat(buf, &mystat);
    entries.size = mystat.st_size;

    /* store owner's name in struct if it is found using getpwuid_r(). */
    if ((pwd = getpwuid_r(mystat.st_uid, , , , ,)) != NULL)
            entries.user = pwd->pw_name;
    else
         perror("user not found");


    /* store group name in struct if it is found using getgrgid_r(). */
    if ((grp = getgrgid_r(mystat.st_gid, , , , ,)) != NULL)
             entries.group = grp->gr_name;
    else
             perror("group not found");


 }

 int cmpfunc( const void *a, const void *b)
 {
     char const *aa = (char const *)a;
     char const *bb = (char const *)b;

     return strcmp(aa, bb);
 }

 qsort(entries, 4, sizeof(int), cmpfunc);

 int i = 0;
 for(int i; i < sizeOf(entries); i++)
 {
        printf("%s %s %llu %s\n", entries.user, entries.group,
        entries.size, entries.name);
 }

        closedir(mydir);

        return 0;
}
1

There are 1 answers

0
TonyB On

Here is an example of using readdir_r() ... you should be able to figure out the others based upon this example:

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

int main(int argc, char *argv[])
{

    DIR *mydir;
    struct dirent myfile;
    struct dirent *result;

    int rc;

    if (argc != 2) {
        perror("must supply a directory");
        return -1;
    }

    mydir = opendir(argv[1]);
    if (mydir == NULL) {
        perror("Cannot find directory");
        return -1;
    }

    while ((rc = readdir_r(mydir, &myfile, &result)) == 0 && result != NULL ) {
        printf("myfile.entryName: -->%s<--  result->d_name: -->%s<--\n",
              myfile.d_name,
              result->d_name);
    }

    closedir(mydir);

}