varargs.5 (2010 09)

v
varargs(5) varargs(5)
receives the variable argument list must pass the address pointer resulting from a call to
va_start()
on to any subsequent calls that need to access this same variable argument list. All routines that receive
this address pointer (v_print_log()
in this example) need only to use va_arg()
to access the origi-
nal variable argument list just as if they were the original routine to be passed the variable arguments.
In this example, one can imagine that there are a series of other routines (such as a
log_warning()
and log_message()
) that also call the v_print_log()
function.
#include <stdio.h>
#include <varargs.h>
#include <unistd.h>
int error_count;
/* VARARGS4 -- for lint */
int
log_errors(log_fp, func_name, err_num, msg_fmt, va_alist)
FILE *log_fp;
char *func_name;
int err_num;
char *msg_fmt;
va_dcl
{
va_list ap;
/* Print error header information */
(void) fprintf(log_fp, "\nERROR in process %d\n", getpid());
(void) fprintf(log_fp, " function \"%s\": ", func_name);
switch(err_num)
{
case ILLEGAL_OPTION:
(void) fprintf(log_fp, "illegal option\n");
break;
case CANNOT_PARSE:
(void) fprintf(log_fp, "cannot parse input file\n");
break;
...
}
/*
* Get pointer to first variable argument so that we can
* pass it on to v_print_log(). We do this so that
* v_print_log() can access the variable arguments passed
* to this routine.
*/
va_start(ap);
v_print_log(log_fp, msg_fmt, ap);
va_end(ap);
}
/* VARARGS2 -- for lint */
int
v_print_log(log_fp, fmt, ap)
FILE *log_fp;
char *fmt;
va_list ap;
{
/*
* If "%Y" is the first two characters in the format string,
2 Hewlett-Packard Company 2 HP-UX 11i Version 3: September 2010