fread.c

来自「在x86平台上运行不可信任代码的sandbox。」· C语言 代码 · 共 55 行

C
55
字号
#include <string.h>#include <unistd.h>#include "ioprivate.h"size_t fread(void *ptr, size_t eltsize, size_t nelts, FILE *f){	size_t totsize = eltsize * nelts;	size_t actsize = 0;	while (totsize > 0) {		size_t buffed = f->ilim - f->ipos;		if (totsize <= buffed) {			// The rest of the data we need is in the buffer.			memcpy(ptr, &f->ibuf[f->ipos], totsize);			f->ipos += totsize;			actsize += totsize;			goto done;		}		// Copy any remaining data we may have in the buffer.		memcpy(ptr, &f->ibuf[f->ipos], buffed);		f->ipos = f->ilim = 0;		ptr += buffed;		actsize += buffed;		totsize -= buffed;		// Don't use the buffer for large reads.		if (totsize >= BUFSIZ) {			ssize_t rc = read(f->fd, ptr, totsize);			if (rc < 0) {				f->errflag = 1;				goto done;			}			if (rc == 0) {				f->eofflag = 1;				goto done;			}			ptr += rc;			actsize += rc;			totsize -= rc;		} else {			if (__getinput(f) < 0)				goto done;		}	}done:	return actsize / eltsize;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?