12

I just discovered that the open() (man 2 open) system call has two versions:

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

And indeed, one can use either in a single C file and both would work. How can standard C achieve this?

lang2
  • 11,433
  • 18
  • 83
  • 133

2 Answers2

9

In fact, it's not C++-style function overloading. It's just that open() is variadic:

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

And only if "flags" require it, will it look for the third argument.

  • didn't see any relationship between flags and mode in the man page. Care to elaborate? – lang2 Mar 01 '13 at 06:05
  • @lang2 the mode is *only used if required,* for example, when `O_CREAT` is specified as flag. There's no sense in talking about the file mode when, for example, you open a file for reading... –  Mar 01 '13 at 06:07
5

It can be done as a variable argument function.

The POSIX documentation for open specifies it like this:

int open(const char *path, int oflag, ...);
Mat
  • 202,337
  • 40
  • 393
  • 406