sprintf(3s) query

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Dec 15 10:02:39 AEST 1988


In article <865 at quintus.UUCP> ok at quintus.UUCP (Richard A. O'Keefe) writes:
>yes, let's.  SunOS was originally based on 4.2BSD, and 4.2BSD sprintf()
>returns the buffer address.  Why is that?  Because Berkeley ***DIDN'T***
>change it!  AT&T changed the "standard" library!

You've oversimplified to the point of unacceptable distortion.
Originally, sprintf()'s return value was not specified (see e.g.
UPM 7th Ed.).  In one of several cases of parallel uncoordinated
invention, Berkeley decided it should be the buffer address
(perhaps because that happened to accidentally be in the return
register already in the VAX assembly-language implementation),
while AT&T decided that all the *printf() functions should return
the (much more useful) count of characters transferred.

To make matters even worse, somewhere along the way (perhaps as a
result of the /usr/group 1984 Standard) Berkeley realized that
there was this discrepancy between the two major UNIX variants.
So they altered the declaration in their <stdio.h> to:

#ifdef vax
char	*sprintf();		/* too painful to do right */
#endif

Note that Sun MUST have changed this in order for it to apply to
their (definitely non-VAX) machines.  In fact a lot of vendors
supplying 4BSD-based systems (maybe even all of them) did the
same thing, since there were applications developed on VAX 4BSD
that relied on it.  (Rogue, for example.)

I hope by now all the BSD code that depends on the value of
sprintf() being anything in particular has been changed to use
some other method.  Usually, something like
	buf[0] = '\0';
	(void)sprintf(buf, fmt, args);
	if (buf[0] == '\0')
		/* error */
is sufficient (it works with both definitions of sprintf()).



More information about the Comp.lang.c mailing list