From 418f661cedc045889b23a0f2881a7049b3d008a4 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 7 Feb 2020 19:47:25 -0800 Subject: New "m" file open mode: non-truncating "w". Quite surprisingly ISO C lacks a way in fopen to open a file for writing such that it is not truncated if it already exists, and not opened in append mode. (But you will be glad to know that ISO C is adding incredibly useful features in this area, like Microsoft's fopen_s!) Let us add modes "m" and "m+" which will be like "w" and "w+", but without the truncation to zero length (no O_TRUNC is passed to open). * stream.c (w_fopen_mode): New static function. (open_file, open_tail, tail_strategy): Use w_fopen_mode instead of directly calling w_fopen. (do_parse_mode): Handle 'm' and set new notrunc flag. * stream.h (struct stdio_mode): New member, notrunc flag. (stdio_mode_init_blank, stdio_mode_init_r, stdio_mode_init_rpb): Initializer macros updated to include initializer for notrunc flag. * txr.1: Documented "m" mode. --- stream.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'stream.h') diff --git a/stream.h b/stream.h index 6fb245d6..2d7688c6 100644 --- a/stream.h +++ b/stream.h @@ -108,6 +108,7 @@ struct stdio_mode { unsigned create : 1; unsigned append : 1; unsigned binary : 1; + unsigned notrunc : 1; unsigned interactive : 1; unsigned unbuf : 1; unsigned linebuf : 1; @@ -115,9 +116,9 @@ struct stdio_mode { int redir[STDIO_MODE_NREDIRS][2]; }; -#define stdio_mode_init_blank { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 } -#define stdio_mode_init_r { 0, 1, 0, 0, 0, 0, 0, 0, 0, -1 } -#define stdio_mode_init_rpb { 0, 1, 1, 0, 0, 1, 0, 0, 0, -1 } +#define stdio_mode_init_blank { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1 } +#define stdio_mode_init_r { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1 } +#define stdio_mode_init_rpb { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, -1 } #define std_input (deref(lookup_var_l(nil, stdin_s))) #define std_output (deref(lookup_var_l(nil, stdout_s))) -- cgit v1.2.3