おまじないではない#include その2

http://d.hatena.ne.jp/ytetsuwo/20110820/p1
の続き

stdio.hからincludeされている/usr/include/_types.hの抜粋は以下の通り

    32  #include <sys/cdefs.h>
    33  #include <machine/_types.h>
    34  
    35  /*
    36   * Standard type definitions.
    37   */
    38  typedef __uint32_t      __blksize_t;    /* file block size */
    39  typedef __int64_t       __blkcnt_t;     /* file block count */
    40  typedef __int32_t       __clockid_t;    /* clock_gettime()... */
    41  typedef __uint32_t      __fflags_t;     /* file flags */
    42  typedef __uint64_t      __fsblkcnt_t;
    43  typedef __uint64_t      __fsfilcnt_t;
    44  typedef __uint32_t      __gid_t;
    45  typedef __int64_t       __id_t;         /* can hold a gid_t, pid_t, or u
id_t */
    46  typedef __uint32_t      __ino_t;        /* inode number */
    47  typedef long            __key_t;        /* IPC key (for Sys V IPC) */
    48  typedef __int32_t       __lwpid_t;      /* Thread ID (a.k.a. LWP) */
    49  typedef __uint16_t      __mode_t;       /* permissions */
    50  typedef int             __accmode_t;    /* access permissions */
    51  typedef int             __nl_item;
    52  typedef __uint16_t      __nlink_t;      /* link count */
    53  typedef __int64_t       __off_t;        /* file offset */
    54  typedef __int32_t       __pid_t;        /* process [group] */

C言語は型のビット数が厳密に定義されていないため、いろいろと不都合が生じる場合がある。そこで、まずmachine/_types.hでそのビット数を厳密に決めた型を定義している。このファイルは環境(32bitOSとか64bitOSとか)によって異なるので注意。で、そのヘッダをincludeして他のヘッダファイルで利用する様々な型を定義している。ビット数固定の型をtypedefしておくて、どのような環境でも型の大きさが固定になって便利なになるわけですね。machine/_types.h は以下の通り

    48  /*
    49   * Basic types upon which most other types are built.
    50   */
    51  typedef __signed char           __int8_t;
    52  typedef unsigned char           __uint8_t;
    53  typedef short                   __int16_t;
    54  typedef unsigned short          __uint16_t;
    55  typedef int                     __int32_t;
    56  typedef unsigned int            __uint32_t;
    57  typedef long                    __int64_t;
    58  typedef unsigned long           __uint64_t;

64bit環境なんでlongが64bitになってるんですね。32bit環境の場合はこの行の内容が変わってきます。

さて、stdio.hに戻ってくると、

    47  typedef __off_t         fpos_t;
    48  
    49  #ifndef _SIZE_T_DECLARED
    50  typedef __size_t        size_t;
    51  #define _SIZE_T_DECLARED
    52  #endif

はい、実際の型を定義してます。stdio.hでは3つの型を定義するとC言語の仕様で決められていて、fpos_tとsize_tはその2つなんです。で、あと1つは何かというと皆さんご存じFILEです。で、この後、FILE構造体の定義が記述されているわけですが、それはまたこんど。