skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

openwritevnclose5.c (713B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 
      7 #include <skalibs/posixplz.h>
      8 #include <skalibs/djbunix.h>
      9 
     10 #define SUFFIX ":skalibs-openwritevnclose:XXXXXX"
     11 
     12 int openwritevnclose5 (char const *fn, struct iovec const *v, unsigned int vlen, devino *devino, unsigned int options)
     13 {
     14   int fd ;
     15   size_t fnlen = strlen(fn) ;
     16   char tmp[fnlen + sizeof(SUFFIX)] ;
     17   memcpy(tmp, fn, fnlen) ;
     18   memcpy(tmp + fnlen, SUFFIX, sizeof(SUFFIX)) ;
     19   fd = mkstemp(tmp) ;
     20   if (fd < 0) return 0 ;
     21   if (!writevnclose_unsafe5(fd, v, vlen, devino, options)) goto failclose ;
     22   if (rename(tmp, fn) < 0) goto fail ;
     23   return 1 ;
     24 
     25  failclose:
     26   fd_close(fd) ;
     27  fail:
     28   unlink_void(tmp) ;
     29   return 0 ;
     30 }