aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

sponge.c (1110B)


      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 
      9 static void
     10 usage(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
     18 int
     19 main(int argc, char *argv[])
     20 {
     21   char tmp[] = "/tmp/sponge-XXXXXX";
     22   int  fd, tmpfd, ret = 0;
     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   if (unlink(tmp) < 0)
     37     eprintf("unlink %s:", tmp);
     38   if (concat(0, "<stdin>", tmpfd, "<tmpfile>") < 0) {
     39     if (close(tmpfd) < 0)
     40       weprintf("close tmpfile:");
     41     return 1;
     42   }
     43   if (lseek(tmpfd, 0, SEEK_SET) < 0)
     44     eprintf("lseek:");
     45 
     46   if ((fd = creat(argv[0], 0666)) < 0)
     47     eprintf("creat %s:", argv[0]);
     48   if (concat(tmpfd, "<tmpfile>", fd, argv[0]) < 0)
     49     ret = 1;
     50   if (close(tmpfd) < 0) {
     51     weprintf("close tmpfile:");
     52     ret = 1;
     53   }
     54   if (close(fd) < 0) {
     55     weprintf("close %s:", argv[0]);
     56     ret = 1;
     57   }
     58 
     59   return ret;
     60 }