From 9f574b9b0ce132eb80e2fba3b43d5547c755ea9d Mon Sep 17 00:00:00 2001 From: rosenrost <52100845+rosenrost@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:39:38 +0100 Subject: [PATCH] Honour the "x" flag of fopen() The exclusive flag was parsed but ignored, so fopen(path, "wx") truncated an existing file. Fail with EEXIST instead, as required by C11. --- sources/fopen.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sources/fopen.c b/sources/fopen.c index dacf272..865f21e 100644 --- a/sources/fopen.c +++ b/sources/fopen.c @@ -74,6 +74,18 @@ FILE *fopen(const char *path, const char *mode) break; } + if (fp->__mode.__exclusive && fp->__mode.__create && !fp->__mode.__append) { + /* "wx": fail if the file already exists instead of truncating it */ + fd = Fopen(path, 0); + + if (fd >= 0) { + Fclose(fd); + __set_errno(EEXIST); + free(fp); + return NULL; + } + } + if (fp->__mode.__append) { fd = Fopen(path, fp->__mode.__write ? 1 + fp->__mode.__read : 0);