Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!uwm.edu!cs.utexas.edu!uunet!munnari.oz.au!cs.mu.oz.au!ok From: ok@cs.mu.oz.au (Richard O'Keefe) Newsgroups: comp.unix.questions Subject: Re: printf() with vector arguments. Keywords: printf() Message-ID: <2400@munnari.oz.au> Date: 12 Oct 89 04:01:49 GMT References: <23078@sequent.UUCP> Sender: news@cs.mu.oz.au Distribution: comp Lines: 38 In article <23078@sequent.UUCP>, paulr@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.