Given that a simple program:
/* ttyname.c */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
char **tty = NULL;
tty = ttyname(fileno(stderr));
if (tty == NULL)
{
fprintf(stderr, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("%s\n", tty);
exit(EXIT_SUCCESS);
}
compile it as ttyname and invoke it as init , the result as following:
Inappropriate ioctl for device
which means that the error code is ENOTTY.
Why can fprintf(stderr, ....) output to screen when stderr doesn't refer to a terminal device ?