Path: utzoo!attcan!uunet!husc6!purdue!umd5!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.unix.wizards
Subject: sprintf return value
Message-ID: <11451@mimsy.UUCP>
Date: 12 May 88 04:26:48 GMT
References: <2855@phoenix.Princeton.EDU>
Distribution: na
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 64

In article <2855@phoenix.Princeton.EDU> lgy@pupthy2.PRINCETON.EDU
(Larry Yaffe) asks in an aside:
>Does anyone know why the folks at Berkeley chose to have their
>sprintf return its first argument, instead of the number of characters
>printed?

Because it had been that way since 32V.  Indeed, the 4.3BSD 
noted

	char *sprintf();	/* too painful to do right */

where `right' presumably meant

	int sprintf();

(or, in New C, `int sprintf(char *str, const char *fmt, ...);'.)

This has been fixed since then; we (read: Keith Bostic) went through
all the utilities and got rid of assumptions about sprintf()'s return
value.  (Actually, I am not sure anyone has gone through /usr/src/new
yet.)  If you want to fix this in older BSD distributions, here is
a functioning, though machine dependent, sprintf and vsprintf.  (Compile
everything but isprintf() below with `-Dsprintf=isprintf'.)

	#include 

	int
	isprintf(s, fmt, args)
		char *s, *fmt;
	{
		FILE f;
		int ret;

		f._flag = _IOSTRG;	/* _IOWRT tickles bug in flsbuf */
		f._ptr = s;
		f._cnt = 32767;		/* `infinity' */
		ret = _doprnt(fmt, &args, &f);
		*f._ptr = 0;
		return (ret);
	}

and

	#include 
	#include 

	int
	vsprintf(s, fmt, ap)
		char *s, *fmt;
		va_list ap;
	{
		FILE f;
		int ret;

		f._flag = _IOSTRG;	/* _IOWRT tickles bug in flsbuf */
		f._ptr = s;
		f._cnt = 32767;		/* `infinity' */
		ret = _doprnt(fmt, ap, &f);
		*f._ptr = 0;
		return (ret);
	}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris