views:

235

answers:

5

Today, looking at the man page for open(), I've noticed this function is 'overloaded':

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

I didn't thought it's possible on C. What's the 'trick' for achieving this ?

LATER EDIT:
So it's not really overloading, because when using varargs - you can only supply multiple arguments of the same type. So, is mode_t behind the scenes an int ?

+3  A: 

very short answer - varargs

KevinDTimm
and knockoff the downvotes for correct answers - terseness is a virtue.
KevinDTimm
+2  A: 

you can fake it using variable arguments list with ...

int function(int x, ...);
knittl
+20  A: 

It's using variable arguments. Those declarations appear only in the man page, as those 2 are the only ways you should call open(). The actual C function will be declared as e.g.

int open(const char *pathname,int flags,...);

With variable arguments, the arguments does not need to be the same type. printf is the obvious example of this.

In the case of open(), the first variable argument have to be mode_t if 'flags contain the O_CREAT flag because implementation of open() expects it to be mode_t (which behind the scenes is likely an unsigned int or unsigned long - but that has nothing to do with varargs)

nos
+5  A: 

"mode must be specified when O_CREAT is in the flags, and is ignored otherwise."

extern int open (__const char *__file, int __oflag, ...)

It uses varargs and only loads the mode variable argument if __oflag contains O_CREAT.

clahey
+7  A: 

C does make it possible to write function with a variable number of argument, such as printf.

With that being said, there is no reliable, cross-platform way in C to write a function that takes exactly 2 or 3 arguments; in general you must do something like

some_function(5, 6, 7, NULL);
some_function(5, 6, 8, 2, 5, NULL);

In other words, you must have a terminating "sentinal" argument. Alternatively, you could include the number of parameters somehow in an earlier parameter, such as

another_func(2, "hello", "world");
another_func(3, "goodbye", "cruel", "world");

The printf family of functions take this approach; the first format parameter contains the number of extra parameter needed; e.g. with printf("%f %f", 5.6, 7.11) you know that there must be 2 float parameters. However, this would be somewhat unsafe in a user-defined library function, since if you said my_printf("%s %f %f %f %s", 5.6) then you could get segfaults or worse. Fortunately, most C compilers will check your calls to printf at compile time to avoid this kind of issue.

In the case of open, the function is declared as having variable arguments, and the third parameter is only checked if O_CREAT is set. So this is how it "safely" determines whether a third argument is present. I put "safely" in quotes because technically there's no way for open to know at runtime how many parameters were actually passed. For example, the following calls would compile without any errors or warnings:

open("foo.txt", 5, "not an integer", 7);    // extra and invalid parameters
open("bar.txt", O_CREAT);                   // third parameter is missing
Eli Courtwright
If `some_function()` takes `int`s, you shouldn't end it with `NULL`, as `NULL` is just 0 in numeric context. `NULL` terminators work best with pointer parameters.
Chris Lutz
@Chris: Good point, I generally use NULL as the terminator regardless of data types, but you're right that it might be confusing, especially to people who don't know that 0 and NULL are the same, since they might think they could write something like `some_func(3, 2, 1, 0, -1, NULL)` when in fact this would cause 0 to act as the terminating parameter.
Eli Courtwright
Recent versions of gcc and the glibc headers together will produce a warning for your last two examples.
caf