summaryrefslogtreecommitdiff
path: root/src/libstddjb/filecopy_suffix.c
blob: 70673eaa27b5c7ad4c5e10815eb87863d0f2e5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* ISC license. */

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <skalibs/bytestr.h>
#include <skalibs/djbunix.h>

int filecopy_suffix (char const *src, char const *dst, unsigned int mode, char const *suffix)
{
  unsigned int dstlen = str_len(dst) ;
  unsigned int suffixlen = str_len(suffix) ;
  char tmp[dstlen + suffixlen + 1] ;
  byte_copy(tmp, dstlen, dst) ;
  byte_copy(tmp + dstlen, suffixlen + 1, suffix) ;
  if (!filecopy_unsafe(src, tmp, mode)) return 0 ;	
  if (rename(tmp, dst) < 0)
  {
    register int e = errno ;
    unlink(tmp) ;
    errno = e ;
    return 0 ;
  }
  return 1 ;
}