diff --git a/LICENSE b/LICENSE index d959501..8f1caaf 100644 --- a/LICENSE +++ b/LICENSE @@ -12,6 +12,7 @@ MIT/X Consortium License © 2012 Christoph Lohmann <20h@r-36.net> © 2012 David Galos © 2012 Robert Ransom +© 2013 Jakob Kramer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), diff --git a/Makefile b/Makefile index 9aeb5c4..7f91bac 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ SRC = \ sleep.c \ sort.c \ split.c \ + sponge.c \ sync.c \ tail.c \ tee.c \ diff --git a/sponge.1 b/sponge.1 new file mode 100644 index 0000000..dca95b9 --- /dev/null +++ b/sponge.1 @@ -0,0 +1,14 @@ +.TH SPONGE 1 sbase\-VERSION +.SH NAME +sponge \- soak up standard input and write to a file +.SH SYNOPSIS +.B sponge +.RI [ file ] +.SH DESCRIPTION +.B sponge +reads stdin and writes to the specified file or stdout if no file is given. +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..456b2d4 --- /dev/null +++ b/sponge.c @@ -0,0 +1,51 @@ +/* See LICENSE file for copyright and license details. */ +#include + +#include "text.h" +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: %s [file]\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + FILE *fp, *tmpfp; + char *outname; + + ARGBEGIN { + default: + usage(); + } ARGEND; + + switch(argc) { + case 0: + fp = stdout; + outname = ""; + break; + case 1: + outname = argv[0]; + break; + default: + usage(); + } + + if(!(tmpfp = tmpfile())) + eprintf("tmpfile:"); + + concat(stdin, "", tmpfp, ""); + rewind(tmpfp); + + if(fp != stdout) + if(!(fp = fopen(argv[0], "w"))) + eprintf("fopen %s:", argv[0]); + concat(tmpfp, "", fp, outname); + + fclose(fp); + fclose(tmpfp); + + return 0; +}