Wrapper function to sprintf

Chris Torek chris at MIMSY.UMD.EDU
Fri Mar 4 13:08:20 AEST 1988


I am afraid I have to agree with Greg Hackney's own description of
his method:

	I couldn't figure out how to pass an array of pointers to sprintf()
	and make it work correctly, but here's a dippy way that does work.

The reason you cannot do this `correctly' is simple.  sprintf takes a
variable argument list; the desired `wrapper' function also takes a
variable argument list; but there is no way to say `take my argument
list, whatever it may have been, package it up, and send it on to this
other function'.  In other words, the only correct call to sprintf
is `direct' one.

There *is* a way to implement the desired `dialog' function, however.
A careful scan of the manuals reveals the `vsprintf' function.  (This
is in SysV, at any rate, and an equivalent 4BSD version appears below.
The 4BSD version is not Officially Approved.)

#include <stdio.h>
#include <varargs.h>

/* char *dialog(char *fmt, ...); */
char *dialog();

main()
{

	(void) dialog("%s\t%s\t%s", "one", "two", "three");
}

char *
dialog(va_alist)
	va_dcl
{
	va_list ap;
	char *format;
	char buffer[512];	/* this had best be big enough */

	va_start(ap);
	format = va_arg(ap, char *);
	(void) vsprintf(buffer, format, ap);
	va_end(ap);
	printf("%s\n", buffer);
	return (buffer);
}

/* if you need it: */
#ifdef notdef
int
vsprintf(str, fmt, arg)
	char *str, *fmt;
	va_list arg;
{
	FILE f;
	int ret;

	f._flag = _IOSTRG;	/* leave out _IOWRT to avoid libc bug */
	f._base = f._ptr = str;
	ret = _doprnt(fmt, arg, &f);
	*f._ptr = 0;
	return (ret);
}
#endif



More information about the Comp.sys.pyramid mailing list