aboutsummaryrefslogtreecommitdiff
path: root/lib/open_tmpfd.c
blob: e94bf327c4a59f166afff589aa1cff9b0c729864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "../ninitfeatures.h"

int open_tmpfd(char *target, char *tmp, int mode) /*EXTRACT_INCL*/ {
  unsigned long len;
  char *x;
  int fd;
  for (len=0 ;; len++) {
    x = tmp + str_copy(tmp, target);
    if (len) x += fmt_ulong(x,len);
    x[0] = '~'; x[1] = 0;
    fd = open(tmp, O_WRONLY|O_CREAT|O_EXCL, mode);
    if (fd >=0 || errno != EEXIST) break;
  }
  return fd;
}