C, Dealing with variable argument functions - c

Let's say I want to do something like this
void my_printf(char *fmt,...) {
char buf[big enough];
sprintf(buf,fmt,...);
}
What is the proper way of passing the variable number of arguments directly to a function with accepts variable arguments?

sprintf has a va_list form called vsprintf. Pass the va_list you construct locally to it as the last argument.
void my_printf(char *fmt,...) {
va_list ap;
va_start(ap, fmt);
char buf[big enough];
vsprintf(buf,fmt,ap);
va_end(ap);
}

I'm not sure how useful this code will be, as it is C++, but it shows how to check, using a Win32 specific function vsnprintf(), that the buffer allocated is big enough and if not allocates a bigger one. And it returns a std::string, so you would have to use malloc/realloc to handle that. But what the hell:
string Format( const char * fmt, ... ) {
const int BUFSIZE = 1024;
int size = BUFSIZE, rv = -1;
vector <char> buf( size );
do {
va_list valist;
va_start(valist, fmt );
// if vsnprintf() returns < 0, the buffer wasn't big enough
// so increase buffer size and try again
rv = _vsnprintf( &buf[0], size, fmt, valist );
va_end( valist );
size *= 2;
buf.resize( size );
}
while( rv < 0 );
return string( &buf[0] );
}

You can us the vsprintf style functions to get printf style printing for your variable length parameter. However there is no requrement to do so. You can if you choose write your function to keep accepting parameters until it encounters a null pointer.
va_list ap;
char *param;
va_start(ap,fmt);
param = va_arg(ap,char*);
while(param)
{
do something...
param = va_arg(ap,char*);
}
or you can have the number of parameters as the first param to your function
void my_printf(int param_num,...)
{
va_list ap;
char *param;
va_start(ap,fmt);
while(param_num)
{
do something...
param = va_arg(ap,char*);
param_num--;
}
}
Its really up to you, the possibilities are limitless. I think the only real requirement to the ellipses is that it has at least one parameter before the ellipses.

Related

passing va_args to a custom format function

I'd like to use a va_args list in a custom formatter function.
I first tried to get vsnprintf() to work to verifiy that my argument pointer list itself is passed correctly => this worked
But when i tried to pass my argument pointer list to another function the pointer does not point to the right element on the stack
#include <stdio.h>
#include <stdarg.h>
int wrapper_snprintf(char *s, size_t n, const char *format, ...)
{
va_list arg;
//Copy Formated string to buffer => adds a terminating null character to the string
va_start(arg, format);
int res = vsnprintf(s, n, format, arg);
va_end(arg);
return res;
}
void Va_Args_Test_C(char *fmt, ...)
{
char test[30] = { 0 };
char test2[30] = { 0 };
{
//vsnprintf
va_list arg;
va_start(arg, fmt);
vsnprintf(test2, 30, fmt, arg);
va_end(arg);
}
{
//custom format function
va_list arg;
va_start(arg, fmt);
wrapper_snprintf(test2, 30, fmt, arg);
va_end(arg);
}
}
the first run worked. The correct argument is copied to string test[].
the second run did not work. test2[] contains the original string + some random number (which looks like a crappy pointer to me). So could you help me? What am I doing wrong?
In the first case you call vsnprintf(test2, 30, fmt, arg);.
This function is defined as
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
In the second case you call wrapper_snprintf(test2, 30, fmt, arg);, but your is defined differently as
int wrapper_snprintf(char *s, size_t n, const char *format, ...)
This is as if you would call snprintf instead of vsnprintf.
If you want a replacement for vsnprintf you have to define your function with an argument of type va_list instead of ...
int wrapper_snprintf(char *s, size_t n, const char *format, va_list ap)
{
//Copy Formated string to buffer => adds a terminating null character to the string
int res = vsnprintf(s, n, format, ap);
return res;
}

vsnprintf and varargs not working, weird results

I'm using vsnprintf (as far as I know correctly) but am getting weird results. I've simplified my code down to the following example:
void func(char *aaa, ...)
{
char *buf;
va_list args;
int size;
va_start(args, aaa);
size = vsnprintf(NULL, 0, aaa, args)+1;
buf = malloc(size);
vsnprintf(buf, size, aaa, args);
printf("%s",buf);
free(buf);
va_end(args);
}
int main(int argc, char **argv)
{
func("abc %s", "def\n");
return 0;
}
I'd expect "abc def" to get printed, but instead I get "abc" followed by some garbage text. Does anyone have an idea of where I messed up?
va_start(args, aaa);
size = vsnprintf(NULL, 0, aaa, args)+1; // Reads all arguments
buf = malloc(size);
vsnprintf(buf, size, aaa, args); // Tries to read all arguments again
printf("%s",buf);
free(buf);
va_end(args);
See the commented lines. You have to reset args between those two calls consuming all arguments.
Insert:
va_end(args);
va_start(args, aaa);
When you pass the handle args to vsnprintf() it will change it internally in your case. If used again it will not give the correct arguments.
Use va_copy() to create a copy of args if you wish to get your optional arguments twice.
void func(char *aaa, ...)
{
char *buf;
va_list args , argsc ; //argsc is used in the second vsnprintf()
int size;
va_start(args, aaa);
va_copy( argsc , args ) ; //make a copy of args
size = vsnprintf(NULL, 0, aaa, args)+1;
buf = malloc(size);
vsnprintf(buf, size, aaa, argsc); //use the copy since args is not valid
printf("%s",buf);
free(buf);
va_end(args);
va_end(argsc); //destroy both
}

Create a "template" (format) string

I want to be able to create a template string and then use it like this:
int execute_command(char *cmd) {
//...
}
char *template_command = "some_command %s some_args %s %d";
char *actual_command = template_command % (cmd1, arg1, 123);
// error, how do I do that?
int res = execute_command(actual_command);
If you know the maximum length of actual_command, then you can use either one of the following:
char actual_command[MAX_LEN+1];
// Option #1
sprintf(actual_command, template_command, cmd1, arg1, 123);
// Option #2
snprintf(actual_command, MAX_LEN+1, template_command, cmd1, arg1, 123);
If MAX_LEN is not defined correctly, then:
Option #1 will result with undefined behavior (possibly runtime exception)
Option #2 will result with incorrect result (contents of actual_command)
Use snprintf and malloc (snprint returns the length of the string it would have written was the buffer only big enough, and receives the size of the buffer). POSIX asnprintf packages that nicely.
If you don't have it, define your own like this:
char* my_asnprintf(const char* format, ...) {
va_list arg;
va_start(arg, format);
size_t n = 1 + vsnprintf((char*)format, 0, format, arg);
va_end(arg);
char* ret = malloc(n);
if(!ret)
return ret;
va_start(arg, format);
vsnprintf(ret, n, format, arg);
va_end(arg);
return ret;
}
Don't forget to free the buffer.

How to remove this warning: second parameter of ‘va_start’ not last named argument?

I have a function (see below) that is emitting the following warning:
second parameter of ‘va_start’ not last named argument
What does it means and how to remove it?
The function is as the following:
static int ui_show_warning(GtkWindow *parent, const gchar *fmt, size_t size, ...)
{
GtkWidget *dialog = NULL;
va_list args = NULL;
int count = -1;
char *msg = NULL;
if((msg = malloc(size + 1)) == NULL)
return -12;
va_start(args, fmt);
if((count = snprintf(msg, size, fmt, args)) < 0)
goto outer;
dialog = gtk_message_dialog_new(parent,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"%s", msg);
(void) gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
outer: {
if(args != NULL)
va_end(args);
if(msg != NULL)
free(msg);
return count;
}
}
You need to use size instead of fmt:
va_start(args, size);
It is size, not fmt, that is the last parameter that has an explicit name (as opposed to vararg parameters, which have no names). You need to pass the last named parameter to va_start in order for it to figure out the address in memory at which the vararg parameters start.
second parameter of ‘va_start’ not last named argument
What does it means and how to remove it?
Your function has named parameters parent, fmt and size. The C spec says you have to always pass the last named parameter to va_start, for compatibility with older compilers. So you must pass size, not fmt.
(But with a modern compiler, it might work anyway)
I think there is a confusion here: most of people only deal with prinf-like functionsh which have format and varargs. and they think they have to pass parameter name which describes format. however va_start has nothing to do with any kind of printf like format. this is just a function which calculates offset on the stack where unnamed parameters start.
I have the same problem on Ubuntu20.04,Contrary to the answer with the most likes,
the code at the beginning,
void sprintf(char *str, char *fmt, ...) {
va_list list;
int i, len;
va_start(list, 2);
...
}
and then, the code as follows
void sprintf(char *str, char *fmt, ...) {
va_list list;
int i, len;
va_start(list, fmt);
...
}
Problem was sovled.

Strange behavior (SEGFAULT) of a C program using stdargs (va_start)

I wrote a variadic C function which mission is to allocate the needed memory for a buffer, and then sprintf the args given to this function in that buffer. But I'm seeing a strange behavior with it. It works only once. If I have two calls for this function it segfaults.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
char *xsprintf(char * fmt, ...)
{
va_list ap;
char *part;
char *buf;
size_t len = strlen(fmt)+1;
va_start(ap, fmt);
while (part = va_arg(ap, char *))
len += strlen(part);
va_end(ap);
buf = (char*) malloc(sizeof(char)*len);
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
return buf;
}
int main(int argc, const char *argv[])
{
char *b;
b = xsprintf("my favorite fruits are: %s, %s, and %s", "coffee", "C", "oranges");
printf("size de buf is %d\n", strlen(b)); //this works. After it, it segfaults.
/*
free(b);
b = NULL;
*/
b = xsprintf("my favorite fruits are: %s, %s, and %s", "coffee", "C", "oranges");
printf("size de buf is %d\n", strlen(b));
printf("%s", b);
return 0;
}
here's the output of this program:
size de buf is 46
[1] 4305 segmentation fault ./xsprintftest
Am I doing something wrong? Shouldn't I have used va_start multiple times in a single function? Do you have any alternatives? Thanks a lot! :)
You should be using vsnprintf. Use it twice. Once with a NULL destination/zero size to find out the length of the buffer you need to allocate, then a second time to fill the buffer. That way your function will work even if all the arguments are not strings.
As written, it will fail if there are any non-string arguments (%d, %x, %f, etc.). And counting the number of % characters is not a valid way to get the number of arguments. Your result could be too many (if there are literal % characters encoded as %%) or too few (if arguments are also needed for %*s, %.*d, etc. width/precision specifiers).
Pass NULL as the last arg to xsprintf():
b = xsprintf("my favorite fruits are: %s, %s, and %s",
"coffee", "C", "oranges", (void*)0);
Then your while() loop will see the NULL and terminate properly.
As R.. mentions in the comment below and in another answer, the xsprintf function will fail if there are other format arguments. You are better off using vsprintf as explained in the other answer.
My intent here was simply to demonstrate the use of a sentinel with va_arg.
First off, try using vsnprintf. It's just a good idea.
That's not your problem, though. Your problem is that you can't call va_arg more times than there are arguments. It doesn't return the number of arguments. You must either pass in a parameter telling it how many there are, or extract the number of special tokens in the format string to figure out how many there must implicitly be.
That's the reason why printf can choke your program if you pass it too few arguments; it will just keep pulling things off the stack.
The problem is that in the bit of code where you're accessing the va_arg() list without a particular defined end:
va_start(ap, fmt);
while (part = va_arg(ap, char *))
len += strlen(part);
va_end(ap);
The stdargs.h facilities don't have any built-in method for determining when the end of the va_list() occurs - you need to have that explicitly done by a convention you come up with. Either using a sentinel value (as in bstpierre's answer), or by having a count provided. A count can be an explicit parameter that's provided, or it can be implicit (such as by counting the number of format specifiers in the format string like the printf() family does).
Of course, you also have the issue that your code currently only supports the one kind of format-specifier (%s), but I assumed that that's intentional at this point.
Thanks a lot for your answers and ideas! So I rewrote my function like this:
void fatal(const char *msg)/*{{{*/
{
fprintf(stderr, "program: %s", msg);
abort ();
}/*}}}*/
void *xmalloc(size_t size)/*{{{*/
{
register void *value = malloc(size);
if (value == 0)
fatal ("Virtual memory exhausted");
return value;
}/*}}}*/
void *xrealloc(void *ptr, size_t size)/*{{{*/
{
register void *value = realloc(ptr, size);
if (value == 0)
fatal ("Virtual memory exhausted");
return value;
}/*}}}*/
char *xsprintf(const char *fmt, ...)/*{{{*/
{
/* Heavily inspired from http://perfec.to/vsprintf/pasprintf */
va_list args;
char *buf;
size_t bufsize;
char *newbuf;
size_t nextsize;
int outsize;
int FIRSTSIZE = 20;
bufsize = 0;
for (;;) {
if(bufsize == 0){
buf = (char*) xmalloc(FIRSTSIZE);
bufsize = FIRSTSIZE;
}
else{
newbuf = (char *)xrealloc(buf, nextsize);
buf = newbuf;
bufsize = nextsize;
}
va_start(args, fmt);
outsize = vsnprintf(buf, bufsize, fmt, args);
va_end(args);
if (outsize == -1) {
/* Clear indication that output was truncated, but no
* clear indication of how big buffer needs to be, so
* simply double existing buffer size for next time.
*/
nextsize = bufsize * 2;
} else if (outsize == bufsize) {
/* Output was truncated (since at least the \0 could
* not fit), but no indication of how big the buffer
* needs to be, so just double existing buffer size
* for next time.
*/
nextsize = bufsize * 2;
} else if (outsize > bufsize) {
/* Output was truncated, but we were told exactly how
* big the buffer needs to be next time. Add two chars
* to the returned size. One for the \0, and one to
* prevent ambiguity in the next case below.
*/
nextsize = outsize + 2;
} else if (outsize == bufsize - 1) {
/* This is ambiguous. May mean that the output string
* exactly fits, but on some systems the output string
* may have been trucated. We can't tell.
* Just double the buffer size for next time.
*/
nextsize = bufsize * 2;
} else {
/* Output was not truncated */
break;
}
}
return buf;
}/*}}}*/
And it's working like a charm! Thanks a million times :)

Resources