summaryrefslogtreecommitdiff
path: root/lib/read-file.c
diff options
context:
space:
mode:
authorDaiki Ueno <ueno@gnu.org>2020-05-27 08:14:44 +0200
committerDaiki Ueno <ueno@gnu.org>2020-05-27 14:14:00 +0200
commite4a38aadac2e90c6dfb317d0845746c200cf6697 (patch)
tree6ac9cb8b1f598dac5f9ccb36b7952cff3549b85f /lib/read-file.c
parente93bdf684a7ed2c9229c0f113e33dc639f1050d7 (diff)
downloadgnulib-e4a38aadac2e90c6dfb317d0845746c200cf6697.tar.gz
read-file: add flags to modify reading behavior
* lib/read-file.h (RF_BINARY): New define. (fread_file, read_file): Take FLAGS argument. (read_binary_file): Remove. * lib/read-file.c (internal_read_file): Merge into ... (read_file): ... here. * modules/read-file-tests (Files): Add "tests/macros.h". * tests/test-read-file.c (main): Refactor using ASSERT macro. * NEWS: Mention this change.
Diffstat (limited to 'lib/read-file.c')
-rw-r--r--lib/read-file.c43
1 files changed, 14 insertions, 29 deletions
diff --git a/lib/read-file.c b/lib/read-file.c
index 293bc3e8a5..904f1c9018 100644
--- a/lib/read-file.c
+++ b/lib/read-file.c
@@ -40,7 +40,7 @@
*LENGTH. On errors, *LENGTH is undefined, errno preserves the
values set by system functions (if any), and NULL is returned. */
char *
-fread_file (FILE *stream, size_t *length)
+fread_file (FILE *stream, int flags _GL_UNUSED, size_t *length)
{
char *buf = NULL;
size_t alloc = BUFSIZ;
@@ -134,9 +134,19 @@ fread_file (FILE *stream, size_t *length)
}
}
-static char *
-internal_read_file (const char *filename, size_t *length, const char *mode)
+/* Open and read the contents of FILENAME, and return a newly
+ allocated string with the content, and set *LENGTH to the length of
+ the string. The string is zero-terminated, but the terminating
+ zero byte is not counted in *LENGTH. On errors, *LENGTH is
+ undefined, errno preserves the values set by system functions (if
+ any), and NULL is returned.
+
+ If the RF_BINARY flag is set in FLAGS, the file is opened in binary
+ mode. */
+char *
+read_file (const char *filename, int flags, size_t *length)
{
+ const char *mode = (flags & RF_BINARY) ? "rbe" : "re";
FILE *stream = fopen (filename, mode);
char *out;
int save_errno;
@@ -144,7 +154,7 @@ internal_read_file (const char *filename, size_t *length, const char *mode)
if (!stream)
return NULL;
- out = fread_file (stream, length);
+ out = fread_file (stream, flags, length);
save_errno = errno;
@@ -161,28 +171,3 @@ internal_read_file (const char *filename, size_t *length, const char *mode)
return out;
}
-
-/* Open and read the contents of FILENAME, and return a newly
- allocated string with the content, and set *LENGTH to the length of
- the string. The string is zero-terminated, but the terminating
- zero byte is not counted in *LENGTH. On errors, *LENGTH is
- undefined, errno preserves the values set by system functions (if
- any), and NULL is returned. */
-char *
-read_file (const char *filename, size_t *length)
-{
- return internal_read_file (filename, length, "re");
-}
-
-/* Open (on non-POSIX systems, in binary mode) and read the contents
- of FILENAME, and return a newly allocated string with the content,
- and set LENGTH to the length of the string. The string is
- zero-terminated, but the terminating zero byte is not counted in
- the LENGTH variable. On errors, *LENGTH is undefined, errno
- preserves the values set by system functions (if any), and NULL is
- returned. */
-char *
-read_binary_file (const char *filename, size_t *length)
-{
- return internal_read_file (filename, length, "rbe");
-}