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);
}
}
The Problem:
The OS is Red Hat 7.0 Maipo x86_64. The legacy code is 32-bit, and must be kept that way.
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.
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.
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);
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]
.
That or somehow get dirent64
to work, or maybe I'm just on the wrong path completely.
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.