diff options
author | Andreas Gruenbacher <agruen@linbit.com> | 2011-02-03 23:07:15 +0100 |
---|---|---|
committer | Andreas Gruenbacher <agruen@linbit.com> | 2011-02-03 23:22:56 +0100 |
commit | 3c776b08449581b44afddeea68942531d2f047b2 (patch) | |
tree | 508b45a2bd3598c9bc31624203056085df496a95 /src | |
parent | 4c3004c17fa72b5b87a1eec29ad41cd6549d0017 (diff) | |
download | patch-3c776b08449581b44afddeea68942531d2f047b2.tar.gz |
Also check if the input file is seekable if a filename is given (-i)
* src/pch.c (open_patch_file): Also check if the input file is
seekable if a filename is given (-i).
* tests/pipe: New file. Test this.
* tests/Makefile.am (TESTS): Add it.
Diffstat (limited to 'src')
-rw-r--r-- | src/pch.c | 74 |
1 files changed, 35 insertions, 39 deletions
@@ -106,52 +106,48 @@ void open_patch_file (char const *filename) { file_offset file_pos = 0; + file_offset pos; struct stat st; + if (!filename || !*filename || strEQ (filename, "-")) - { - file_offset stdin_pos; -#if HAVE_SETMODE_DOS - if (binary_transput) - { - if (isatty (STDIN_FILENO)) - fatal ("cannot read binary data from tty on this platform"); - setmode (STDIN_FILENO, O_BINARY); - } -#endif - if (fstat (STDIN_FILENO, &st) != 0) - pfatal ("fstat"); - if (S_ISREG (st.st_mode) && (stdin_pos = file_tell (stdin)) != -1) - { - pfp = stdin; - file_pos = stdin_pos; - } - else - { - size_t charsread; - int fd = make_tempfile (&TMPPATNAME, 'p', NULL, O_RDWR | O_BINARY, 0); - TMPPATNAME_needs_removal = 1; - pfp = fdopen (fd, "w+b"); - if (! pfp) - pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME)); - for (st.st_size = 0; - (charsread = fread (buf, 1, bufsize, stdin)) != 0; - st.st_size += charsread) - if (fwrite (buf, 1, charsread, pfp) != charsread) - write_fatal (); - if (ferror (stdin) || fclose (stdin) != 0) - read_fatal (); - if (fflush (pfp) != 0 - || file_seek (pfp, (file_offset) 0, SEEK_SET) != 0) - write_fatal (); - } - } + pfp = stdin; else { pfp = fopen (filename, binary_transput ? "rb" : "r"); if (!pfp) pfatal ("Can't open patch file %s", quotearg (filename)); - if (fstat (fileno (pfp), &st) != 0) - pfatal ("fstat"); + } +#if HAVE_SETMODE_DOS + if (binary_transput) + { + if (isatty (fileno (pfp))) + fatal ("cannot read binary data from tty on this platform"); + setmode (fileno (pfp), O_BINARY); + } +#endif + if (fstat (fileno (pfp), &st) != 0) + pfatal ("fstat"); + if (S_ISREG (st.st_mode) && (pos = file_tell (pfp)) != -1) + file_pos = pos; + else + { + size_t charsread; + int fd = make_tempfile (&TMPPATNAME, 'p', NULL, O_RDWR | O_BINARY, 0); + FILE *read_pfp = pfp; + TMPPATNAME_needs_removal = 1; + pfp = fdopen (fd, "w+b"); + if (! pfp) + pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME)); + for (st.st_size = 0; + (charsread = fread (buf, 1, bufsize, read_pfp)) != 0; + st.st_size += charsread) + if (fwrite (buf, 1, charsread, pfp) != charsread) + write_fatal (); + if (ferror (read_pfp) || fclose (read_pfp) != 0) + read_fatal (); + if (fflush (pfp) != 0 + || file_seek (pfp, (file_offset) 0, SEEK_SET) != 0) + write_fatal (); } p_filesize = st.st_size; if (p_filesize != (file_offset) p_filesize) |