libc.a中FILE结构和相应的函数指针的分析

来源:互联网 发布:Mac怎样设置快捷键 编辑:程序博客网 时间:2024/06/10 08:31
fopen()函数在fopen.c文件里面定义。
调用了__fopen()函数,里面第三个参数是-1,而fdopen里面也调用了__fopen函数。
里面第三个参数是fd。
调用的第二个函数是__file()函数,主要是分配一个FILE结构,并且初始化相应的读写相关的函数指针。
FILE *fopen F2(CONST char *, name, CONST char *, mode)

{
  register int fd;            /* opened file descriptor */
  short flags;                /* flag settings */

  return (fd = __fopen(name, mode, -1, &flags)) < 0
    ? (FILE *) (NULL)
    : __file((FILE *) NULL, fd, flags);
}
__fopen()函数功能是看是否存在要打开的文件,不存在则创建一个文件。


__file函数的功能是申请一个FILE结构,然后初始化,特别是相应的读写相关的函数进行初始化,和函数指针数组有密切的关系,读有关的是filtable[],写有关的是flstable[]。
先是遍历FILE结构组成的链表看是否已经打开了这个文件,如果已经被打开了,则不能第二次打开,返回NULL。
然后是malloc一个FILE结构,进行初始化。

brdupdate函数在_update.c文件里面定义,调用了__brdonly()函数。
int __brdupdate F1(register FILE *, fp)

{
  if (TESTFLAG(fp, (_IOWRITE | _IOEOF | _IOERR)))
    return EOF;

  SETFLAG(fp, _IOREAD);

  return __brdonly(fp);
}

__brdonly()函数在_bread.c文件里面定义,调用了_brd()函数。
  SETFILBUF(fp, __brd); 这一句也设置了一个FILE结构里面的成员,
函数指针__brd。
_brd()函数在_bread.c文件里面定义,调用了__ioread()函数。
__ioread()函数在_ioread.c文件里面定义,终于调用了read函数。
__stdiosize_t __ioread F3(int, fd, char *, p, __stdiosize_t, n)

{
  register int r;            /* bytes read by read call */

  if (n > INT_MAX)
    n = INT_MAX;
  do
    r = read(fd, p, n);
  while (r == -1 && (errno == EINTR || errno == EAGAIN));

  return r < 0 ? 0 : r;
}




/*                _ f i l e
 *
 * Allocate a stream descriptor
 *
 * Allocate and initialise a FILE structure. If the pointer passed
 * to the function is NULL, a FILE will be allocated, otherwise
 * the one specified will be used. The function will return
 * a pointer to the FILE structure, or NULL if it fails.
 */

#include "stdiolib.h"

/*LINTLIBRARY*/

#if    _IOREAD != 1 || _IOWRITE != 2
    _IOREAD == 1 and _IOWRITE == 2 assumed
#endif

static int (*filtable[]) P((FILE *)) =
  {__brdupdate, __brdonly, __bffil};

static int (*flstable[]) P((int, FILE *)) =
  {__bwrupdate, __bffls, __bwronly};

FILE *__file F3(register FILE *, fp, int, fd, register short, flags)

{

/* Retain flags and insert if necessary */
  if (fp != NULL) {
    if (TESTFLAG(fp, _IOINSERT)) {
      CLEARFLAG(fp, _IOINSERT);
      FINSERT(fp);
    }
    flags |= GETFLAG(fp, _IORETAIN);
  }

/* Allocate or find a file structure */
  else {
    for (fp = __iop; fp != NULL && TESTFLAG(fp, ~_IORETAIN); fp = fp->__next) {
      if (fileno(fp) == fd) {
/* Multiple fdopen() on a file descriptor is debatable */
/* Error return here breaks too many broken programs!  */
/*    return NULL; */
    __freebuf(fp);
    break;
      }
    }

/* Retain flags on existing descriptors */
    if (fp != NULL)
      flags |= GETFLAG(fp, _IORETAIN);

/* No existing descriptor */
    else {
      if ((fp = (FILE *) malloc(sizeof(*fp))) == NULL)
    return NULL;

      FINSERT(fp);
    }
  }

/* Stream descriptor needs to be initialised */
  fp->__rend   = NULL;
  fp->__rptr   = NULL;
  fp->__wend   = NULL;
  fp->__wptr   = NULL;
  fp->__base   = NULL;
  fp->__flag   = flags;
  fp->__file   = fd;
  fp->__bufsiz = __allocbuf(fp);

  flags       &= _IOREAD | _IOWRITE;
  fp->__filbuf = filtable[flags];
  fp->__flsbuf = flstable[flags];
  fp->__flush  = __btfls;

  return fp;
}

0 0
原创粉丝点击