_cleanup() before fork()? (was Re: How to terminate a child ...)

Chris Torek chris at mimsy.umd.edu
Mon Jun 11 01:08:09 AEST 1990


In article <775 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:
>It would be quite helpful if stdio had officially defined some
>"fflush_all()" operation ...

As of ANSI C, it does: fflush((FILE *)NULL) does the trick.

>- which is necessary anyway, because its implicitly called by exit().
>For as much as I know the usual name of this routine is "_cleanup()",
>but using it - of course - is not recommendable for portable programs.

Indeed.  In particular, in many implementations _cleanup() *closes*
every stdio stream.  The result is that stdin, stdout, and stderr are
gone.

fflush((FILE *)NULL) is the only semi-portable solution.  If you need
something like this, you are probably best off including a separate C
file with a function `flushall' that reads:

	#include <stdio.h>

	flushall()
	{
	#ifdef FFLUSH_NULL	/* use ANSI standard method */
		(void) fflush((FILE *)NULL);
	#else
	#ifdef _NFILE	/* probably a SysV or V7 or 4.1BSD system */
		register FILE *fp;
		for (fp = &_iob[0]; fp < &_iob[_NFILE]; fp++)
			if (fp->_flag)
				(void) fflush(fp);
	#else		/* probably a 4.2BSD system or derivative */
		extern int fflush();	/* might not be in <stdio.h> */
		extern int _fwalk();	/* undocumented but present */
		(void) _fwalk(fflush);
	#endif
	#endif
	}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.questions mailing list