[comp.unix.questions] stdio.h & sprintf question

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/09/90)

In article <HARKCOM.90Jun8175943@potato.pa.Yokogawa.CO.JP> harkcom@potato.pa.Yokogawa.CO.JP (Alton Harkcom) writes:
>char    *sprintf();             /* too painful to do right */

This is an artifact from an early implementation of sprintf() that
happened to leave the buffer address in the function return register,
so some applications eventually started relying on it.  On non-BSD
systems sprintf() eventually was changed from an accidental void-
valued function that happened to act like a char*-valued one to a
deliberately int-valued one, along with the rest of the *printf()
family.  While Berkeley didn't follow this trend for quite a while,
because as the comment indicates it would require tracking down all
uses in applications and fixing them, I think 4.4BSD plans to track
the C standard, which specifies int-valued.

harkcom@potato.pa.Yokogawa.CO.JP (Alton Harkcom) (06/09/90)

   I got a bit of a laugh when I looked at the stdio.h file on
my work station. (BSD43) There was an ifdef for vax with this assignment...

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

   Anybody know what this means?
   Can you explain it simply?
   Can you explain why it isn't defined for all machines?

Thanks in advance.

--
--harkcom@potato.pa.yokogawa.co.jp

chris@mimsy.umd.edu (Chris Torek) (06/11/90)

In article <13081@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) writes:
>>char    *sprintf();             /* too painful to do right */
>... is an artifact from an early implementation of sprintf() ...
>While Berkeley didn't [fix sprintf] for quite a while, because as the
>comment indicates it would require tracking down all uses in
>applications and fixing them, I think 4.4BSD plans to track the C
>standard, which specifies int-valued.

The type of sprintf was changed either for or immediately after the
`tahoe' release of 4.3BSD.  <stdio.h> now contains an int declaration,
without any mysterious `#ifdef vax'.  The number of programs that
subsequently fail to compile and/or run is astonishing (this includes,
e.g., X11R3).  In most cases it is simply a matter of removing the
declaration `char *sprintf();' from some source modules; in some
cases it requires expanding things, e.g., from
	return (sprintf(buf, "%s/%s", dir, file));
to
	(void) sprintf(buf, "%s/%s", dir, file);
	return (buf);
and it is the latter that is painful.  (No, there is no `osprintf'
function that acts like sprintf but returns its first argument, though
if someone argued sufficiently hard for it we might be convinced to
put one in -lcompat.)

While 4.4BSD will not be completely ANSI compliant (even when using
gcc and -D_ANSI_SOURCE), we have at least started the task.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris