I have a version of a program that used to compile to a *.o
file, but now it does not, and gives a compiler error.
I have tried to compile my code with gcc compiler on Linux, and the compile fails.
#include <stdio.h>
int isatty();
long isatty_(lio_number)
long *lio_number;
{
int file_desc;
if ((*lio_number)==5)
{
file_desc = stdin->_file;
return isatty(file_desc);
}
else
return 0;
}
I expect the command gcc -c isatty.c
to yield isatty.o
but it does not. Instead, I get this message:
isatty.c: In function ‘isatty_’:
isatty.c:11: error: ‘struct _IO_FILE’ has no member named ‘_file’
Advertisement
Answer
Never use any members of the FILE
structure.
Use fileno(stdin)
instead of stdin->_file
.
The member _file
is a MinGW-specific name for the file descriptor, while fileno
is a widely-supported POSIX-compliant function.
Along with that, you may want to #include <unistd.h>
instead of defining isatty
explicitly.
If you’re limited to writing your code this way for some reason, don’t expect it to be portable. Otherwise, this should work:
#include <stdio.h>
#include <unistd.h>
long isatty_(long *lio_number)
{
int file_desc;
if (*lio_number == 5)
{
file_desc = fileno(stdin);
return isatty(file_desc);
}
else
{
return 0;
}
}
What this changes is that it includes unistd.h
which provides a declaration for isatty
, it includes the types of the arguments within the function definition, and it uses fileno(stdin)
instead of stdin->_file
, of which the former is much more portable. It also improves the formatting so others can read your code if they need to.