summaryrefslogtreecommitdiff
path: root/tvision/compat/mkstemp.c
blob: a8340f97fd21c6c63d950dc9d1fed4544f3fcf68 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Copyright (C) 2000-2005 Salvador E. Tropea */
/* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <cl/needs.h>

#ifdef NEEDS_MKSTEMP
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <io.h>
#include <string.h>

#ifdef TVComp_Watcom
 #include <stdlib.h>
 #define mktemp(a) _mktemp(a)
#endif

#if _MSC_VER >= 1400
 #define mktemp(a) _mktemp(a)
 #define open(a,b,c) _open(a,b,c)
#endif

int mkstemp (char *_template)
{
  char tmp_name[FILENAME_MAX];
  int  fd = -1;

  /* Make sure we create a non-exisiting file, even
     if race conditions exist with other processes.  */
  do {
    strcpy(tmp_name, _template);
    errno = 0;
  } while (mktemp (tmp_name) != NULL
       /* SAA: changed file mode from 0 to 0666 because on WinNT it creted files
        * with read-only attribute set. Fix me if it causes problems on other
        * platforms.
        */
	   && (fd = open(tmp_name, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0666)) == -1
	   && errno == EEXIST);

  if (fd == -1)
    errno = ENOENT;
  else
    strcpy(_template, tmp_name);

  return fd;
}
#endif