sbase

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

sponge.c (712B)


      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 	argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
     21 
     22 	if (argc != 1)
     23 		usage();
     24 
     25 	if ((tmpfd = mkstemp(tmp)) < 0)
     26 		eprintf("mkstemp:");
     27 	unlink(tmp);
     28 	if (concat(0, "<stdin>", tmpfd, "<tmpfile>") < 0)
     29 		return 1;
     30 	if (lseek(tmpfd, 0, SEEK_SET) < 0)
     31 		eprintf("lseek:");
     32 
     33 	if ((fd = creat(argv[0], 0666)) < 0)
     34 		eprintf("creat %s:", argv[0]);
     35 	if (concat(tmpfd, "<tmpfile>", fd, argv[0]) < 0)
     36 		return 1;
     37 
     38 	return 0;
     39 }