Why stdout is not equal to 1 in gnu coreutils?

lllllllllllll

Test platform is 32 bit x64 Linux, coreutils 8.5.

In the source code of base64, fwrite will use stdout to output the base64 encoded string

  1. When I use ltrace to print all the libc call, we can see that stdout is equal to 0xb772da20

    __libc_start_main(0x8048eb0, 2, 0xbf892f74, 0x804cb50, 0x804cbc0 <unfinished ...>
    
    strrchr("base64", '/')                                                         = NULL
    setlocale(6, "")                                                               = "en_US.UTF-8"
    bindtextdomain("coreutils", "/usr/share/locale")                               =      "/usr/share/locale"
    textdomain("coreutils")                                                        = "coreutils"
    __cxa_atexit(0x804a3b0, 0, 0, 0xbf892f74, 2)                                   = 0
    getopt_long(2, 0xbf892f74, "diw:", 0x0804d1a0, NULL)                           = -1
    fopen64("testbase64", "rb")                                                    = 0x8591878
    fileno(0x8591878)                                                              = 3
    posix_fadvise64(3, 0, 0, 0, 0)                                                 = 0
    fread_unlocked(0xbf89225c, 1, 3072, 0x8591878)                                 = 900
    fwrite_unlocked("Ly8gcXVpY2tTb3J0LmMKI2luY2x1ZGUg"..., 1, 76, 0xb772da20)      = 76
    
  2. When I modify the code of base64 like this:

    int main (int argc, char **argv)
    {
    
      printf("%p \n", stdout);
      int opt;
      FILE *input_fh;
      const char *infile;
      .....
    

The output is still 0xb772da20 , it is strange to me as this is the first line of base64.c.

I grep in the lib folder of coreutils

grep stdout *.h

and I don't see any predefine of stdout.

Could anyone give me some help about why stdout will be defined as "0xb772da20" , not 1, not 0?

Basile Starynkevitch

According to stdout(3), the stdout is a pointer (to some FILE opaque structure), because it is a file stream. It is not a file descriptor. Its file descriptor is STDOUT_FILENO which indeed is 1.

On my Gnu libc Linux system, I have near line 169 of /usr/include/stdio.h :

/* Standard streams.  */
extern struct _IO_FILE *stdin;      /* Standard input stream.  */
extern struct _IO_FILE *stdout;     /* Standard output stream.  */
extern struct _IO_FILE *stderr;     /* Standard error output stream.  */
/* C89/C99 say they're macros.  Make them happy.  */
#define stdin stdin
#define stdout stdout
#define stderr stderr

Before that (line 48) there is

typedef struct _IO_FILE FILE;

See also this answer to a related question.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GNU coreutils and GNU Bash

From Dev

Perl implementation of the GNU Coreutils

From Dev

rbenv missing GNU coreutils

From Dev

Is gnu coreutils sort broken?

From Dev

GNU coreutils need suggestions

From Dev

GNU coreutils `sort` behave differently

From Dev

Why is !0 equal to 1 and not -1?

From Dev

Is grep part of coreutils? (If not, why?)

From Dev

Any options to replace GNU coreutils on Linux?

From Dev

How to make sort consider blanks (GNU coreutils)

From Dev

how does the gnu coreutils `date` work?

From Dev

GNU make - multiple jobs but MAKEFLAGS has --jobs equal to 1

From Dev

GNU make - multiple jobs but MAKEFLAGS has --jobs equal to 1

From Java

Why does !!1=="1" equal true and !!2=="2" equal false?

From Java

Why does 11010100 << 1 equal 110101000, not 10101000?

From Dev

Why COUNT(*) is equal to 1 without FROM clause?

From Java

Why is not variance of normalized data by sklearn equal 1?

From Dev

Why does bitwise "not 1" equal -2?

From Dev

Why is 1 << 3 equal to 8 and not 6?

From Dev

Why does 11010100 << 1 equal 110101000, not 10101000?

From Dev

GNU ls from Coreutils missing OS X ACL implementation

From Dev

Get "timeout" command from GNU coreutils 8 on RHEL 5.7 machine

From Dev

get a part of a binary file using gnu-coreutils, bash

From Dev

Preserve original order if numeric value is equal in coreutils sort?

From Dev

Why is coreutils sort slower than Python?

From Dev

GNU parallel sorted stdout and stderr

From Dev

GNU parallel sorted stdout and stderr

From Dev

Why does list[::-1] not equal list[:len(list):-1]?

From Dev

Why is 3-1*8+2*3 equal to 1