master xplshn/aruu / cmd / pseudo / sponge.c
 1/* See LICENSE file for copyright and license details. */
 2
 3
 4#include <fcntl.h>
 5#include <stdlib.h>
 6#include <unistd.h>
 7
 8#include "util.h"
 9
10static void
11usage(void)
12{
13	eprintf("usage: %s file\n", argv0);
14}
15
16// ?man sponge: soak up input
17// ?man arguments: file
18// ?man soak up standard input and write to a file
19int
20main(int argc, char *argv[])
21{
22	char tmp[] = "/tmp/sponge-XXXXXX";
23	int fd, tmpfd;
24
25	ARGBEGIN {
26	default:
27		usage();
28	} ARGEND
29
30	if (argc != 1)
31		usage();
32
33	if ((tmpfd = mkstemp(tmp)) < 0)
34		eprintf("mkstemp:");
35	unlink(tmp);
36	if (concat(0, "<stdin>", tmpfd, "<tmpfile>") < 0)
37		return 1;
38	if (lseek(tmpfd, 0, SEEK_SET) < 0)
39		eprintf("lseek:");
40
41	if ((fd = creat(argv[0], 0666)) < 0)
42		eprintf("creat %s:", argv[0]);
43	if (concat(tmpfd, "<tmpfile>", fd, argv[0]) < 0)
44		return 1;
45
46	return 0;
47}