1

I have to write a function that accepts a variable number of arguments, similar to the open() function

int open (const char *filename, int flags[, mode_t mode])
  1. I had a look at the va_list functionality, but how can I find out how many arguments the user has supplied? I dont want to have a parameter in the function that indicates this.

  2. Being a sort of newbie, where can I look how the open() function is implemented? How does open() know whether the user has supplied the mode argument or not?

Thanks in advance.

5 Answers 5

7
  1. You cannot. The caller must either supply a trailing sentinel argument (e.g. a 0), or explicitly tell the function how many arguments to expect (e.g. the format fields in printf).

  2. open knows to expect the third argument because you will have set the O_CREAT flag in the flags argument.

0
3

Copied straight from the GCC page.

Here is a complete sample function that accepts a variable number of arguments. The first argument to the function is the count of remaining arguments, which are added up and the result returned. While trivial, this function is sufficient to illustrate how to use the variable arguments facility.

 #include <stdarg.h>
 #include <stdio.h>

 int
 add_em_up (int count,...)
 {
   va_list ap;
   int i, sum;

   va_start (ap, count);         /* Initialize the argument list. */

   sum = 0;
   for (i = 0; i < count; i++)
     sum += va_arg (ap, int);    /* Get the next argument value. */

   va_end (ap);                  /* Clean up. */
   return sum;
 }

 int
 main (void)
 {
   /* This call prints 16. */
   printf ("%d\n", add_em_up (3, 5, 5, 6));

   /* This call prints 55. */
   printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

   return 0;
 }
5
  • 1
    Here , count says how many arguments the user has supplied. Thats what I dont want. If you see the open() function, there is no count argument.
    – Korizon
    Jun 27, 2011 at 20:37
  • 1
    I think the point was that open doesn't take any argument specifying if the third parameter is passed or not. So, your example doesn't answer the question. Jun 27, 2011 at 20:38
  • Unfortunately you can't. You have to somehow include the information of how many extra parameters are there in the call. Jun 27, 2011 at 20:39
  • how does the open() function work, since there is no way the user says how many extra parameters there are. Also, if you read the value of the mode argument in open(), how would u know its user supplied and not some random value?
    – Korizon
    Jun 27, 2011 at 20:43
  • 2
    @horizon_06 open knows because you specify O_CREAT in the flags. If you: Specify O_CREAT and do not supply a 3. argument, open() will not know, and you will get undefined behavior.
    – nos
    Jun 27, 2011 at 20:48
1

What you're looking for is variadic function implementations. Here is a doc that will walk you through the entire chain.

1

It does use O_CREAT.

I find the implementation from source of kernel 6.1.0-9-amd64:

static __attribute__((unused))
int open(const char *path, int flags, ...)
{
    mode_t mode = 0;
    int ret;

    if (flags & O_CREAT) {
        va_list args;

        va_start(args, flags);
        mode = va_arg(args, mode_t);
        va_end(args);
    }

    ret = sys_open(path, flags, mode);

    if (ret < 0) {
        SET_ERRNO(-ret);
        ret = -1;
    }
    return ret;
}
-1

The short answer to the question, "How does open know if it was passed two or three arguments?" is: it's magic. The long answer is that the implementation is allowed to do certain things that you are not (within standard C, that is).

Another example is main. It has two valid prototypes:

int main(void);

and

int main(int argc, char *argv[]); /* or equivalent */

The implementation must allow you to use any of the two, and "deduce", which is the one you used.

The answer to the question, "How can I do something similar in my code?", is: you can't, at least not within the constraints imposed by the C standard.

9
  • The answer by Oli Charlesworth is what I was looking for, so basically, either include a trailing sentinel value, or explicitly say how many arguments to expect.
    – Korizon
    Jun 27, 2011 at 20:49
  • 4
    -1: open is not magic. It simply uses the flags to determine what to expect. Jun 27, 2011 at 21:20
  • @Oli: open is not magic, but the implementation of open, which allows one to pass either two or three parameters to it is "magic", in the sense that it can't be done in standard C. If it weren't "magic", it would be possible to write open-like function in standard C (taking either 2 or 3 parameters). Jun 27, 2011 at 22:08
  • 1
    @Alok: Of course it is possible! If a particular flag is set, then look for a third parameter. Jun 27, 2011 at 22:08
  • @Oli: in other words, can you write a function add() in standard C, which will add either 2 or 3 numbers passed to it, depending upon whether the first number is negative or not? So, I should be able to call add(1, 2, 3); to get 6, and add(-1, 5); to get 4? Note that I must be able to call add() with different number of parameters. Jun 27, 2011 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.