Hash :
d0450ae9
Author :
Date :
2024-01-17T21:53:15
Improve naming of local variables. * gnulib-tool, posix-modules, build-aux/relocatable.sh.in, build-aux/reloc-ldflags, build-aux/install-reloc, tests/init.sh, m4/argz.m4: Rename save_IFS to saved_IFS. * m4/*.m4: Rename save_LIBS to saved_LIBS. * m4/00gnulib.m4, m4/gnulib-common.m4, m4/warn-on-use.m4, m4/wchar_h.m4: Rename save_ac_compile to saved_ac_compile. * m4/00gnulib.m4: Rename ac_save_ac_compile to ac_saved_ac_compile. * m4/gnulib-common.m4: Rename save_ac_compile_for_check_decl to saved_ac_compile_for_check_decl. * m4/assert_h.m4, m4/manywarnings.m4, m4/stdalign.m4, m4/visibility.m4: Rename gl_save_CFLAGS to gl_saved_CFLAGS. * m4/dirfd.m4: Rename dirfd_save_CFLAGS to gl_saved_CFLAGS. * m4/include_next.m4, m4/pread.m4, m4/pwrite.m4, m4/termcap.m4, m4/terminfo.m4: Rename gl_save_CPPFLAGS to gl_saved_CPPFLAGS. * m4/lib-link.m4: Rename ac_save_CPPFLAGS to acl_save_CPPFLAGS. * m4/iconv.m4, m4/readline.m4: Rename am_save_CPPFLAGS to gl_saved_CPPFLAGS. * m4/gettext.m4: Rename gt_save_CPPFLAGS to gt_saved_CPPFLAGS. * m4/ansi-c++.m4: Rename gl_save_CXX to gl_saved_CXX. * m4/manywarnings-c++.m4: Rename gl_save_CXXFLAGS to gl_saved_CXXFLAGS. * m4/lib-link.m4: Rename acl_save_ifs to acl_saved_IFS. * m4/lib-prefix.m4: Rename acl_save_IFS to acl_saved_IFS. * m4/progtest.m4: Rename ac_save_IFS to gt_saved_IFS. * m4/lib-link.m4: Rename save_libdir to saved_libdir, acl_save_libdir to acl_saved_libdir. * m4/ld-version-script.m4: Rename save_LDFLAGS to saved_LDFLAGS. * m4/acl.m4, m4/getgroups.m4: Rename ac_save_LIBS to gl_saved_LIBS. * m4/lib-link.m4: Rename ac_save_LIBS to acl_saved_LIBS. * m4/getdomainname.m4, m4/gethostname.m4, m4/getloadavg.m4, m4/hostent.m4, m4/inet_ntop.m4, m4/inet_pton.m4, m4/pthread_mutex_timedlock.m4, m4/pthread_sigmask.m4, m4/pthread-spin.m4, m4/readutmp.m4, m4/selinux-selinux-h.m4, m4/servent.m4, m4/socketlib.m4, m4/termcap.m4, m4/terminfo.m4, m4/thread.m4: Rename gl_save_LIBS to gl_saved_LIBS. * m4/getaddrinfo.m4, m4/iconv.m4, m4/readline.m4: Rename am_save_LIBS to gl_saved_LIBS. * m4/gettext.m4, m4/intlmacosx.m4: Rename gt_save_LIBS to gt_saved_LIBS. * m4/libunistring.m4: Rename glus_save_LIBS to glus_saved_LIBS. * m4/nanosleep.m4: Rename nanosleep_save_libs to gl_saved_LIBS. * m4/lib-prefix.m4: Rename acl_save_prefix to acl_saved_prefix, acl_save_exec_prefix to acl_saved_exec_prefix. * m4/configmake.m4: Rename gl_save_<variable> to gl_saved_<variable>. * m4/terminfo.m4: Likewise. * m4/gettext.m4: Rename gt_save_<variable> to gt_saved_<variable>. * m4/javaexec.m4: Rename save_CLASSPATH to gt_saved_CLASSPATH. * m4/warnings.m4: Rename gl_save_compiler_FLAGS to gl_saved_compiler_FLAGS. * lib/pread.c, lib/pwrite.c, lib/read-file.c, lib/tempname.c: Rename save_errno to saved_errno. * lib/read-file.c: Rename save_alloc to saved_alloc. * lib/strptime.c: Rename save_wday to saved_wday, save_mday to saved_mday, save_mon to saved_mon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
/* read-file.c -- read file contents into a string
Copyright (C) 2006, 2009-2024 Free Software Foundation, Inc.
Written by Simon Josefsson and Bruno Haible.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "read-file.h"
/* Get fstat. */
#include <sys/stat.h>
/* Get ftello. */
#include <stdio.h>
/* Get PTRDIFF_MAX. */
#include <stdint.h>
/* Get malloc, realloc, free. */
#include <stdlib.h>
/* Get memcpy, memset_explicit. */
#include <string.h>
/* Get errno. */
#include <errno.h>
/* Read a STREAM 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_SENSITIVE flag is set in FLAGS:
- You should control the buffering of STREAM using 'setvbuf'. Either
clear the buffer of STREAM after closing it, or disable buffering of
STREAM before calling this function.
- The memory buffer internally allocated will be cleared upon failure. */
char *
fread_file (FILE *stream, int flags, size_t *length)
{
char *buf = NULL;
size_t alloc = BUFSIZ;
/* For a regular file, allocate a buffer that has exactly the right
size. This avoids the need to do dynamic reallocations later. */
{
struct stat st;
if (fstat (fileno (stream), &st) >= 0 && S_ISREG (st.st_mode))
{
off_t pos = ftello (stream);
if (pos >= 0 && pos < st.st_size)
{
off_t alloc_off = st.st_size - pos;
/* '1' below, accounts for the trailing NUL. */
if (PTRDIFF_MAX - 1 < alloc_off)
{
errno = ENOMEM;
return NULL;
}
alloc = alloc_off + 1;
}
}
}
if (!(buf = malloc (alloc)))
return NULL; /* errno is ENOMEM. */
{
size_t size = 0; /* number of bytes read so far */
int saved_errno;
for (;;)
{
/* This reads 1 more than the size of a regular file
so that we get eof immediately. */
size_t requested = alloc - size;
size_t count = fread (buf + size, 1, requested, stream);
size += count;
if (count != requested)
{
saved_errno = errno;
if (ferror (stream))
break;
/* Shrink the allocated memory if possible. */
if (size < alloc - 1)
{
if (flags & RF_SENSITIVE)
{
char *smaller_buf = malloc (size + 1);
if (smaller_buf == NULL)
memset_explicit (buf + size, 0, alloc - size);
else
{
memcpy (smaller_buf, buf, size);
memset_explicit (buf, 0, alloc);
free (buf);
buf = smaller_buf;
}
}
else
{
char *smaller_buf = realloc (buf, size + 1);
if (smaller_buf != NULL)
buf = smaller_buf;
}
}
buf[size] = '\0';
*length = size;
return buf;
}
{
char *new_buf;
size_t saved_alloc = alloc;
if (alloc == PTRDIFF_MAX)
{
saved_errno = ENOMEM;
break;
}
if (alloc < PTRDIFF_MAX - alloc / 2)
alloc = alloc + alloc / 2;
else
alloc = PTRDIFF_MAX;
if (flags & RF_SENSITIVE)
{
new_buf = malloc (alloc);
if (!new_buf)
{
/* BUF should be cleared below after the loop. */
saved_errno = errno;
break;
}
memcpy (new_buf, buf, saved_alloc);
memset_explicit (buf, 0, saved_alloc);
free (buf);
}
else if (!(new_buf = realloc (buf, alloc)))
{
saved_errno = errno;
break;
}
buf = new_buf;
}
}
if (flags & RF_SENSITIVE)
memset_explicit (buf, 0, alloc);
free (buf);
errno = saved_errno;
return NULL;
}
}
/* 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. If the RF_SENSITIVE flag is set in FLAGS, the memory buffer
internally allocated will be cleared upon failure. */
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;
if (!stream)
return NULL;
if (flags & RF_SENSITIVE)
setvbuf (stream, NULL, _IONBF, 0);
out = fread_file (stream, flags, length);
if (fclose (stream) != 0)
{
if (out)
{
if (flags & RF_SENSITIVE)
memset_explicit (out, 0, *length);
free (out);
}
return NULL;
}
return out;
}