代码之家  ›  专栏  ›  技术社区  ›  Alex V

如何包括Linux目录64结构使用的S64和U64类型?

  •  0
  • Alex V  · 技术社区  · 7 年前

    我正在尝试使用 getdents64 64位Ubuntu上的Linux系统调用。我的代码,这是基于 getdents man page ,如下所示:

        #define _GNU_SOURCE
    #include <dirent.h>     /* Defines DT_* constants */
    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/syscall.h>
    #include <sys/types.h>
    #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
    
    struct linux_dirent64 {
        u64 d_ino;
        s64 d_off;
        unsigned short d_reclen;
        unsigned char d_type;
        char d_name[0];
    };
    
    
    #define BUF_SIZE 1024
    
    int
    main(int argc, char *argv[])
    {
        int fd, nread;
        char buf[BUF_SIZE];
        struct linux_dirent64 *d;
        int bpos;
        char d_type;
    
        fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
        if (fd == -1)
            handle_error("open");
    
        for ( ; ; ) {
            nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
            if (nread == -1)
                handle_error("getdents64");
    
            if (nread == 0)
                break;
    
            printf("--------------- nread=%d ---------------\n", nread);
            printf("inode#    file type  d_reclen  d_off   d_name\n");
            for (bpos = 0; bpos < nread;) {
                d = (struct linux_dirent64 *) (buf + bpos);
                printf("%8d  ", d->d_ino);
                d_type = *(buf + bpos + d->d_reclen - 1);
                printf("%-10s ", (d_type == DT_REG) ?  "regular" :
                                 (d_type == DT_DIR) ?  "directory" :
                                 (d_type == DT_FIFO) ? "FIFO" :
                                 (d_type == DT_SOCK) ? "socket" :
                                 (d_type == DT_LNK) ?  "symlink" :
                                 (d_type == DT_BLK) ?  "block dev" :
                                 (d_type == DT_CHR) ?  "char dev" : "???");
                printf("%4d %10lld  %s\n", d->d_reclen,
                        (long long) d->d_off, d->d_name);
                bpos += d->d_reclen;
            }
        }
    
        exit(EXIT_SUCCESS);
    }
    

    真正重要的部分(我的代码中断的地方)是:

    struct linux_dirent64 {
        u64 d_ino; // this is this problem
        s64 d_off; // this too
        unsigned short d_reclen;
        unsigned char d_type;
        char d_name[0];
    };
    

    当我编译程序时,得到以下错误:

    hc-24@HP24:~$ gcc v.c 
    v.c:14:5: error: unknown type name ‘u64’
         u64 d_ino;
         ^
    v.c:15:5: error: unknown type name ‘s64’
         s64 d_off;
         ^
    

    我想问题是我包括 sys/types.h 试图获得 s64 和 u64 . 我找到了一个使用 linux_dirent64 在网上,他们只是 #include <types.h> . 当我尝试时,我得到了错误:

    hc-24@HP24:~$ gcc v.c 
    love.c:8:19: fatal error: types.h: No such file or directory
    compilation terminated.
    

    所以我必须引进这两种类型( S64 和 U64 )我完全不知道。任何建议都非常感谢。谢谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   JonatanE    7 年前

    我想 <types.h> 是Linux内核源代码中的头文件。

    见 this 源的链接。

    以下是该文件的摘录:

    #if defined(__GNUC__)
    typedef u64         uint64_t;
    typedef u64         u_int64_t;
    typedef s64         int64_t;
    #endif
    

    所以,我想您可以把它添加到代码中,以便编译它。

    #include <stdint.h>
    typedef u64 uint64_t;
    typedef s64 int64_t;