summaryrefslogtreecommitdiff
path: root/src/libstddjb/filecopy_suffix.c
blob: 3cc5218ea42b4373d33e87c46fcd7c5cf0cb8ed9 (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 <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <skalibs/djbunix.h>

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