History

tools/nolibc/sys: make open() take a vararg on the 3rd argument

Let's pass a vararg to open() so that it remains compatible with existing code. The arg is only dereferenced when flags contain O_CREAT. The function is generally not inlined anymore, causing an extra call (total 16 extra bytes) but it's still optimized for constant propagation, limiting the excess to no more than 16 bytes in practice when open() is called without O_CREAT, and ~40 with O_CREAT, which remains reasonable.

Willy Tarreau committed 4 years ago

Commit a7604ba1 parent acab7bc1 file · +15 −3Browse files

Unified · Split

tools/include/nolibc/sys.h+15−3
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 #ifndef _NOLIBC_SYS_H
88 #define _NOLIBC_SYS_H
99
10+#include <stdarg.h>
1011 #include "std.h"
1112
1213 /* system includes */
@@ -719,7 +720,7 @@ int mount(const char *src, const char *tgt, · 706 unmodified lines
719720
720721
721722 /*
722− * int open(const char *path, int flags, mode_t mode);
723+ * int open(const char *path, int flags[, mode_t mode]);
723724 */
724725
725726 static __attribute__((unused))
@@ -735,9 +736,20 @@ int sys_open(const char *path, int flags, mode_t mode) · 9 unmodified lines
735736 }
736737
737738 static __attribute__((unused))
738−int open(const char *path, int flags, mode_t mode)
739+int open(const char *path, int flags, ...)
739740 {
740− int ret = sys_open(path, flags, mode);
741+ mode_t mode = 0;
742+ int ret;
743+
744+ if (flags & O_CREAT) {
745+ va_list args;
746+
747+ va_start(args, flags);
748+ mode = va_arg(args, mode_t);
749+ va_end(args);
750+ }
751+
752+ ret = sys_open(path, flags, mode);
741753
742754 if (ret < 0) {
743755 SET_ERRNO(-ret);