views:

979

answers:

3

I have approximately 30 variadic functions. Each one accepts a path as the final argument, i.e.:

bool do_foo(struct *f, int q, const char *fmt, ...)

In each function, I have to check that the expanded format is less then or equal to a certain size. So, I find myself copy / pasting the same chunk of code to check for how many characters vsnprintf() didn't print, set errno accordingly and bail out of the write.

What I would like to do is write a function to do this, which would return a statically allocated (expanded) string that is known to be a safe size, or newly initialized string (via memset) on failure, which could be checked against NULL. The checks also have to determine if the string is an absolute or relative path, which influences the safe size of the string. Its a lot of duplicate code and its starting to smell.

Is there a way that I can pass the contents of the elipsis from my function's entry to another function? Or do I have to call va_start() first, and then pass the va_list to the helper function?

Edit:

I am not at all opposed to passing the va_list to the helper, I just wanted to make sure that nothing else existed. It seems to me the compiler understands where the variadic arguments begin, so I was just curious if I could tell it to pass them along.

A: 

You have to pass va_list to the helper.

Andrew Grant
+9  A: 

You can't, you can only pass the arguments as a va_list. See the comp.lang.c FAQ.

In general, if you're writing variadic functions (that is, functions which take a variable number of arguments) in C, you should write two versions of each function: one which takes an ellipsis (...), and one which takes a va_list. The version taking an ellipsis should call va_start, call the version taking a va_list, call va_end, and return. There's no need for code duplication between the two versions of the function, since one calls the other.

Adam Rosenfield
Adam, thanks. I got so wrapped up in figuring out just how to search for this that I forgot to check comp.lang.c :)
Tim Post
+1  A: 

Probably you can use variadic macros - like this:

#define FOO(...)  do { do_some_checks; myfun(__VA_ARGS__); } while (0)

NB! Variadic macros are C99-only

qrdl
I was looking at those, that saves me from having to add a wrapper around the function that actually does the write.
Tim Post
c99 is no problem, my program is rather gcc / linux specific
Tim Post