summaryrefslogtreecommitdiff
path: root/src/ptx.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1999-03-07 04:32:25 +0000
committerJim Meyering <jim@meyering.net>1999-03-07 04:32:25 +0000
commit59277d9406effa8b6c40bb9ad311e14eee58cc59 (patch)
tree00d3e569601760c64327f2dc4caba3aacc3f0764 /src/ptx.c
parent73444fc7782863585a25eb8d38d7de5dc0f9d7e6 (diff)
downloadcoreutils-59277d9406effa8b6c40bb9ad311e14eee58cc59.tar.xz
(swallow_file_in_memory): Slurp up the whole file at
once on MSDOS as well, but we have to relax the test for whether reading it succeeded.
Diffstat (limited to 'src/ptx.c')
-rw-r--r--src/ptx.c30
1 files changed, 19 insertions, 11 deletions
diff --git a/src/ptx.c b/src/ptx.c
index 69105fc11..798261c35 100644
--- a/src/ptx.c
+++ b/src/ptx.c
@@ -557,25 +557,33 @@ swallow_file_in_memory (const char *file_name, BLOCK *block)
if (fstat (file_handle, &stat_block) < 0)
error (EXIT_FAILURE, errno, file_name);
-#if !MSDOS
-
- /* On MSDOS, we cannot predict in memory size from file size, because of
- end of line conversions. */
-
if (S_ISREG (stat_block.st_mode))
{
+ size_t in_memory_size;
+
block->start = (char *) xmalloc ((size_t) stat_block.st_size);
- if (read (file_handle, block->start, (size_t) stat_block.st_size)
+ if ((in_memory_size = read (file_handle,
+ block->start, (size_t) stat_block.st_size))
!= stat_block.st_size)
- error (EXIT_FAILURE, errno, file_name);
+ {
+#if MSDOS
+ /* On MSDOS, in memory size may be smaller than the file
+ size, because of end of line conversions. But it can
+ never be smaller than half the file size, because the
+ minimum is when all lines are empty and terminated by
+ CR+LF. */
+ if (in_memory_size != (size_t)-1
+ && in_memory_size >= stat_block.st_size / 2)
+ block->start = (char *) xrealloc (block->start, in_memory_size);
+ else
+#endif /* not MSDOS */
- block->end = block->start + stat_block.st_size;
+ error (EXIT_FAILURE, errno, file_name);
+ }
+ block->end = block->start + in_memory_size;
}
else
-
-#endif /* not MSDOS */
-
{
block->start = (char *) xmalloc ((size_t) 1 << SWALLOW_REALLOC_LOG);
used_length = 0;