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 ?
Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is ...
And C doesn't support Function Overloading.
An overloaded function is really just a set of different functions that happen to have the same name. The determination of which function to use for a particular call is resolved at compile time. In Java, function overloading is also known as compile-time polymorphism and static polymorphism.
Function overloading is a feature of object-oriented programming, which allows two or more functions to be created with the same name but with different arguments (different number of arguments or different data types of arguments). For example : // Function with the same name and different arguments.
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 don't 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)
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
"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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With