Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!tektronix!tekcrl!terryl From: terryl@tekcrl.TEK.COM Newsgroups: comp.unix.wizards Subject: Re: #defines with variable # arguments Message-ID: <2633@tekcrl.TEK.COM> Date: 13 May 88 17:25:44 GMT References: <2855@phoenix.Princeton.EDU+ Reply-To: terryl@tekcrl.tek.com Distribution: na Organization: Tektronix, Inc., Beaverton, OR. Lines: 46 In article <2855@phoenix.Princeton.EDU+ lgy@pupthy2.PRINCETON.EDU (Larry Yaffe) writes: + + Are there any versions of cpp which allow one to define macros which +accept variable numbers of arguments? I keep wishing for this every time +I try to move code developed using sysV sprintf to a BSD system, so that +I could do something like: + +#ifdef BSD_TO_5_SPRINTF +char *(*Sprintf)() = sprintf ; +#define sprintf(...) strlen (Sprintf (...)) +#endif + +#ifdef 5_TO_BSD_SPRINTF +int (*Sprintf)() = sprintf ; +#define sprintf(str,...) ((void) Sprintf (str,...), str) +#endif I came across this kludge years ago, hope it helps: #define sprintf(prf) { Sprintf prf } The TRICK is to ADD an extra level of parentheses, using it like thus: sprintf( (buffer,"",) ); This makes cpp THINK there is only one argument to the macro; I know you don't want to muck with the sources and just want to put in the #define and be done with it, but this is the only way I know of. + I know that some systems have `vsprintf' or some such thing which +can be called from a sprintf replacement routine, but since the vprintf +routines are not universally available (sadly), using them seems only +to add to portability problems. + + Does anyone know why the folks at Berkeley chose to have their +sprintf return its first argument, instead of the number of characters +printed? I can't think of any good reason for this choice, since it +throws away valuable information (the # of characters printed) which +is painful to reacquire. How painful is a strlen???? (I.E. I really don't understand your complaint). Since Berkeley code is almost always derived upon ATT (or whatever they're calling themselves this year!!! (-:) code, it sounds like an earlier version of sprintf returned its first argument. However, I also can't really think of any good reason for one return value over the other.......