summaryrefslogtreecommitdiffstats
path: root/src/sed/lib/mkstemp.c
blob: 5b00205c2d59ce5a9e24bb1a31e53bc41f331838 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_STRINGS_H
# include <strings.h>
#else
# include <string.h>
#endif /* HAVE_STRINGS_H */

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif /* HAVE_STDLIB_H */

#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif /* HAVE_SYS_FILE_H */

#ifdef HAVE_IO_H
# include <io.h>
#endif /* HAVE_IO_H */

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */

#include <limits.h>
#include <errno.h>

/* Generate a unique temporary file name from template.  The last six characters of
   template must be XXXXXX and these are replaced with a string that makes the
   filename unique. */

int
mkstemp (template)
     char *template;
{
  int i, j, n, fd;
  char *data = template + strlen(template) - 6;

  if (data < template) {
    errno = EINVAL;
    return -1;
  }

  for (n = 0; n <= 5; n++)
    if (data[n] != 'X') {
      errno = EINVAL;
      return -1;
    }

  for (i = 0; i < INT_MAX; i++) {
    j = i ^ 827714841;             /* Base 36 DOSSUX :-) */
    for (n = 5; n >= 0; n--) {
      data[n] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [j % 36];
      j /= 36;
    }

    fd = open (template, O_CREAT|O_EXCL|O_RDWR, 0600);
    if (fd != -1)
      return fd;
  }
    
  errno = EEXIST;
  return -1;
}