简体   繁体   English

使用readdir_r读取目录中的文件,并使用qsort进行排序

[英]Read files in a directory using readdir_r and sort using qsort

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. 我试图用C编写一个程序,该程序从目录中读取文件并检测每个文件的名称,用户,组和大小。 The information for each file is stored in a struct array and sorted by file name using qsort. 每个文件的信息都存储在struct数组中,并使用qsort按文件名排序。 The sorted files are then printed to the screen. 然后将排序后的文件打印到屏幕上。 This program must use readdir_r, getpwuid_r and getgrgid_r. 该程序必须使用readdir_r,getpwuid_r和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. 我不明白必须如何实现必须为这些功能的“ _r”版本提供的多个参数。 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? 谁能帮助我了解在这种情况下如何正确使用“ _r”功能? 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;
}

Here is an example of using readdir_r() ... you should be able to figure out the others based upon this example: 这是使用readdir_r()的示例...您应该能够根据此示例找出其他示例:

#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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM