Volker Schatz suggests that a useful improvement to `reservoir -o'
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 22 Mar 2008 14:18:23 +0000 (14:18 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 22 Mar 2008 14:18:23 +0000 (14:18 +0000)
would be to have it refrain from clobbering the output file if there
is no data at all to write to it. Implement this, preserving the
previous simple behaviour under the new name -O.

git-svn-id: svn://svn.tartarus.org/sgt/utils@7936 cda61777-01e9-0310-a592-d414129be87e

reservoir/reservoir
reservoir/reservoir.but

index fe01ba1..da7f576 100755 (executable)
@@ -3,14 +3,15 @@
 # reservoir -- read stdin until EOF, and _then_ write it all to stdout.
 
 $usage =
-  "usage: reservoir [ -o file ]\n".
-  "where: -o file                open and write to file after end of input\n".
+  "usage: reservoir [ -o file | -O file ]\n".
+  "where: -O file                open and write to file after end of input\n".
+  "       -o file                same, but only if output is non-empty\n".
   " also: reservoir --version    report version number\n" .
   "       reservoir --help       display this help text\n" .
   "       reservoir --licence    display (MIT) licence text\n";
 
 $licence =
-  "reservoir is copyright 2005 Simon Tatham.\n" .
+  "reservoir is copyright 2005,2008 Simon Tatham.\n" .
   "\n" .
   "Permission is hereby granted, free of charge, to any person\n" .
   "obtaining a copy of this software and associated documentation files\n" .
@@ -33,6 +34,7 @@ $licence =
   "SOFTWARE.\n";
 
 $outputfile = undef;
+$allowemptyoutput = 0;
 
 while ($_=shift @ARGV) {
   last if /^--$/;
@@ -51,10 +53,11 @@ while ($_=shift @ARGV) {
   } elsif ($arg eq "-licence" or $arg eq "-license") {
     print $licence;
     exit 0;
-  } elsif ($arg =~ /^o(.*)$/) {
-    $outputfile = $1;
+  } elsif ($arg =~ /^([oO])(.*)$/) {
+    $allowemptyoutput = ($1 eq "O");
+    $outputfile = $2;
     $outputfile = shift @ARGV if $outputfile eq "";
-    die "reservoir: expected file name after '-o'\n" if $outputfile eq "";
+    die "reservoir: expected file name after '-$1'\n" if $outputfile eq "";
   } else {
     die "reservoir: unrecognised option '-$arg'\n";
   }
@@ -67,6 +70,7 @@ $data = '';
 $data .= $_ while <STDIN>;
 
 if (defined $outputfile) {
+    exit unless length($data) or $allowemptyoutput;
     open OUT, ">$outputfile" or die "$outputfile: open: $!\n";
     select OUT;
 }
index 875f3db..4d1412c 100644 (file)
@@ -1,4 +1,4 @@
-\cfg{man-identity}{reservoir}{1}{2005-05-13}{Simon Tatham}{Simon Tatham}
+\cfg{man-identity}{reservoir}{1}{2008-03-22}{Simon Tatham}{Simon Tatham}
 
 \title Man page for \cw{reservoir}
 
@@ -8,8 +8,8 @@
 
 \U SYNOPSIS
 
-\c reservoir [ -o filename ]
-\e bbbbbbbbb   bb iiiiiiii
+\c reservoir [ -o filename | -O filename ]
+\e bbbbbbbbb   bb iiiiiiii   bb iiiiiiii
 
 \U DESCRIPTION
 
@@ -22,12 +22,20 @@ writes none of its outgoing data until all of its input has arrived.
 
 \U OPTIONS
 
-\dt \cw{-o} \e{filename}
+\dt \cw{-O} \e{filename}
 
 \dd Causes the output to be written to \e{filename} rather than to
 standard output. \e{filename} is not opened until after
 \cw{reservoir} detects end of file on its input.
 
+\dt \cw{-o} \e{filename}
+
+\dd Exactly like \cw{-O}, but with one special case: if there is no
+output at all to be written, \cw{reservoir} will not open the output
+file for writing at all. Hence, if the process which is supposed to
+generate the output completely fails to run, \e{filename} will not
+be overwritten.
+
 \U EXAMPLES
 
 If you have a program which filters its input in some way (for
@@ -90,9 +98,12 @@ Now \cw{reservoir} will not open \cw{temp.txt} for output until
 
 (This is not a reliable means of editing files in place. If
 something goes wrong half way through writing the output, part of
-your data will be lost. Also, the file is not replaced atomically.
-This method is very convenient in non-critical situations, but is
-not recommended for critical or automated use.)
+your data will be lost, although the default behaviour of \cw{-o}
+will at least avoid overwriting the file if something goes wrong
+\e{before} the output begins to be written. Also, the file is not
+replaced atomically. This method is very convenient in non-critical
+situations, such as when the target file is backed up in source
+control, but is not recommended for critical or automated use.)
 
 Another use for \cw{-o} is for requesting a list of files using
 \c{find}(1) or \c{ls}(1), without the output file appearing in the