13

I was looking through /usr/include/stdio.h

and happened to come across the following piece of code.

/* 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

My question is, where is this stucture struct _IO_FILE declared, i want to see the layout. and also the code mentions

#define stdin stdin

How is this supposed to work?

ArunMKumar
  • 718
  • 2
  • 5
  • 14
  • What OS/compiler are you using? – templatetypedef May 07 '13 at 16:46
  • **Operating System :** 3.2.0-41-generic-pae #66-Ubuntu SMP Thu Apr 25 03:50:20 UTC 2013 – ArunMKumar May 07 '13 at 16:48
  • **Compiler:** gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 – ArunMKumar May 07 '13 at 16:48
  • Have you tried a grep/awk search through the libc files already? – Maggy May May 07 '13 at 16:53
  • 1
    Knowing what the structure looks like isn't going to be a useful as you might expect. It will vary from one OS to another, and even from one release of your standard library to another. Any code you write that depends on these internal details will be extremely non-portable. If you're trying to accomplish something specific, there are likely to be better ways to do it. – Keith Thompson May 07 '13 at 17:23
  • 1
    wasn't trying to make any thing non-portable, just a habit i got from developing device drivers in Linux. You have no library there so u got to dig into everything. – ArunMKumar May 07 '13 at 17:28
  • When I look at an .h file that's part of a library, I use the preprocessor directly. E.g. `cpp <<< "#include " | cat -n | less`. Let the shell do the work. – Braden Best Oct 06 '15 at 02:05

2 Answers2

19

It's in /usr/include/libio.h (which is included in stdio.h) and struct defintion is:

struct _IO_FILE {
  int _flags;           /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
  char* _IO_write_base; /* Start of put area. */
  char* _IO_write_ptr;  /* Current put pointer. */
  char* _IO_write_end;  /* End of put area. */
  char* _IO_buf_base;   /* Start of reserve area. */
  char* _IO_buf_end;    /* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
#if 0
  int _blksize;
#else
  int _flags2;
#endif
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

I searched for the definition by looking at the preprocessed code:

echo '#include<stdio.h>' | gcc -E -
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
P.P
  • 117,907
  • 20
  • 175
  • 238
  • This was really helpful @KingsIndian, i don't know much about the echo command so can yo please give me some clarification on the echo command that you used along with the parameters that you passed. – ArunMKumar May 07 '13 at 17:17
  • 1
    That's what it looks like on your system. It could be completely different on another system. – Keith Thompson May 07 '13 at 17:24
  • That's same as including the stdio.h in a C program and looking at the preprocessed code. `-E` options tells gcc to stop after the preprocessing stage after all the #include's. – P.P May 07 '13 at 17:24
3

You can do search in this way:

grep -rn "struct _IO_FILE {" --include="*.h" /usr/include
Eric
  • 22,183
  • 20
  • 145
  • 196