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