summaryrefslogtreecommitdiff
path: root/sponge.c
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 /sponge.c
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>
Diffstat (limited to 'sponge.c')
-rw-r--r--sponge.c12
1 files changed, 10 insertions, 2 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);