summaryrefslogtreecommitdiff
path: root/src/dd.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1999-04-26 13:31:49 +0000
committerJim Meyering <jim@meyering.net>1999-04-26 13:31:49 +0000
commit9622d451400664794421c44343dbbdfc7b2d6704 (patch)
tree21fabac533d03516da2092ec9bb41a463adab870 /src/dd.c
parent890a087101afdaf2de7442c3d737e8d29b92ded8 (diff)
downloadcoreutils-9622d451400664794421c44343dbbdfc7b2d6704.tar.xz
(main): If you can't open an output file (with seek=...) read-write,
then open it for write and report an error if we can't seek.
Diffstat (limited to 'src/dd.c')
-rw-r--r--src/dd.c20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/dd.c b/src/dd.c
index eebd2143e..09d5f44b1 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -1043,16 +1043,18 @@ main (int argc, char **argv)
if (output_file != NULL)
{
+ mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+ int opts
+ = (O_CREAT
+ | (seek_record || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
+
/* Open the output file with *read* access only if we might
- need to read to satisfy a `seek=' request. */
- int omode = (seek_record ? O_RDWR : O_WRONLY) | O_CREAT;
-
- if (seek_record == 0 && !(conversions_mask & C_NOTRUNC))
- omode |= O_TRUNC;
- output_fd = open (output_file, omode,
- (S_IRUSR | S_IWUSR
- | S_IRGRP | S_IWGRP
- | S_IROTH | S_IWOTH));
+ need to read to satisfy a `seek=' request. If we can't read
+ the file, go ahead with write-only access; it might work. */
+ if (! seek_record
+ || (output_fd = open (output_file, O_RDWR | opts, perms)) < 0)
+ output_fd = open (output_file, O_WRONLY | opts, perms);
+
if (output_fd < 0)
error (1, errno, "%s", output_file);
#if HAVE_FTRUNCATE