2 * sponge.c - read in all available info from stdin, then output it to
3 * file named on the command line
5 * Copyright © 2006 Tollef Fog Heen
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/types.h>
33 printf("sponge <file>: suck in all input from stdin and write it to <file>\n");
37 int main(int argc
, char **argv
) {
39 size_t bufsize
= 8192;
44 if (argc
> 2 || (argc
== 2 && strcmp(argv
[1], "-h") == 0)) {
48 bufstart
= buf
= malloc(bufsize
);
54 while ((i
= read(0, buf
, bufsize
- bufused
)) > 0) {
56 if (bufused
== bufsize
) {
58 bufstart
= realloc(bufstart
, bufsize
);
64 buf
= bufstart
+ bufused
;
72 outfd
= open(argv
[1], O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
74 fprintf(stderr
, "Can't open %s: %s\n", argv
[1], strerror(errno
));
82 i
= write(outfd
, bufstart
, bufused
);