The definition of the UNIX open() function when used with the O_CREAT flag is that it requires a third argument named mode in order to set the files\' priv
We can use C macros to catch this problem.
#undef open
#define open(a, b, c) open(a, b, c)
Now you can't call open
without three arguments.
This is similar to writing macros for struct
initializers, to make sure users don't neglect to initialize some members:
#define foo_initializer(a, b, c) { .x = (a), .y = (b), .z = (c) }
If later we add a new member w
, we can extend foo_initializer
with a new argument. When we recompile the code base, the compiler will find all the places where it's only being given three arguments. Whereas "naked" initializers that neglect to initialize w
will continue to compile cleanly.