⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subcalls.c

📁 su 的源代码库
💻 C
字号:
/* Copyright (c) Colorado School of Mines, 2006.*//* All rights reserved.                       *//*********************** self documentation **********************//***************************************************************************SUBCALLS - routines for system functions with error checking efopen		fopen with error checkefreopen	freopen with error checkefdopen		fdopen with error checkepopen		popen with error checkefclose		fclose with error checkepclose		pclose with error checkefflush		fflush with error checkeremove		remove with error checkerename		rename with error checkefseek		fseek with error checkefseeko		fseeko with error checkeftell		ftell with error checkeftello		ftello with error checketmpfile	tmpfile with error checkerewind		rewind (dummy sub)emkstemp		mkstemp with error checkemalloc		malloc with error checkerealloc	realloc with error checkecalloc		calloc with error checkefgetpos	fgetpos with error checkefsetpos	fsetpos with error checkefread		fread with error checkefwrite		fwrite with error check****************************************************************************Function Prototypes:FILE *efopen(const char *file, const char *mode);FILE *efreopen(const char *file, const char *mode, FILE *stream1);FILE *efdopen(int fd, const char *mode);FILE *epopen(char *command, char *type);int efclose(FILE *stream);int epclose(FILE *stream);int efflush(FILE *stream);int eremove(const char *file);int erename(const char *oldfile, const char* newfile);int efseek(FILE *stream, off_t offset, int origin);void erewind(FILE *stream);long eftell(FILE *stream);off_t eftello(FILE *streem);int efseek(FILE *stream, off_t offset, int origin);FILE *etmpfile(void);void *emalloc(size_t size);void *erealloc(void *memptr, size_t size);void *ecalloc(size_t count, size_t size);int efgetpos(FILE *stream, fpos_t *position);int efsetpos(FILE *stream, const fpos_t *position);size_t efread(void *bufptr, size_t size, size_t count, FILE *stream);size_t efwrite(void *bufptr, size_t size, size_t count, FILE *stream);****************************************************************************Returns: All abort with message on errorefopen returns a FILE pointerefreopen returns a FILE pointerefdopen returns a FILE pointerefclose returns 0efflush returns 0eremove returns 0erename returns 0efseek returns 0efseeko returns 0erewind is voideftell returns file position indicatoreftello returns file position indicatoretmpfile returns a FILE pointeremalloc returns void pointer to allocated memoryerealloc returns void pointer to allocated memoryecalloc returns void pointer to allocated memoryefgetpos returns 0efsetpos returns 0efread returns number of items actually readefwrite returns number of items actually written****************************************************************************Notes:Getting less than the number of bytes asked for on a freadis *not* a system subroutine error--usually it just meansend of file has been reached.  However, it *might* be an errorin some application.  Similarly coming up empty is not a systemerror, but might be an application error.  It is left to the userto trap these instances.  In particular, feof can be used to testfor end of file.****************************************************************************References: Rochkind, "Advanced UNIX Programming"Kernighan and Pike, "The UNIX Programming Environment"Kernighan and Ritchie, "The C Programming Language"Mark Williams Company, "ANSI C--A Lexical Guide"****************************************************************************Authors: SEP: Rick Ottolini, Ron, Jon Claerbout, Stew LevinCWP: Shuki Ronen, Jack Cohen***************************************************************************//**************** end self doc ********************************/#include "par.h"#define ERROR	NULLFILE *efopen(const char *file, const char *mode){	FILE *stream;	if (ERROR == (stream = fopen(file, mode)))		err("%s: efopen: fopen failed", __FILE__);		return stream;}FILE *efreopen(const char *file, const char *mode, FILE *stream1){	FILE *stream2;	if (ERROR == (stream2 = freopen(file, mode, stream1)))			err("%s: efreopen: freopen failed", __FILE__);		return stream2;}FILE *efdopen(int fd, const char *mode){	FILE *stream;	if (ERROR == (stream = fdopen(fd, mode)))		      err("%s: efdopen: fdopen failed", __FILE__);		return stream;}FILE *epopen(char *command, char *type){	FILE *stream;	if (ERROR == (stream = popen(command, type)))		      err("%s: epopen: popen failed", __FILE__);		return stream;}int efclose(FILE *stream){	int status;	if (EOF == (status = fclose(stream)))		      err("%s: efclose: fclose failed", __FILE__);	return status;}int epclose(FILE *stream){	int status;	if (EOF == (status = pclose(stream)))		      err("%s: epclose: pclose failed", __FILE__);	return status;}int efflush(FILE *stream){	int status;	if (EOF == (status = fflush(stream)))		      err("%s: efflush: fflush failed", __FILE__);	return status;}int eremove(const char *file){	int status;	if ((status = remove(file)))		syserr("%s: eremove: remove failed", __FILE__);	return status;}int erename(const char *oldfile, const char *newfile){	int status;	if ((status = rename(oldfile, newfile)))		syserr("%s: erename: rename failed", __FILE__);	return status;}int efseek(FILE *stream, off_t offset, int origin){	if (fseek(stream, offset, origin))  /* non-zero => error */		      err("%s: efseek: fseek failed", __FILE__);	return 0;}void erewind(FILE *stream)	/* dummy function */{	rewind(stream);	return;}long eftell(FILE *stream){	long position;	if (-1L == (position = ftell(stream)))		syserr("%s: eftell: ftell failed", __FILE__);	return position;}int efseeko(FILE *stream, off_t offset, int origin){	/* non-zero => error */	if (fseeko(stream, (off_t) offset, (int) origin))		err("%s : efseeko: fseeko failed", __FILE__);	return 0;}off_t eftello(FILE *streem){	off_t eposition;	off_t test=-1;	eposition = ftello(streem);	if (test == eposition) {		fprintf(stderr,"sizeof(off_t)=%lu\n",				(unsigned long) sizeof(eposition));	}		return eposition;}FILE *etmpfile(void){	FILE *stream;	if (ERROR == (stream = tmpfile()))		      err("%s: etmpfile: tmpfile failed", __FILE__);		return stream;}void *emalloc(size_t size){	void *memptr;	if (ERROR == (memptr = malloc(size)))		err("%s : emalloc: malloc failed", __FILE__);		return memptr;}void *erealloc(void *memptr, size_t size){	void *newptr;	if (ERROR == (newptr = realloc(memptr, size)))		err("%s : erealloc: realloc failed", __FILE__);		return newptr;}void *ecalloc(size_t count, size_t size){	void *memptr;	if (ERROR == (memptr = calloc(count, size)))		err("%s : ecalloc: calloc failed", __FILE__);		return memptr;}/* fgetpos and fsetpos may not exist on some systems *//* if you get error messages about these then comment out the next two *//* subroutine definitions *//* beginning of fgetpos  and fsetpos block */#ifndef SUN_A	int efgetpos(FILE *stream, fpos_t *position)	{		int status;		if ((status = fgetpos(stream, position)))			syserr("%s: efgetpos: fgetpos failed", __FILE__);		return status;	}	int efsetpos(FILE *stream, const fpos_t *position)	{		int status;		if ((status = fsetpos(stream, position)))			syserr("%s: efsetpos: fsetpos failed", __FILE__);		return status;	}#endif /* end of SUN_A *//* end of fgetpos, fsetpos block */size_t efread(void *bufptr, size_t size, size_t count, FILE *stream){	size_t nread;	if (!size) err("%s: efread: fread given 0 item size", __FILE__);	nread = fread(bufptr, size, count, stream);	if (nread != count && ferror(stream))		      err("%s: efread: fread only %d items of %d",				__FILE__, nread, count);	return nread;}size_t efwrite(void *bufptr, size_t size, size_t count, FILE *stream){	size_t nwrite;	nwrite = fwrite(bufptr, size, count, stream);	if (nwrite != count)		      err("%s: efwrite: fwrite only %d items of %d",				__FILE__, nwrite, count);	return nwrite;}#ifdef TESTmain(int argc, char **argv){	FILE *fpr, *fpw;	char msg[BUFSIZ];	char erbuf[BUFSIZ], ewbuf[BUFSIZ], rbuf[BUFSIZ], wbuf[BUFSIZ];	char pbuf[1], pfbuf[1];	size_t mbytes, rbytes, wbytes;	size_t ritems, witems;	initargs(argc, argv);	/* Exercise efread and efwrite */  	fpw = efopen("junk.fwr", "w+"); 	strcpy(ewbuf, "   Writing with efwrite\n");	witems = strlen(ewbuf);	efwrite(ewbuf, 1, witems, fpw);	erewind(fpw);	fread(rbuf, 1, witems, fpw);	erewind(fpw);	strcpy(msg, "***efwrite from file to buffer ...");	mbytes = strlen(msg);	fwrite(msg, 1, mbytes, stdout);	fwrite(rbuf, 1, witems, stdout);  	fpr = fopen("junk.frd", "w+"); 	strcpy(wbuf, "   Reading with efread\n");	ritems = strlen(wbuf);	fwrite(wbuf, 1, ritems, fpr);	erewind(fpr);	strcpy(wbuf, "   efread saw zero bytes\n");	witems = strlen(wbuf);	strcpy(msg, "***efread from file to buffer ...");	mbytes = strlen(msg);	fwrite(msg, 1, mbytes, stdout);	if (!efread(erbuf, 1, ritems, fpr)) {		fwrite(wbuf, 1, witems, stdout);	} else {		fwrite(erbuf, 1, ritems, stdout);	}	erewind(fpr); 	strcpy(wbuf, "   Reading byte by byte with efread\n");	ritems = strlen(wbuf);	fwrite(wbuf, 1, ritems, fpr);	erewind(fpr);	strcpy(wbuf, "   exit loop: efread returned zero\n");	witems = strlen(wbuf);	strcpy(msg, "***efread file byte by byte to buffer ...");	mbytes = strlen(msg);	fwrite(msg, 1, mbytes, stdout);	while (efread(erbuf, 1, 1, fpr)) {		fwrite(erbuf, 1, 1, stdout);	}	erewind(fpr);	fwrite(wbuf, 1, witems, stdout); 	strcpy(wbuf, "");	ritems = strlen(wbuf);	fwrite(wbuf, 1, ritems, fpr);	erewind(fpr);	strcpy(wbuf, "   efread saw zero bytes\n");	witems = strlen(wbuf);	strcpy(msg, "***efread from EMPTY file to buffer ...");	mbytes = strlen(msg);	fwrite(msg, 1, mbytes, stdout);	efread(erbuf, 1, ritems, fpr);	erewind(fpr);	fwrite(wbuf, 1, witems, stdout);	efclose(fpw);	efclose(fpr);	eremove("junk.frd");	eremove("junk.fwr");	return EXIT_SUCCESS;}#endif

⌨️ 快捷键说明

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