#defines with variable # arguments

Richard A. O'Keefe ok at quintus.UUCP
Sun May 15 19:22:31 AEST 1988


In article <979 at cresswell.quintus.UUCP>, ok at quintus.UUCP (Richard A. O'Keefe) writes:
> What I suggest is a one-time conversion:
> 	sprintf(S,F)		-> sprintf0(S,F)
> 	sprintf(S,F,A)		-> sprintf1(S,F,A)
> 	...
> 	sprintf(S,F,A,B,C,D,E,F)-> sprintf6(S,F,A,B,C,D,E,F)
> however many you need.  This is easy enough to do with m4.

I should substantiate that.

cat >sprintf.m4 <<'EOF'
define(sprintf,``sprintf'eval($#-2)($*)')dnl
EOF

This uses $# and $* which are in the System V and PD versions, but not in
the BSD version, so you may have to do your conversion on System V.
All it takes is
	m4 sprintf.m4 original.c >converted.c

If you just want to make the BSD sprintf() look as though it returned an
integer, do

cat >sprintflen.m4 <<'EOF'
define(sprintf,`strlen(`sprintf'($*))')dnl
EOF



More information about the Comp.unix.wizards mailing list