diff options
author | Andy Wingo <wingo@pobox.com> | 2016-07-16 15:34:41 +0200 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2016-07-25 11:46:18 +0200 |
commit | aae356158412662c97b7178768bfe4be41749a3b (patch) | |
tree | 7aa62f3b9a505ecc4f5008a60221e54f2def5317 /libguile/fports.c | |
parent | e868fae6585d82c0b46a9a840913f0674dde0d3e (diff) | |
download | guile-aae356158412662c97b7178768bfe4be41749a3b.tar.gz |
Allow mkstemp! to have optional "mode" argument
* m4/mkstemp.m4: Remove.
* lib/mkstemp.c: Remove.
* lib/mkostemp.c: New file.
* m4/mkostemp.m4: New file.
* lib/Makefile.am:
* m4/gnulib-cache.m4:
* m4/gnulib-comp.m4: Remove mkstemp module, replace with mkostemp.
* libguile/fports.h:
* libguile/fports.c (scm_i_mode_to_open_flags): Factor out helper to
parse mode string to open flags.
(scm_open_file_with_encoding): Use the new helper.
* libguile/filesys.c:
(scm_i_mkstemp): Adapt to take optional second argument, being a mode
string. Use mkostemp.
(scm_mkstemp): Backwards compatible shim that calls scm_i_mkstemp.
* doc/ref/posix.texi:
* NEWS: Update.
* module/system/base/compile.scm (call-with-output-file/atomic): Pass
"wb" as mode, to cause O_BINARY to be added on MinGW.
Diffstat (limited to 'libguile/fports.c')
-rw-r--r-- | libguile/fports.c | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/libguile/fports.c b/libguile/fports.c index 271f3a0a1..f535f8a25 100644 --- a/libguile/fports.c +++ b/libguile/fports.c @@ -152,45 +152,17 @@ fport_canonicalize_filename (SCM filename) } } -/* scm_open_file_with_encoding - Return a new port open on a given file. - - The mode string must match the pattern: [rwa+]** which - is interpreted in the usual unix way. - - Unless binary mode is requested, the character encoding of the new - port is determined as follows: First, if GUESS_ENCODING is true, - 'file-encoding' is used to guess the encoding of the file. If - GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used - unless it is also false. As a last resort, the default port encoding - is used. It is an error to pass a non-false GUESS_ENCODING or - ENCODING if binary mode is requested. - - Return the new port. */ -SCM -scm_open_file_with_encoding (SCM filename, SCM mode, - SCM guess_encoding, SCM encoding) -#define FUNC_NAME "open-file" +int +scm_i_mode_to_open_flags (SCM mode, int *is_binary, const char *FUNC_NAME) { - SCM port; - int fdes, flags = 0, binary = 0; - unsigned int retries; - char *file; + int flags = 0; const char *md, *ptr; - if (SCM_UNLIKELY (!(scm_is_false (encoding) || scm_is_string (encoding)))) - scm_wrong_type_arg_msg (FUNC_NAME, 0, encoding, - "encoding to be string or false"); - - scm_dynwind_begin (0); - - file = scm_to_locale_string (filename); - scm_dynwind_free (file); - if (SCM_UNLIKELY (!scm_i_try_narrow_string (mode))) scm_out_of_range (FUNC_NAME, mode); md = scm_i_string_chars (mode); + *is_binary = 0; switch (*md) { @@ -215,7 +187,7 @@ scm_open_file_with_encoding (SCM filename, SCM mode, flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; break; case 'b': - binary = 1; + *is_binary = 1; #if defined (O_BINARY) flags |= O_BINARY; #endif @@ -229,6 +201,45 @@ scm_open_file_with_encoding (SCM filename, SCM mode, ptr++; } + return flags; +} + +/* scm_open_file_with_encoding + Return a new port open on a given file. + + The mode string must match the pattern: [rwa+]** which + is interpreted in the usual unix way. + + Unless binary mode is requested, the character encoding of the new + port is determined as follows: First, if GUESS_ENCODING is true, + 'file-encoding' is used to guess the encoding of the file. If + GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used + unless it is also false. As a last resort, the default port encoding + is used. It is an error to pass a non-false GUESS_ENCODING or + ENCODING if binary mode is requested. + + Return the new port. */ +SCM +scm_open_file_with_encoding (SCM filename, SCM mode, + SCM guess_encoding, SCM encoding) +#define FUNC_NAME "open-file" +{ + SCM port; + int fdes, flags, binary = 0; + unsigned int retries; + char *file; + + if (SCM_UNLIKELY (!(scm_is_false (encoding) || scm_is_string (encoding)))) + scm_wrong_type_arg_msg (FUNC_NAME, 0, encoding, + "encoding to be string or false"); + + scm_dynwind_begin (0); + + file = scm_to_locale_string (filename); + scm_dynwind_free (file); + + flags = scm_i_mode_to_open_flags (mode, &binary, FUNC_NAME); + for (retries = 0, fdes = -1; fdes < 0 && retries < 2; retries++) |