[lug] variable args
Holshouser, David
dholshou at ball.com
Fri May 4 14:07:47 MDT 2001
I cannot use [vf]printf for 2 reasons.
I am working in a restricted environment (hardware&software. VxWorks rtos)
and must (by mandate from above) use a function called logMsg to do any
printing.
notice the 126 in the first output line.
if I change the magic number in the call to strncpy, that also changes.
output:
-------
1 126 44367872
2 44367872 431
void test(char *fmt, ...)
{
char str[128];
va_list ap;
str[0]='\0';
va_start(ap, fmt);
strncpy(str, fmt, 126);
printf(str, va_arg( ap, va_list ) );
fprintf(stderr, str, va_arg( ap, va_list ) );
va_end(ap);
}
> -----Original Message-----
> From: D. Stimits [mailto:stimits at idcomm.com]
> Sent: Friday, May 04, 2001 1:51 PM
> To: lug at lug.boulder.co.us
> Subject: Re: [lug] variable args
>
>
> Out of curiosity, I'm wondering what would happen if you did
> not buffer?
> printf() buffers since it uses stdout, but if you instead try
> fprintf()
> and print to stderr, buffering is removed. Buffering could be doing
> something odd that stderr would not act odd with. I am also
> curious why
> vprintf can't be used?
>
> Also, you might want to temporarily alter the "test" function
> so that it
> does not use the "fmt" argument directly; instead, strcpy (or strncpy)
> fmt to a temporary buffer; I suggest this because for testing you want
> to be guaranteed that "fmt" can't be changed to point at something
> different as things go along, and a separate copy will guarantee the
> variable argument setup does not change it.
>
> D. Stimits, stimits at idcomm.com
>
> "Holshouser, David" wrote:
> >
> > FYI
> >
> > Calling va_arg(ap, va_list) only seems to return the first
> argument in the
> > original list.
> > And it turns out that I can't use vprint (or printf for
> that matter).
> > Back to the drawing board.
> >
> > The following code produces:
> > 1 2 3
> > 1 1627655920 144
> >
> > #include<stdio.h>
> > #include<stdarg.h>
> >
> > void test(char *fmt, ...)
> > {
> > va_list ap;
> > va_start(ap, fmt);
> > vprintf(fmt, ap);
> > printf( fmt, va_arg( ap, va_list ) );
> > va_end(ap);
> > }
> >
> > int main (void)
> > {
> > test("%d %d %d\n", 1, 2, 3);
> > }
> >
> > --
> > David Holshouser
> > Engineer I
> > Ball Aerospace & Technologies Corp.
> > (303)939-5085 dholshou at ball.com
> >
> > _______________________________________________
> > Web Page: http://lug.boulder.co.us
> > Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>
More information about the LUG
mailing list