How to call A variable parameter function

C

CFAN

I have written a variable parameter function:
APR_DECLARE_NONSTD(apr_status_t) sas_strcat(char *buffer,size_t *
buffer_len ,... )
{
char *cp, *argp;
apr_size_t saved_lengths[MAX_SAVED_STR_LENGTHS];
apr_size_t total_len;
int nargs = 0;

/* Pass one --- find length of required string */

apr_size_t len = 0;
va_list adummy;

va_start(adummy, buffer_len );

while ((cp = va_arg(adummy, char *)) != NULL) {
apr_size_t cplen = strlen(cp);
if (nargs < MAX_SAVED_STR_LENGTHS) {
saved_lengths[nargs++] = cplen;
}
(apr_status_t) len += cplen;
}

va_end(adummy);

total_len = len ;
/* Allocate the required string */
if ( len > *buffer_len + 1)
return APR_ENOMEM;

cp = buffer;

/* Pass two --- copy the argument strings into the result space */

va_start(adummy, buffer_len );

nargs = 0;
while ((argp = va_arg(adummy, char *)) != NULL) {
if (nargs < MAX_SAVED_STR_LENGTHS) {
len = saved_lengths[nargs++];
}
else {
len = strlen(argp);
}
//Possible copy it to itself
//example: filename=strcat(filename,"_001","_002",NULL);
//maybe need not , VC memcpy do check overlap
memmove(cp, argp, len);
cp += len;
}

va_end(adummy);
*buffer_len = total_len+1;
*cp = '\0';
return APR_SUCCESS;
}

Now I want to add a function like
sas_strcat_with_enter(char *buffer,size_t * buffer_len ,... )

every thing is as same as the sas_strcat,except for this function will
add a "\n" and the end of string.
how can i make such funtions by calling the sas_strcat from within the
sas_strcat_with_enter function

Thanks.
 
B

Bill Pursell

I have written a variable parameter function:
It's called a "variadic function".
APR_DECLARE_NONSTD(apr_status_t) sas_strcat(char *buffer,size_t *
buffer_len ,... )

I'm guessing the APR_DECLARE_NONSTD is a macro which
expands to something useful on your system. For posts
here, it would help to either provide that definition or
remove invocations of it in posted code.
Now I want to add a function like
sas_strcat_with_enter(char *buffer,size_t * buffer_len ,... )

every thing is as same as the sas_strcat,except for this function will
add a "\n" and the end of string.
how can i make such funtions by calling the sas_strcat from within the
sas_strcat_with_enter function

You can't. But you can write a function that takes a va_list as
an argument and does all the work, and then have both
sas_strcat_with_unix_newline() and sas_strcat() call that function.
 
B

Ben Bacarisse

CFAN said:
I have written a variable parameter function:
APR_DECLARE_NONSTD(apr_status_t) sas_strcat(char *buffer,size_t *
buffer_len ,... )
Now I want to add a function like
sas_strcat_with_enter(char *buffer,size_t * buffer_len ,... )

every thing is as same as the sas_strcat,except for this function will
add a "\n" and the end of string.
how can i make such funtions by calling the sas_strcat from within the
sas_strcat_with_enter function

You wait months for a question about variadic functions to come along
and then two turn up in as many days...

This must have happened before because it is a FAQ (no. 15.12):

http://c-faq.com/varargs/handoff.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top