Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!uflorida!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: sprintf(3s) query Message-ID: <9181@smoke.BRL.MIL> Date: 15 Dec 88 00:02:39 GMT References: <1102@entropy.ms.washington.edu> <8131@ihlpl.ATT.COM> <865@quintus.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 36 In article <865@quintus.UUCP> ok@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 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()).