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

📄 subcalls.c

📁 seismic software,very useful
💻 C
字号:
/* Copyright (c) Colorado School of Mines, 1990./* All rights reserved.                       *//*---------------------------------------------------------------------- * Copyright (c) Colorado School of Mines, 1989. * All rights reserved. * * This code is part of SU.  SU stands for Seismic Unix, a processing line * developed at the Colorado School of Mines, partially based on Stanford * Exploration Project (SEP) software.  Inquiries should be addressed to: * *  Jack K. Cohen, Center for Wave Phenomena, Colorado School of Mines, *  Golden, CO 80401  (dix@mines.colorado.edu) * *  J.C. Dulac,        add etempfile. *  Z. Li,       , replaced tempnam with tmpnam for Convex compatibility *                 added etempnam    *---------------------------------------------------------------------- */#include "par.h"#define ERROR	NULL/* subcalls - routines for system functions with error checking * * efopen   - fopen with error check * efreopen - freopen with error check * efdopen  - fdopen with error check * epopen   - popen with error check * efclose  - fclose with error check * epclose  - pclose with error check * efflush  - fflush with error check * eremove  - remove with error check * erename  - rename with error check * efseek   - fseek with error check * erewind  - rewind (dummy sub) * eftell   - ftell with error check * etmpfile - tmpfile with error check * etempfile -tmpfile with a specified directory * etmpnam  - tmpnam with error check * emalloc  - malloc with error check * erealloc - realloc with error check * ecalloc  - calloc with error check * efgetpos - fgetpos with error check * efsetpos - fsetpos with error check * efread   - fread with error check * efwrite  - fwrite with error check * pfread   - fread on pipe with error check * * Returns: All abort with message on error *	efopen returns a FILE pointer *	efreopen returns a FILE pointer *	efdopen returns a FILE pointer *	efclose returns 0 *	efflush returns 0 *	eremove returns 0 *	erename returns 0 *	efseek returns 0 *	erewind is void *	eftell returns file position indicator *	etmpfile returns a FILE pointer *	etempfile returns a FILE pointer *	etmpnam returns a char pointer *	emalloc returns void pointer to allocated memory *	erealloc returns void pointer to allocated memory *	ecalloc returns void pointer to allocated memory *	efgetpos returns 0 *	efsetpos returns 0 *	efread returns number of items actually read *	efwrite returns number of items actually written *	pfread returns number of items actually read from pipe * * Notes: *	Getting less than the number of bytes asked for on a fread *	is *not* a system subroutine error--usually it just means *	end of file has been reached.  However, it *might* be an error *	in some application.  Similarly coming up empty is not a system *	error, but might be an application error.  It is left to the user *	to trap these instances.  In particular, feof can be used to test *	for end of file. * * Synopsis: *	FILE *efopen(file, mode) *	const char *file - file to open *	const char *mode - access mode: "r", "w", ... * *	FILE *efreopen(file, mode, stream) *	const char *file - file to open *	const char *mode - access mode: "r", "w", ... *	FILE *stream     - stream to reopen * *	FILE *efdopen(fd, mode) *	int fd           - file descriptor of open file *	char *mode - access mode: "r", "w", ... * *	FILE *popen(command, type) *	char *command - command to pipe from or to *	char *type    - "r" or "w" * *	int efclose(stream) *	FILE *stream - stream to close * *	int epclose(stream) *	FILE *stream - stream to close * *	int efflush(stream) *	FILE *stream - stream to flush * *	int eremove(file) *	const char *file - file to remove * *	int erename(file) *	const char *file - file to rename * *	int efseek(stream, offset, origin) *	FILE *stream - stream to examine *	long offset  - amount to move file pointer from origin *	int origin   - SEEK_SET, SEEK_CUR or SEEK_END * *	void erewind(stream) *	FILE *stream - stream to examine * *	long eftell(stream) *	FILE *stream - stream to examine * *	FILE *etmpfile(void) * *      FILE * etmpfile(char *dir) *      char *dir   - directory where to put temporary file  *                    if NULL then environment variable SU_SCRATCHDIR is taken *                    if again NULL then SCRATCHDIR * *	char *etmpnam(namebuffer) *	char *namebuffer - buffer to hold name of file * *	void *emalloc(size) *	size_t size - size in bytes requested *	 *	void *erealloc(memptr, size) *	void *memptr - pointer previously allocated *	size_t size  - size in bytes requested *	 *	void *ecalloc(count, size) *	size_t count - number of units requested *	size_t size  - size of unit in bytes *	 *	int eread(fd, buf, nbytes) *	int fd      - file descriptor of file to be read * *	int efgetpos(stream, position) *	FILE *stream     - stream to examine *	fpos_t *position - file position indicator * *	int efsetpos(stream, position) *	FILE *stream           - stream to examine *	const fpos_t *position - file position indicator * *	size_t efread(bufptr, size, count, stream) *	void *bufptr - pointer to buffer *	int size     - sizeof(*bufptr) *	int count    - number of items to be read *	FILE *stream - input stream * *	size_t efwrite(bufptr, size, count, stream) *	void *bufptr - pointer to buffer *	size_t size  - sizeof(*bufptr) *	size_t count - number of items to be written *	FILE *stream - output stream * *	size_t pfread(bufptr, size, count, stream) *	void *bufptr - pointer to buffer *	size_t size  - sizeof(item pointed to) *	size_t count - number of items to be read *	FILE *stream - input stream * * Credits:  *	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" *	SEP: Rick, Ron, Jon, Stew *	CWP: Shuki, Jack * */FILE *efopen(const char *file, const char *mode){	FILE *stream;	if (ERROR == (stream = fopen(file, mode)))	      err("%s: efopen: fopen failed for file %s", __FILE__,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 for file %s", __FILE__,file);		return stream2;}/* FILE *efdopen(int fd, char *mode) */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, long 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;}FILE *etmpfile(void){	FILE *stream;	/* tmpfile almost surely gives its own error message, but	/* since this is not guaranteed by ANSI C we give one too */	if (ERROR == (stream = tmpfile()))		      err("%s: etmpfile: tmpfile failed", __FILE__);		return stream;}#ifndef SCRATCHDIR#define SCRATCHDIR   "/usr/tmp"#endif#ifndef E_SCRATCHDIR#define E_SCRATCHDIR "SU_SCRATCHDIR"#endifFILE *etempfile(char *scrdir){    char *tempname;    char *dfname, *fname;    FILE *fp;    if( scrdir == NULL ) {       if( !(scrdir=getenv(E_SCRATCHDIR)) ) {           scrdir = SCRATCHDIR ;       }    }        /*	    tempname=tempnam(scrdir, NULL);    */    tempname = (char*)malloc(240*sizeof(char));    dfname = (char*)malloc(240*sizeof(char));    fname = (char*)malloc(240*sizeof(char));    strcpy(tempname,scrdir);    dfname = tmpnam(NULL);    fname = (char*)rindex(dfname,'/');         strcat(tempname,fname);    if (tempname == NULL) {	fp = NULL;    } else {	fp = fopen (tempname, "w+");	unlink (tempname);    }    if (fp == NULL) 	perror ("etempfile(...) failed");     free(tempname);    free(dfname);    free(fname);    return fp;}char *etempnam(char *scrdir, char *pfx){    char *tempname, *filename;    char *dfname, *fname;    if( scrdir == NULL ) {       if( !(scrdir=getenv(E_SCRATCHDIR)) ) {           scrdir = SCRATCHDIR ;       }    }        tempname = (char*)malloc(240*sizeof(char));    dfname = (char*)malloc(240*sizeof(char));    fname = (char*)malloc(240*sizeof(char));    strcpy(tempname,scrdir);    dfname = tmpnam(pfx);    fname = (char*)rindex(dfname,'/');         strcat(tempname,fname);    if (tempname == NULL) 	perror ("etempnam(...) failed");     free(dfname);    free(fname);    filename = tempname;	    return filename;}char *etmpnam(char *namebuffer){	char *filename;	if (ERROR == (filename = tmpnam(namebuffer)))		      err("%s: etmpnam: tmpnam failed", __FILE__);		return filename;}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;}	int efgetpos(FILE *stream, fpos_t *position)	{		int status;		/* if (status = fgetpos(stream, position)) */		if (*position = ftell(stream)) {			status = 1;			syserr("%s: efgetpos: fgetpos failed", __FILE__);		}		return status;	}	int efsetpos(FILE *stream, const fpos_t *position)	{		int status;		/* if (status = fsetpos(stream, position)) */		if (fseek(stream, *position, 0)) {			status = 1;			syserr("%s: efsetpos: fsetpos failed", __FILE__);		}		return status;	}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__);	for(nread=0; nread<count;) {		int i=fread(((char*)bufptr)+nread*size,size,count-nread,stream);		if(i>0) nread += i ;		else break;	}	/* 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); */	for(nwrite=0; nwrite<count;) {	     int i=fwrite(((char*)bufptr)+nwrite*size,size,count-nwrite,stream);		if(i>0) nwrite += i ;		else break;	}	if (nwrite != count)		      err("%s: efwrite: fwrite only %d items of %d",				__FILE__, nwrite, count);	return nwrite;}size_t pfread(void *bufptr, size_t size, size_t count, FILE *stream){	size_t nread, ntotal = 0;	if (!size) err("%s: pfread: fread given 0 item size", __FILE__);	while (count) {		nread = fread(bufptr, size, count, stream);		if (ferror(stream)) {			      err("%s: efread: fread failed", __FILE__);		} else if (!nread || feof(stream)) {			return 0;		} else {			ntotal += nread;			count  -= nread;			bufptr = (void*)((char*)bufptr+nread*size);			/* bufptr += nread*size; */		}	}	return ntotal;}#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;	size_t (*readptr) ();	/* pointer to efread() or pfread()	*/	size_t efread();	/* must be declared to use ptr		*/	size_t pfread();	/* must be declared to use ptr		*/	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");	/* Exercise pfread and efread */	/* Set appropriate read function for input filetype */	switch(filestat(STDIN)) {	case TTY:		err("input can't be tty");	break;	case DISK:	case TAPE:		readptr = efread;		strcpy(ewbuf, "***Disk stdin: use efread ...   ");		witems = strlen(ewbuf);		efwrite(ewbuf, 1, witems, stdout);	break;	case PIPE:		readptr = pfread;		strcpy(ewbuf, "***Pipe stdin: use pfread ...   ");		witems = strlen(ewbuf);		efwrite(ewbuf, 1, witems, stdout);	break;	default:		err("undefined input filetype %s", printstat(STDIN));	break;	}	while ((*readptr)(pfbuf, 1, 1, stdin)) {		efwrite(pfbuf, 1, 1, stdout);	}	return EXIT_SUCCESS;}#endif

⌨️ 快捷键说明

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