groff: update vendor branch to v1.20.1
[dragonfly.git] / contrib / groff / src / libs / libgroff / tmpname.cpp
1 /* Copyright (C) 2001, 2003, 2004, 2009 Free Software Foundation, Inc.
2      Written by Werner Lemberg (wl@gnu.org)
3
4 This file is part of groff.
5
6 groff is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 groff is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19
20 /* This file is heavily based on the function __gen_tempname() in the
21    file tempname.c which is part of the fileutils package. */
22
23
24 #include "lib.h"
25
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <time.h>
30
31 #include "posix.h"
32 #include "nonposix.h"
33
34 #ifndef TMP_MAX
35 # define TMP_MAX 238328
36 #endif
37
38 #if HAVE_SYS_TIME_H
39 # include <sys/time.h>
40 #endif
41
42 #ifdef HAVE_GETTIMEOFDAY
43 #ifdef NEED_DECLARATION_GETTIMEOFDAY
44 extern "C" {
45   int gettimeofday(struct timeval *, void *);
46 }
47 #endif
48 #endif
49
50 #if HAVE_CC_INTTYPES_H
51 # include <inttypes.h>
52 #endif
53
54 /* Use the widest available unsigned type if uint64_t is not
55    available.  The algorithm below extracts a number less than 62**6
56    (approximately 2**35.725) from uint64_t, so ancient hosts where
57    uintmax_t is only 32 bits lose about 3.725 bits of randomness,
58    which is better than not having mkstemp at all.  */
59 #if !defined UINT64_MAX && !defined uint64_t
60 # define uint64_t uintmax_t
61 #endif
62
63 /* These are the characters used in temporary filenames.  */
64 static const char letters[] =
65 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
66
67 int gen_tempname(char *tmpl, int dir)
68 {
69   static uint64_t value;
70
71   size_t len = strlen(tmpl);
72   if (len < 6 || strcmp(&tmpl[len - 6], "XXXXXX"))
73     return -1; /* EINVAL */
74
75   /* This is where the Xs start.  */
76   char *XXXXXX = &tmpl[len - 6];
77
78   /* Get some more or less random data.  */
79 #if HAVE_GETTIMEOFDAY
80   timeval tv;
81   gettimeofday(&tv, NULL);
82   uint64_t random_time_bits = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec;
83 #else
84   uint64_t random_time_bits = time(NULL);
85 #endif
86   value += random_time_bits ^ getpid();
87
88   for (int count = 0; count < TMP_MAX; value += 7777, ++count) {
89     uint64_t v = value;
90
91     /* Fill in the random bits.  */
92     XXXXXX[0] = letters[v % 62];
93     v /= 62;
94     XXXXXX[1] = letters[v % 62];
95     v /= 62;
96     XXXXXX[2] = letters[v % 62];
97     v /= 62;
98     XXXXXX[3] = letters[v % 62];
99     v /= 62;
100     XXXXXX[4] = letters[v % 62];
101     v /= 62;
102     XXXXXX[5] = letters[v % 62];
103
104     int fd = dir ? mkdir(tmpl, S_IRUSR | S_IWUSR | S_IXUSR)
105                  : open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
106
107     if (fd >= 0)
108       return fd;
109     else if (errno != EEXIST)
110       return -1;
111   }
112
113   /* We got out of the loop because we ran out of combinations to try.  */
114   return -1; /* EEXIST */
115 }