Skip to content

Commit 72eeebe

Browse files
committed
Only use open macro on GCC.
1 parent 835a820 commit 72eeebe

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Include/np_embed.h

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ NP_DECL(int) np_ftrylockfile(void *e);
338338

339339
#if !defined(NUITKAPYTHON_EMBED_BUILD) && !defined(NP_STDIO_ALREADY_LOADED)
340340

341+
#ifdef __GNUC__
341342
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
342343
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
343344

@@ -419,7 +420,7 @@ NP_DECL(int) np_ftrylockfile(void *e);
419420
#define NUM_ARGS1(_20,_19,_18,_17,_16,_15,_14,_13,_12,_11,_10,_9,_8,_7,_6,_5,_4,_3,_2,_1, n, ...) n
420421
#define NUM_ARGS0(...) NUM_ARGS1(__VA_ARGS__,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
421422
#define NUM_ARGS(...) IF(DEC(NUM_ARGS0(__VA_ARGS__)))(NUM_ARGS0(__VA_ARGS__),IF(IS_PAREN(__VA_ARGS__ ()))(0,1))
422-
423+
#endif // __GNUC__
423424

424425
// Preprocessor Translation
425426
#define FILE EFILE
@@ -429,7 +430,7 @@ NP_DECL(int) np_ftrylockfile(void *e);
429430
extern EFILE *stdin; /* Standard input stream. */
430431
extern EFILE *stdout; /* Standard output stream. */
431432
extern EFILE *stderr; /* Standard error output stream. */
432-
#endif
433+
#endif // __linux
433434

434435
/* File Opening and Closing */
435436
ALWAYS_INLINE NP_DECL(EFILE*) fopen(const char* file, const char* mode) {
@@ -439,33 +440,18 @@ ALWAYS_INLINE NP_DECL(EFILE*) _fopen(const char* file, const char* mode) {
439440
return np_fopen(file, mode);
440441
}
441442

442-
#ifdef _WIN32
443-
#ifdef __cplusplus
444-
ALWAYS_INLINE NP_DECL(int) open(const char *pathname, int flags, int mode = 0) {
445-
#else
446-
ALWAYS_INLINE NP_DECL(int) open(const char *pathname, int flags, int mode) {
447-
#endif
448-
#else
443+
#ifdef __GNUC__
449444
#ifdef __cplusplus
450445
ALWAYS_INLINE NP_DECL(int) open(const char *pathname, int flags, mode_t mode = 0) {
451446
#else
452447
ALWAYS_INLINE NP_DECL(int) open(const char *pathname, int flags, mode_t mode) {
453-
#endif
454448
#endif
455449
return np_open(pathname, flags, mode);
456450
}
457-
#ifdef _WIN32
458-
#ifdef __cplusplus
459-
ALWAYS_INLINE NP_DECL(int) _open(const char *pathname, int flags, int mode = 0) {
460-
#else
461-
ALWAYS_INLINE NP_DECL(int) _open(const char *pathname, int flags, int mode) {
462-
#endif
463-
#else
464451
#ifdef __cplusplus
465452
ALWAYS_INLINE NP_DECL(int) _open(const char *pathname, int flags, mode_t mode = 0) {
466453
#else
467454
ALWAYS_INLINE NP_DECL(int) _open(const char *pathname, int flags, mode_t mode) {
468-
#endif
469455
#endif
470456
return np_open(pathname, flags, mode);
471457
}
@@ -482,7 +468,29 @@ ALWAYS_INLINE NP_DECL(int) _open(const char *pathname, int flags, mode_t mode) {
482468
#define open7(a, b, c, d, e, f, g) open(a, b, c, d, e, f, g)
483469
#define open(...) CAT( open, NUM_ARGS( __VA_ARGS__ ) )( __VA_ARGS__ )
484470
#define _open(...) CAT( open, NUM_ARGS( __VA_ARGS__ ) )( __VA_ARGS__ )
485-
#endif
471+
#endif // !__cplusplus
472+
#else // __GNUC__
473+
ALWAYS_INLINE NP_DECL(int) open(const char *pathname, int flags, ... /* mode_t mode */ ) {
474+
va_list args;
475+
va_start(args, flags);
476+
mode_t mode = 0;
477+
if (flags & O_CREAT) {
478+
mode = va_arg(args, int);
479+
}
480+
va_end(args);
481+
return np_open(pathname, flags, mode);
482+
}
483+
ALWAYS_INLINE NP_DECL(int) _open(const char *pathname, int flags, ... /* mode_t mode */ ) {
484+
va_list args;
485+
va_start(args, flags);
486+
mode_t mode = 0;
487+
if (flags & O_CREAT) {
488+
mode = va_arg(args, int);
489+
}
490+
va_end(args);
491+
return np_open(pathname, flags, mode);
492+
}
493+
#endif // !__GNUC__
486494

487495
ALWAYS_INLINE NP_DECL(EFILE*) fdopen(int fd, const char *mode) {
488496
return np_fdopen(fd, mode);
@@ -546,7 +554,7 @@ ALWAYS_INLINE NP_DECL(EFILE*) wfopen(const wchar_t* file, const wchar_t* mode) {
546554
ALWAYS_INLINE NP_DECL(EFILE*) _wfopen(const wchar_t* file, const wchar_t* mode) {
547555
return np_wfopen(file, mode);
548556
}
549-
#endif
557+
#endif // _WIN32
550558

551559
ALWAYS_INLINE NP_DECL(EFILE*) tmpfile() {
552560
return np_tmpfile();

0 commit comments

Comments
 (0)