printf() with vector arguments.

Richard O'Keefe ok at cs.mu.oz.au
Thu Oct 12 14:01:49 AEST 1989


In article <23078 at sequent.UUCP>, paulr at sequent.UUCP (Paul Reger) writes:
> I was wondering if there exists such a beast as:
> void vec_printf(char *fmt,void *args[]);
> (as opposed to what we're all familiar with: void printf(char *fmt, ...);)

Check your manuals to see if you have vprintf().  It's about as close as
you'll get.  If you haven't got vprintf(), you may have _doprnt().

> Such a thing would be useful for a tool that can be used with any
> shell - call it shell_printf.  This would have the synopsis:
> shell_printf fmt [arg1 [arg2 [arg3 ... [argn]]]]

Why call it shell_printf?  I call mine printf.

> Without the vec_printf() routine, such a tool would be hard to do.

Why?  Mine wasn't.  You have to scan the format yourself anyway to ensure
that the number and type of the actual parameters corresponds to the
number and type of arguments expected by the format.  If you don't do
that, (a) you miss the chance to do some pleasant conversions, e.g.
	printf %d 0xABCD
does hex to decimal conversion and
	printf %d "'x'"
prints the ASCII code for x, and (b) your program will dump core from
time to time, which is not a courteous thing for a utility to do.

All you really need is printf.  For example, in handling
	printf "Hello%c %.*s" "','" 5 "World series"
you scan along looking for a %, printing "Hello".  Then you parse the
format code, copying it into a buffer, note that it requires an integer,
fetch an integer from the remaining arguments, and
	printf(buffer, intcvt(*++argv));
Eventually, you have written "Hello, World", having called printf()
twice.

Ok, so this is slower than doing a call to vec_printf(), but the cost
can be ignored in comparison with the cost of starting up the process
in the first place, and it gives you the opportunity to check for mistakes.



More information about the Comp.unix.questions mailing list