Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何包含linux_dirent64结构使用的s64和u64类型?_C_Linux - Fatal编程技术网

C 如何包含linux_dirent64结构使用的s64和u64类型?

C 如何包含linux_dirent64结构使用的s64和u64类型?,c,linux,C,Linux,我试图在64位Ubuntu上使用getdents64Linux系统调用。我的代码是基于getdents的一个最小示例,如下所示: #define _GNU_SOURCE #include <dirent.h> /* Defines DT_* constants */ #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>

我试图在64位Ubuntu上使用
getdents64
Linux系统调用。我的代码是基于
getdents
的一个最小示例,如下所示:

    #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);
}
当我编译我的程序时,我得到以下错误:

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\u dirent64
的例子,他们只是
\include
。当我尝试这样做时,我得到了一个错误:

hc-24@HP24:~$ gcc v.c 
love.c:8:19: fatal error: types.h: No such file or directory
compilation terminated.
所以我不得不导入这两种类型(
s64
u64
),我完全没有任何线索。非常感谢您的任何建议。谢谢

我猜
是linux内核源代码中的一个头

有关源代码,请参阅链接

以下是该文件的摘录:

#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;
#包括
类型定义u64 uint64;
typedef s64 int64_t;
我猜
是linux内核源代码中的一个标题

有关源代码,请参阅链接

以下是该文件的摘录:

#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;
#包括
类型定义u64 uint64;
typedef s64 int64_t;

您的
linux\u目录64
与手册页中的不同。因此,也许您应该尝试一下本机的
man
?“这些不是您感兴趣的接口。请查看readdir(3)中符合POSIX标准的C库接口。本页记录了裸内核系统调用接口。”来自链接的手册页。也许你应该改用
readdir
函数?@EugeneSh。对于getdents,我使用的getdents64具有不同的结构。手册页只记录linux目录,而不是linux目录64,这是不同的。我从readdir的源代码中获得了linux_dirent64。@JonatanE我绝对必须使用
getdents64
不幸的是,使用@JonatanE的答案,我的代码还有很多其他问题。事实证明,getdents和getdents64比我想象的要不同得多。我会处理它并编辑我的问题。但看起来我遇到的具体问题已经解决了。谢谢大家!您的
linux\u目录64
与手册页中的目录不同。因此,也许您应该尝试一下本机的
man
?“这些不是您感兴趣的接口。请查看readdir(3)中符合POSIX标准的C库接口。本页记录了裸内核系统调用接口。”来自链接的手册页。也许你应该改用
readdir
函数?@EugeneSh。对于getdents,我使用的getdents64具有不同的结构。手册页只记录linux目录,而不是linux目录64,这是不同的。我从readdir的源代码中获得了linux_dirent64。@JonatanE我绝对必须使用
getdents64
不幸的是,使用@JonatanE的答案,我的代码还有很多其他问题。事实证明,getdents和getdents64比我想象的要不同得多。我会处理它并编辑我的问题。但看起来我遇到的具体问题已经解决了。谢谢大家!我不认为这是一个恰当的解决办法。原来我也使用GeDous64有点不正确,所以我会修正我的问题在未来的人的代码(加上我,因为我需要它工作,哈哈),我不认为这是一个合适的解决方案。这就行了。事实证明,我使用getdents64时也有点不正确,所以我会在我的问题中为将来的人们修正代码(加上我,因为我需要它来工作,哈哈)