Unix O_CREAT flag without mode specified

前端 未结 6 1007
孤独总比滥情好
孤独总比滥情好 2020-12-18 01:23

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

6条回答
  •  执笔经年
    2020-12-18 02:23

    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.

提交回复
热议问题