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;
}
调用了__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
- libc.a中FILE结构和相应的函数指针的分析
- libc.a中FILE结构的分析
- 结构体中函数指针的用法。
- file结构体中private_data指针的疑惑
- 关于函数指针和指针函数的分析
- 理解数组,结构 ,函数指针,指针函数,数组指针,指针数组,结构指针的定义和实现
- 数值分析 幂法求矩阵A按模最大的特征值和相应的特征向量
- 函数指针的分析
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析 .
- 结构体变量、结构指针变量、结构数组作为函数的参数应用实例分析
- 指针数组 和 数组指针分析 main函数的参数
- getopt.h和相应的函数
- glibc和libc的区别
- C++中引用和指针的分析
- C语言的函数指针和结构体
- Eclipse Neon git push时, 报401 Authorization Required
- 为什么 ObjC 的 protocol 都要遵从 NSObject 协议?
- jTable基础知识:画表格之前要做的事
- xcode8 导致storyboard页面显示不全
- 深入理解 NodeList
- libc.a中FILE结构和相应的函数指针的分析
- 百度地图bug
- TextInputLayout 的使用
- discuz 与YII 的同步登录
- vmwre 桥模式上网, 虚拟机作为一个独立的局域网电脑
- 学习分隔String的应用类 StringTokenizer
- 一些java lambda表达式的笔记
- Leetcode 415. Add Strings 字符串加法 解题报告
- Java ArrayList删除特定元素的方法