[net.bugs.4bsd] C startup in /lib/crt0.o

ark@alice.UucP (Andrew Koenig) (04/28/86)

> I see that the C startup routine _start in /lib/crt0.o under 4.2BSD
> is a bit over-protective, in that it calls routine _exit (rather than
> the system-call __exit) when it terminates.

If it didn't do that, the last buffer would go unflushed
on all output files.

guy@sun.UUCP (04/28/86)

> I see that the C startup routine _start in /lib/crt0.o under 4.2BSD
> is a bit over-protective, in that it calls routine _exit (rather than
> the system-call __exit) when it terminates.
> (At least, this is the case >here< - I don't know if its very standard!)

Yes, it is extremely standard.  Just about every UNIX implementation of C
does it (S3, S5, 4.xBSD, probably V7).

> Does anyone know why? (or care, for that matter).

In most UNIX implementations of C, if "main" returns with a given value, the
program acts as if "exit" had been called as if "exit" had been called with
that value.  (Yes, I already know Sun UNIX doesn't do this.)  This is
implicitly stated in the S5 documentation, and very explicitly stated in the
draft ANSI standard for C.  It has nothing whatsoever to do with
"over-protectiveness"; it's a matter of the semantics of the routine "main".

> I suppose it does no harm really (except load in 4k of stdio-library
> garbage, which I could really cope without!).

It only does so in systems like 4.xBSD, which have removed the trick in the
C library which causes the standard I/O library to be brought in only if it
was actually used.  This trick dates back to V7, and is still in S3 and S5.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.arpa

rd@lri.UUCP (Roland Dirlewanger) (04/29/86)

In article <100@cstvax.UUCP> simon@cstvax.UUCP (Simon Brown) writes:
>
>I see that the C startup routine _start in /lib/crt0.o under 4.2BSD
>is a bit over-protective, in that it calls routine _exit (rather than
>the system-call __exit) when it terminates.
>(At least, this is the case >here< - I don't know if its very standard!)
>
>Does anyone know why? (or care, for that matter).
>I suppose it does no harm really (except load in 4k of stdio-library
>garbage, which I could really cope without!).

It is standard, and in almost every Unix system. This needed to flush all
the buffering done by the stdio-library when your process exits.

If you don't use the stdio package, adding these few lines to your programs
will avoid these 4K of garbage to be loaded:

exit (n) {
	return _exit (n);
}

Question:
	How standard is the stdio package ? I had some troubles with it,
	especially on a SUN 2 (may be this bug has been corrected in the new
	3.0 version of SUN's system). Using stdio's printf caused the next
	malloc to return a non-aligned pointer. Using it as pointer to
	a short integer obviously core dumped.

Personnaly, I don't use the stdio package.
I have rewritten from scratch my own printf, sprintf and fprintf
(except for floatting point numbers) and I use only reads and
writes for accessing files (sometimes I have to do buffered
I/O myself, if I don't want to access files one character at a time).

				Roland Dirlewanger