Here, I was asking about an assignment of implementing the sys_open
syscall for OS161
which will be called by the open
function.
The definition of the open
function is as follows:
int open(const char *filename, int flags, ...);
and the man page looks like this:
Synopsis
#include <unistd.h>#include <fcntl.h>intopen(const char *filename, int flags);intopen(const char *filename, int flags, mode_t mode);
Since it's a variadic function, it can take as many arguments as passed, but only the cases with 2 or 3 arguments are defined.
My question was how to determine the number of arguments passed to open
, is it 2 or 3. The answer way that if the O_CREAT bit is set in the flags
, then a mode
is provided.
But I've seen some code within the OS that looks like this:
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC);
In this case, the value of the mode
would be whatever was in the a2
register and not a valid value.
1 - What happens in this case and what should I do in such case? How the arguments are passed? Does it pass a zero in the a2
register (not likely to happen)? Or does it just leave whatever value was in the register? At any case, how can I determine whether the last argument is valid or not?
I used to let the sys_open
function receive 3 arguments and only use the last one (mode
) if the O_CREAT
bit is set. But now since the function can be called with the bit set and with no third argument, I can still use the value of mode
even if it's invalid.
2 - What should the logic be for handling the arguments?
The way the syscall is issued is detailed in the question mentioned above.