[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fileno(stdin) versus STDIN_FILENO and friends



Shouldn't STDOUT_FILENO etc always be valid, regardless of thread?

Consider this code snippet installed in the RTEMS shell as "filenos":
===
static int
filenos_cmd(int argc, char **argv)
{
    int stdin_fileno = fileno(stdin),
        stdout_fileno = fileno(stdout),
        stderr_fileno = fileno(stderr);

    fprintf(stderr,
        "fileno(stdin),  STDIN_FILENO:  %d %d\n"
        "fileno(stdout), STDOUT_FILENO: %d %d\n"
        "fileno(stderr), STDERR_FILENO: %d %d\n",
        stdin_fileno,  STDIN_FILENO,
        stdout_fileno, STDOUT_FILENO,
        stderr_fileno, STDERR_FILENO);

    fprintf(stderr, "- %s\n",
        (stdin_fileno == STDIN_FILENO &&
        stdout_fileno == STDOUT_FILENO &&
        stderr_fileno == STDERR_FILENO) ? "OK" : "BAD");

    return 0;
}
====

Called from the console:

[/] # filenos                                                                                                                                                                                                                             
fileno(stdin),  STDIN_FILENO:  0 0                                                                                                                                                                                                         
fileno(stdout), STDOUT_FILENO: 1 1                                                                                                                                                                                                         
fileno(stderr), STDERR_FILENO: 2 2                                                                                                                                                                                                         
- OK                                                                                                                                                                                                                                         
[/] #                                                                                                                                                                 

Called via telnet:

[/] # filenos
fileno(stdin),  STDIN_FILENO:  7 0
fileno(stdout), STDOUT_FILENO: 8 1
fileno(stderr), STDERR_FILENO: 9 2
- BAD
[/] # 

Shouldn't the macros reference something that changes with the thread context to appropriate file descriptors without forcing reference to <stdio.h>?

Peter
-----------------
Peter Dufault
HD Associates, Inc.      Software and System Engineering