main() and exit() (was: Strange lint mumblings)

Steve Summit scs at adam.pika.mit.edu
Fri Jan 6 15:26:54 AEST 1989


In article <7082 at batcomputer.tn.cornell.edu> braner at tcgould.tn.cornell.edu (Moshe Braner) writes:
>On systems I have worked on, calling exit() links in most of the
>STDIO library modules, resulting in an executable program that is
>much bigger than it needs to be...

In article <9253 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>This didn't happen on UNIX systems before "ranlib" invaded, because
>the C library provided two modules containing the same entry point,
>one linked in when any part of STDIO was linked and the other module
>otherwise.

I frequently use another technique to keep executables small,
which requires neither manipulation of library ordering nor
atexit/onexit, and works fine in the presence of ranlib.  To
solve the _cleanup problem, I'd write exit as:

	int (*_cleanupptr)() = NULL;

	exit(status)
	int status;
	{
	if(_cleanupptr != NULL)
		(*_cleanupptr)();

	_exit(status);
	}

and then put

	extern int (*_cleanupptr)();
	extern int _cleanup();

	...

	_cleanupptr = _cleanup;

in fopen and/or _flsbuf.  _cleanup is therefore not one of exit's
undefined externals, and won't get pulled in unless stdio is
actually used.

I'm not sure if I saw this technique somewhere or if I invented
it.  If the latter, remember, you saw it here first! :-)

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list