READDIR()32/64兼容性问题 [英] readdir() 32/64 compatibility issues
问题描述
我试图得到一些旧的遗留code对新的64位系统的工作,我现在卡住了。下面是我使用的测试存在于当前打破了实际程序功能的一小C文件。
I'm trying to get some old legacy code working on new 64-bit systems, and I'm currently stuck. Below is a small C file I'm using to test functionality that exists in the actual program that is currently breaking.
#define _POSIX_SOURCE
#include <dirent.h>
#include <sys/types.h>
#undef _POSIX_SOURCE
#include <stdio.h>
main(){
DIR *dirp;
struct dirent *dp;
char *const_dir;
const_dir = "/any/path/goes/here";
if(!(dirp = opendir(const_dir)))
perror("opendir() error");
else{
puts("contents of path:");
while(dp = readdir(dirp))
printf(" %s\n", dp->d_name);
closedir(dirp);
}
}
问题:
该操作系统是Red Hat 7.0 x86_64的迈波。
传统code是32位的,并且必须保持这种方式。
The OS is Red Hat 7.0 Maipo x86_64. The legacy code is 32-bit, and must be kept that way.
我已经得到了该计划做工精细用 -m32
标记与 G ++
编译。所产生的问题是在运行时, READDIR()
得到一个64位的inode,然后抛出一个EOVERFLOW错误号,当然没有被打印出来。
I've gotten the compile for the program working fine using the -m32
flag with g++
. The problem that arises is during runtime, readdir()
gets a 64-bit inode and then throws an EOVERFLOW errno and of course nothing gets printed out.
我试过使用 readdir64()
在 READDIR()
来取得了一些成功。我不再得到错误号EOVERFLOW,两条线出来的终端上,但文件本身没有得到打印。我假定这是由于不是缓冲什么的dirent
的期望。
I've tried using readdir64()
in place of readdir()
to some success. I no longer get the errno EOVERFLOW, and the lines come out on the terminal, but the files themselves don't get printed. I'm assuming this is due to the buffer not being what dirent
expects.
我已经尝试使用 dirent64
来试图缓解这个问题,但每当我试图这样我得到:
I've attempted to use dirent64
to try to alleviate this problem but whenever I attempt this I get:
test.c:19:22 error: dereferencing pointer to incomplete type
printf(" %s\n", dp->d_name);
我不知道是否有一种方法来手动移 DP-&GT; d_name
缓冲器,用于的dirent
来与 READDIR使用()
。我在GDB注意到,使用 READDIR()
和的dirent
在 DP-GT&结果; d_name
具有 DP-&GT列出的目录; d_name [1]
,而 readdir64()
和的dirent
给人的第一目录在 DP-方式&gt; d_name [8]
I'm wondering if there's a way to manually shift the dp->d_name
buffer for dirent
to be used with readdir()
. I've noticed in Gdb that using readdir()
and dirent
results in dp->d_name
having directories listed at dp->d_name[1]
, whereas readdir64()
and dirent
gives the first directory at dp->d_name[8]
.
这或以某种方式获得 dirent64
来工作,或者也许我只是我在错误的道路上彻底。
That or somehow get dirent64
to work, or maybe I'm just on the wrong path completely.
最后,值得注意的是,该程序的功能完全没有 -m32
标志包括在内,所以我假设它必须是一个32/64位兼容性错误的地方。任何帮助是AP preciated。
Lastly, it's worth noting that the program functions perfectly without the -m32
flag included, so I'm assuming it has to be a 32/64 compatibility error somewhere. Any help is appreciated.
推荐答案
为了得到一个64位的 ino_t过去
与GCC和Glibc,你需要定义< A HREF =http://linux.die.net/man/7/feature_test_macros相对=nofollow>功能 _XOPEN_SOURCE
和 _FILE_OFFSET_BITS = 64
。
In order to get a 64-bit ino_t
with GCC and Glibc, you need to define the features _XOPEN_SOURCE
and _FILE_OFFSET_BITS=64
.
$ echo '#include <dirent.h>' | gcc -m32 -E -D_XOPEN_SOURCE -D_FILE_OFFSET_BITS=64 - | grep ino
__extension__ typedef unsigned long int __ino_t;
__extension__ typedef __u_quad_t __ino64_t;
typedef __ino64_t ino_t;
__ino64_t d_ino;
我说这个从文档阅读和检查preprocessor,而不是从深经验或以上2 ^ 32 inode编号文件系统测试,所以我不保证你不会遇到其他问题倒行了。
I say this from documentation reading and checking the preprocessor, not from deep experience or testing with a filesystem with inode numbers above 2^32, so I don't guarantee that you won't run into other problems down the line.
这篇关于READDIR()32/64兼容性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!