From: SMTP%"titandmp@ftp-gw-1.pa.dec.com" 5-SEP-1994 14:43:51.67 To: USRC CC: Subj: v44i052: va.h - Dealing with variable argument definitions, Part01/01 Resent-Date: 5 Sep 1994 12:06:34 -0500 Path: decwrl!spool.mu.edu!howland.reston.ans.net!math.ohio-state.edu!jussieu.fr!univ-lyon1.fr!swidir.switch.ch!newsfeed.ACO.net!Austria.EU.net!EU.net!uunet!sparky!not-for-mail From: gwyn@arl.mil (Doug Gwyn (ACISD/MCSB)) Newsgroups: comp.unix.solaris,comp.sources.misc Subject: v44i052: va.h - Dealing with variable argument definitions, Part01/01 Followup-To: comp.sources.d Date: 5 Sep 1994 12:06:34 -0500 Organization: U.S. Army Research Laboratory APG, MD. Lines: 170 Sender: kent@sparky.sterling.com Approved: kent@sparky.sterling.com Distribution: inet Message-Id: <34fj6q$4br@sparky.sterling.com> Nntp-Posting-Host: sparky.sterling.com X-Md4-Signature: 63e0cf822d2d14d7a2f7503007bee4fa Xref: decwrl comp.unix.solaris:25128 comp.sources.misc:1577 To: unix-sources@pa.dec.com Resent-Message-Id: <"KvtuF2.0.p61.f9rQk"@ftp-gw-1.pa.dec.com> Resent-From: unix-sources@pa.dec.com X-Mailing-List: archive/latest/67 X-Loop: unix-sources@pa.dec.com Precedence: list Resent-Sender: unix-sources-request@pa.dec.com Submitted-by: gwyn@arl.mil (Doug Gwyn (ACISD/MCSB)) Posting-number: Volume 44, Issue 52 Archive-name: va.h/part01 Environment: K&R C, GCC In article <3331u5$dcp@news.mic.ucla.edu> bryan@trifid.astro.ucla.edu (Bryan LittleField ) writes: >Anyway, I'm using the variable argument stuff and GCC does not like it.. >I include the "" include file etc.. >It turns out the problem is the include has the include >of the include file which redefines the vararg stuff so that >it won't work with my program for now.. >What should I do to make GCC compile with the "" include file ?? You shouldn't insist on using , since (as you discovered) its macros conflict with the newer Standard C . The following programming aid might be helpful to you; I'm putting it into the public domain in order to help people who need to write code that will work in *both* the Standard C and traditional UNIX K&R C environments. --------- #! /bin/sh # This is a shell archive. Remove anything before this line, then feed it # into a shell via "sh file" or similar. To overwrite existing files, # type "sh file -c". # Contents: Va.h # Wrapped by kent@sparky on Mon Sep 5 11:59:58 1994 PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH echo If this archive is complete, you will see the following message: echo ' "shar: End of archive 1 (of 1)."' if test -f 'Va.h' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'Va.h'\" else echo shar: Extracting \"'Va.h'\" \(3296 characters\) sed "s/^X//" >'Va.h' <<'END_OF_FILE' X/** X -- MUVES "Va" (variable argument) package definitions X X**/ X/* X created: 94/08/16 D A Gwyn X last edit: 94/08/19 D A Gwyn X SCCS ID: @(#)Va.h 1.1 X */ X/** X X The Va package provides portable support for functions taking X a variable number of arguments. defines several macros X that work together to hide the differences between the old X UNIX facility and the new Standard C . X X Rather than describing each Va package macro separately, it is X best to give an example of the proper usage of the whole set of X macros. It should be easy to adapt this generic example to any X specific requirement. X X The example is for a function named Advise that has two X required arguments, the first being a printf-like format string X and the second a flag that indicates (when true) that an extra X "verbosity level" is provided as the third argument. Remaining X arguments are those, if any, associated with the format string. X The Advise function prints the formatted result on the standard X error output if and only if the verbosity level is given and is X greater than 0. It returns true unless it had trouble printing X on the standard error output stream. X X Any code that wants to invoke the Advise function must include X a proper declaration for it: X X #include X extern bool Advise( VaT(const char *) VaT(bool) VaDots ); X X The implementation of the Advise function might be: X X #include X #include X #include X X // VARARGS // not VARARGS2 X bool X Advise( VaT( const char *format ) VaT( bool verbose ) VaAList ) X VaDcl X { X VaD( char *format ) // no "const" here X VaD( bool verbose ) X VaList( ap ) X register int verbosity; X register bool status; X X VaStart( ap, verbose ) X VaI( ap, char *, format ) // no "const" here X VaI( ap, bool, verbose ) X X if ( verbose ) X verbosity = VaArg( ap, int ); X else X verbosity = 0; X X if ( verbosity > 0 ) X status = vfprintf( stderr, format, ap ) > 0; X else X status = true; X X VaEnd( ap ) X return status; X } X X Note that several of these macros are reminiscent of the va_* X macros in or , but there are significant X differences. Proper usage of the "function-like" macros, in X particular, does not require semicolons; this is intentional, X in order to avoid warnings about "null statements" from certain X compilers and "lint". It is suggested that the best way to use X this package is to copy the above example and then make changes X to the copy that are necessary for the specific application. X**/ X X#if __STDC__ X X#include X X#define VaT( t ) t, X#define VaDots ... X#define VaAList ... X#define VaD( d ) /* nothing */ X#define VaDcl /* nothing */ X#define VaList( ap ) va_list ap; X#define VaStart( ap, A0 ) va_start( ap,A0 ); X#define VaI( ap, T, Ai ) /* nothing */ X#define VaArg( ap, T ) va_arg( ap, T ) X#define VaEnd( ap ) va_end( ap ); X X#else /* "classic" version of UNIX assumed */ X X#include X X#define VaT( t ) /* nothing */ X#define VaDots /* nothing */ X#define VaAList va_alist X#define VaD( d ) d; X#define VaDcl va_dcl X#define VaList( ap ) va_list ap; X#define VaStart( ap, A0 ) va_start( ap ); X#define VaI( ap, T, Ai ) Ai = va_arg( ap, T ); X#define VaArg( ap, T ) va_arg( ap, T ) X#define VaEnd( ap ) va_end( ap ); X X#endif X END_OF_FILE if test 3296 -ne `wc -c <'Va.h'`; then echo shar: \"'Va.h'\" unpacked with wrong size! fi # end of 'Va.h' fi echo shar: End of archive 1 \(of 1\). cp /dev/null ark1isdone MISSING="" for I in 1 ; do if test ! -f ark${I}isdone ; then MISSING="${MISSING} ${I}" fi done if test "${MISSING}" = "" ; then echo You have the archive. rm -f ark[1-9]isdone else echo You still must unpack the following archives: echo " " ${MISSING} fi exit 0 exit 0 # Just in case...