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