summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Schier <nicolas@hjem.rpa.no>2015-01-14 06:47:19 +0100
committerJoey Hess <joeyh@joeyh.name>2015-01-19 13:00:46 -0400
commit51c6514da433a54a70b49bc928113119a08440a6 (patch)
treebe8f685fb0e3e3f7a6893d065f379573175379cf
parent5f0360ae33d21d8ba35679dc5c1dd3af9e36dab4 (diff)
downloadmoreutils-51c6514da433a54a70b49bc928113119a08440a6.tar.gz
sponge: add append option '-a' (Closes: #623197)
With this patch, option '-a' is introduced to append to the output file instead of doing backup and recreation. Original-by: Michael Stummvoll <michael@stummi.org> Signed-off-by: Nicolas Schier <nicolas@hjem.rpa.no>
-rw-r--r--sponge.c12
-rw-r--r--sponge.docbook18
2 files changed, 27 insertions, 3 deletions
diff --git a/sponge.c b/sponge.c
index 969703f..61e6a42 100644
--- a/sponge.c
+++ b/sponge.c
@@ -42,7 +42,8 @@
char *tmpname = NULL;
void usage() {
- printf("sponge <file>: soak up all input from stdin and write it to <file>\n");
+ printf("sponge [-a] <file>: soak up all input from stdin and write it "
+ "to <file>\n");
exit(0);
}
@@ -278,6 +279,12 @@ int main (int argc, char **argv) {
ssize_t i = 0;
size_t mem_available = default_sponge_size();
int tmpfile_used=0;
+ int append=0;
+
+ if ((argc == 3) && (!strcmp(argv[1], "-a"))) {
+ append = 1;
+ argc--, argv++;
+ }
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) {
usage();
@@ -342,6 +349,7 @@ int main (int argc, char **argv) {
/* If it's a regular file, or does not yet exist,
* attempt a fast rename of the temp file. */
if (((exists &&
+ !append &&
S_ISREG(statbuf.st_mode) &&
! S_ISLNK(statbuf.st_mode)
) || ! exists) &&
@@ -350,7 +358,7 @@ int main (int argc, char **argv) {
}
else {
/* Fall back to slow copy. */
- outfile = fopen(outname, "w");
+ outfile = fopen(outname, append ? "a" : "w");
if (!outfile) {
perror("error opening output file");
exit(1);
diff --git a/sponge.docbook b/sponge.docbook
index 07a2645..a892837 100644
--- a/sponge.docbook
+++ b/sponge.docbook
@@ -48,7 +48,7 @@ USA
<refsynopsisdiv>
<cmdsynopsis>
- <command>sed '...' file | grep '...' | sponge file</command>
+ <command>sed '...' file | grep '...' | sponge [-a] file</command>
</cmdsynopsis>
</refsynopsisdiv>
@@ -72,6 +72,22 @@ USA
stdout.</para>
</refsect1>
+
+ <refsect1>
+ <title>OPTIONS</title>
+
+ <variablelist>
+
+ <varlistentry>
+ <term><option>-a</option></term>
+ <listitem>
+ <para>Append the output to the specified file
+ instead possible backup and re-creation.</para>
+ </listitem>
+ </varlistentry>
+
+ </variablelist>
+ </refsect1>
<refsect1>
<title>AUTHOR</title>