openwritenclose5.c (718B)
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/devino.h> 9 #include <skalibs/djbunix.h> 10 11 #define SUFFIX ":skalibs-openwritenclose:XXXXXX" 12 13 int openwritenclose5 (char const *fn, char const *s, size_t n, devino *devino, unsigned int options) 14 { 15 int fd ; 16 size_t fnlen = strlen(fn) ; 17 char tmp[fnlen + sizeof(SUFFIX)] ; 18 memcpy(tmp, fn, fnlen) ; 19 memcpy(tmp + fnlen, SUFFIX, sizeof(SUFFIX)) ; 20 fd = mkstemp(tmp) ; 21 if (fd < 0) return 0 ; 22 if (!writenclose_unsafe5(fd, s, n, devino, options)) goto failclose ; 23 if (rename(tmp, fn) < 0) goto fail ; 24 return 1 ; 25 26 failclose: 27 fd_close(fd) ; 28 fail: 29 unlink_void(tmp) ; 30 return 0 ; 31 }