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
7#ifndef _NOLIBC_SYS_H7#ifndef _NOLIBC_SYS_H
8#define _NOLIBC_SYS_H8#define _NOLIBC_SYS_H
9 9
10#include <stdarg.h>
10#include "std.h"11#include "std.h"
11 12
12/* system includes */13/* system includes */
@@ -719,7 +720,7 @@ int mount(const char *src, const char *tgt, · 706 unmodified lines
719 720
720 721
721/*722/*
722 * int open(const char *path, int flags, mode_t mode);723 * int open(const char *path, int flags[, mode_t mode]);
723 */724 */
724 725
725static __attribute__((unused))726static __attribute__((unused))
@@ -735,9 +736,20 @@ int sys_open(const char *path, int flags, mode_t mode) · 9 unmodified lines
735}736}
736 737
737static __attribute__((unused))738static __attribute__((unused))
738int open(const char *path, int flags, mode_t mode)739int open(const char *path, int flags, ...)
739{740{
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);
741 753
742 if (ret < 0) {754 if (ret < 0) {
743 SET_ERRNO(-ret);755 SET_ERRNO(-ret);