I’m in the process of open sourcing some code which was written by myself and a few other individuals. In one of the code segments written by one of the other individuals, I am seeing the following:
open(filePath, O_RDONLY | O_CREAT, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
open(filePath, O_WRONLY | O_CREAT, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Some how, this compiled on their system when we needed to execute it. However, on my system, GCC complains that open() is being passed too many arguments (which from my perspective, is accurate, as open only accepts 2 or 3 arguments to my knowledge). I’m also rather confused as to why O_RDONLY | O_CREAT and O_WRONLY | O_CREAT are both being passed in the same call to open.
While I wait to hear back from the other developer, does anyone have an idea as to why there are 4 arguments in this call to open()?
In the POSIX standard, the C interface to
open()is declared as:Now, you’re only supposed to provide one extra argument, but there’s nothing the compiler can do to stop you providing more. If you’re compiling with C++, then it could be that you have overloaded declarations of
open():This would more accurately reflect what is expected. There are definitely some issues to be resolved implementing that, but it could be done.
So, the compilation probably worked on one machine because it was using the ‘official’ declaration with variable length argument list. On your system, you seem to be better constrained.
The original code is broken. Fix it. And fix it in the new code.