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.
Unified · Split
| @@ -7,6 +7,7 @@ · 6 unmodified lines | |||
| 7 | #ifndef _NOLIBC_SYS_H | 7 | #ifndef _NOLIBC_SYS_H |
| 8 | #define _NOLIBC_SYS_H | 8 | #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 | ||
| 725 | static __attribute__((unused)) | 726 | static __attribute__((unused)) |
| @@ -735,9 +736,20 @@ int sys_open(const char *path, int flags, mode_t mode) · 9 unmodified lines | |||
| 735 | } | 736 | } |
| 736 | 737 | ||
| 737 | static __attribute__((unused)) | 738 | static __attribute__((unused)) |
| 738 | int open(const char *path, int flags, mode_t mode) | 739 | int 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); |