 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jonathan Leffler Guest
|
Posted: Mon Apr 16, 2007 8:18 pm Post subject: Would this (mis)use of <varargs.h> ever have worked? |
|
|
I recently found some code that attempted to support both <stdarg.h> and
<varargs.h> headers (a rather futile exercise since there was at least
one prototype-only function in the file), but the <varargs.h> part of
the code tried a stunt I'd not seen before and I wonder whether it would
have worked.
The stunt:
#ifdef USE_STDARG_H
int function(FILE *fp, char *format, ...)
#else
int function(fp, format, va_alist)
FILE *fp;
char *format;
va_dcl
#endif /* USE_STDARG_H */
{
va_list ap;
#ifdef USE_STDARG_H
va_start(ap, format);
#else
va_start(ap);
#endif /* USE_STDARG_H */
...common code...assumes ap is correctly positioned...
va_end(ap);
return(0);
}
The devious bit is the 'pre-positioning' of the va_alist; I've never
seen that described (for example, not in "C: A Reference Manual", 5th Edn).
I'd expect the <varargs.h> code to be (extracted from the <stdarg.h> code):
int function(va_alist)
va_dcl
{
FILE *fp;
char *format;
va_list ap;
va_start(ap);
va_arg(ap, FILE *);
va_arg(ap, char *);
...common code...ap is correctly positioned!...
va_end(ap);
return(0);
}
Does anyone have any idea whether it would have worked? Reliably?
This is for my curiosity rather than anything else; the <varargs.h> code
will be consigned to history, only visible in the CM system. The code
in question dates back to 1994 (when there was a case for supporting
<varargs.h> as not all systems we worked with had ISO C compilers back
then), but no-one else has seen fit to scrutinize it and modify it,
probably because the <varargs.h> code is never used these days. It so
happens that (a) 1994 is when the code base started using prototypes
generally, and (b) it was when we changed CM systems (from SCCS to Atria
ClearCase), so I don't have any pre-history of the file to see whether
it was used in the 1980s. These facts mean I can't be sure whether the
<varargs.h> code was ever actually used.
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler (AT) earthlink (DOT) net, jleffler (AT) us (DOT) ibm.com
Guardian of DBD::Informix v2007.0226 -- http://dbi.perl.org/
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Apr 19, 2007 7:14 pm Post subject: Re: Would this (mis)use of <varargs.h> ever have worked? |
|
|
Jonathan Leffler wrote:
| Quote: | int function(fp, format, va_alist)
FILE *fp;
char *format;
va_dcl
...
The devious bit is the 'pre-positioning' of the va_alist; I've never
seen that described ...
|
I've seen it before, but it isn't in accordance with the specs
for the varargs facility, and might not work on some platforms.
| Quote: | I'd expect the <varargs.h> code to be (extracted from the <stdarg.h> code):
int function(va_alist)
va_dcl
{
FILE *fp;
char *format;
va_list ap;
va_start(ap);
va_arg(ap, FILE *);
va_arg(ap, char *);
...
|
Well, not quite: thost last two need to be assigned to their variables.
| Quote: | ... These facts mean I can't be sure whether the
varargs.h> code was ever actually used.
|
We had to do something similar in the MUVES project, which was
developed right around the time of the first C standard and thus
had to cope with both methods. MUVES actually defined macros so
that the code was more like:
#include "Va.h" /* defines macros depending on the environment */
int function(VaT(FILE *fp) VaT(char *format) VaAlist )
VaDcl
{
VaD(FILE *fp)
VaD(char *format)
VaList(ap)
VaStart(ap, format)
VaI(ap, FILE *, fp)
VaI(ap, char *, format)
/* ... VaArg(ap, type) used instead of va_arg(ap, type);
ap usable with vfprintf etc. ... */
VaEnd(ap)
return 0;
}
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|