From 60731fb6e8108919d7636716f615fd3b8670d7ae Mon Sep 17 00:00:00 2001 From: David Galos Date: Tue, 2 Jul 2013 13:26:24 -0400 Subject: [PATCH] actually add the .c and .1 files --- sponge.1 | 15 +++++++++++++++ sponge.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 sponge.1 create mode 100644 sponge.c diff --git a/sponge.1 b/sponge.1 new file mode 100644 index 0000000..a33d81a --- /dev/null +++ b/sponge.1 @@ -0,0 +1,15 @@ +.TH SPONGE 1 sbase\-VERSION +.SH NAME +sponge \- soak up standard input and write to a file +.SH SYNOPSIS +.B sponge +.IR file +.SH DESCRIPTION +.B sponge +reads stdin completely, then writes the saved contents to +.IR file . +This makes it possible to easily create pipes which read from and write to +the same file. + +If the given file is a symbolic link, it writes to the links's destination +instead. diff --git a/sponge.c b/sponge.c new file mode 100644 index 0000000..8084166 --- /dev/null +++ b/sponge.c @@ -0,0 +1,40 @@ +/* See LICENSE file for copyright and license details. */ +#include + +#include "text.h" +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: sponge file\n"); +} + +int +main(int argc, char *argv[]) +{ + FILE *fp, *tmpfp; + + ARGBEGIN { + default: + usage(); + } ARGEND; + + if(argc != 1) + usage(); + + if(!(tmpfp = tmpfile())) + eprintf("tmpfile:"); + + concat(stdin, "", tmpfp, ""); + rewind(tmpfp); + + if(!(fp = fopen(argv[0], "w"))) + eprintf("sponge: '%s':", argv[0]); + concat(tmpfp, "", fp, argv[0]); + + fclose(fp); + fclose(tmpfp); + + return 0; +}