C Macro question
fjr at marsdome.myip.org
fjr at marsdome.myip.org
Sun Jun 16 18:33:00 EDT 2002
Jerry,
thanks for the code snipet. That provided exactly what I needed
:)
-fjr
----Original Message-----
>From: Jerry Feldman <gaf at blu.org>
>To: discuss at blu.org
>Subj: Re: C Macro question
>Sent: Sunday, June 16, 2002 6:11 PM
>
>I write C code according to the C standard. vfprintf(3) is a
standard
>function available on all standard C implementations. C
preprocessor macros
>according to the standard do not support variable arguments. There
are ways
>to get around this, but I dod not want to be overly complicated.
>"Derek D. Martin" wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> At some point hitherto, Jerry Feldman hath spake thusly:
>> > In short, macros do not support variable arguments, but....
>>
>> Well, at least with Gnu tools, yes they do. See my previous
post. I
>> haven't tried this with any proprietary compiler, as I currently
don't
>> have access to any.
>>
>> > You are better off using a C function for that (assume you
include
>> > stdarg.h).
>> > int printerr(const star *fmt, ...)
>> > {
>> > va_list ap;
>> > int rv;
>> > time_t now = time(NULL);
>> > char msg[SOMESIZE];
>> > va_start(ap, fmt);
>> > sprintf(msg, "%s - ERROR: %s", ctime(&now), fmt);
>> > rv = vfprintf(logfile, msg, ap);
>> > return rv;
>> > }
>>
>> This reminds me; your implementation (if you're not using glibc)
may
>> not have vsnprintf(). If you're writing to a file, you can use
a
>> method something like gaf's above. Most implementations have
>> vfprintf() which make that possible.
>>
>> If you're using some other API, such as to log a message to
syslog,
>> that won't work (because you need to have the message you're
logging
>> in a buffer to pass to syslog()). In such a case, you can
borrow the
>> source for vsnprintf() from the glibc source. Many Gnu programs
>> explicitly include the source for functions added by the most
recent C
>> standards (such as vsnprintf()), as many older implementations
don't
>> have them. Chances are fair that if you've got the source for
any Gnu
>> program laying around, the code for vsnprintf() might be with
it.
>>
>> - --
>> Derek Martin ddm at pizzashack.org
>> - ---------------------------------------------
>> I prefer mail encrypted with PGP/GPG!
>> GnuPG Key ID: 0x81CFE75D
>> Retrieve my public key at http://pgp.mit.edu
>> Learn more about it at http://www.gnupg.org
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.0.6 (GNU/Linux)
>> Comment: For info see http://www.gnupg.org
>>
>> iD8DBQE9DPtedjdlQoHP510RAoCDAJ9229nJvYv+0++OEzk9iYcuuT9AiwCgj29r
>> BOyJKvoFQHg74CzRL9giFKQ=
>> =02N5
>> -----END PGP SIGNATURE-----
>> _______________________________________________
>> Discuss mailing list
>> Discuss at blu.org
>> http://www.blu.org/mailman/listinfo/discuss
>>
>
>--
>Jerry Feldman <gaf at blu.org>
>Boston Linux and Unix user group
>http://www.blu.org PGP key id:C5061EA9
>PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506
1EA9
>
>
>_______________________________________________
>Discuss mailing list
>Discuss at blu.org
>http://www.blu.org/mailman/listinfo/discuss
>
>
begin 600 Blank Document.psw
`
end
More information about the Discuss
mailing list