Merge from vendor branch BIND:
[dragonfly.git] / contrib / groff / src / libs / libgroff / tmpname.cc
1 /* Copyright (C) 2001 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 2, or (at your option) any later
9 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 along
17 with groff; see the file COPYING.  If not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20
21 /* This file is heavily based on the function __gen_tempname() in the
22    file tempname.c which is part of the fileutils package. */
23
24 /* $DragonFly: src/contrib/groff/src/libs/libgroff/Attic/tmpname.cc,v 1.2 2003/11/03 22:51:48 dillon Exp $ */
25
26 #include "lib.h"
27
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <time.h>
32
33 #include "posix.h"
34 #include "nonposix.h"
35
36 #ifndef TMP_MAX
37 # define TMP_MAX 238328
38 #endif
39
40 #if HAVE_SYS_TIME_H
41 # include <sys/time.h>
42 #endif
43
44 #ifdef HAVE_GETTIMEOFDAY
45 #ifdef NEED_DECLARATION_GETTIMEOFDAY
46 extern "C" {
47   int gettimeofday(struct timeval *, void *);
48 }
49 #endif
50 #endif
51
52 #if HAVE_STDINT_H
53 # include <stdint.h>
54 #endif
55
56 /* Use the widest available unsigned type if uint64_t is not
57    available.  The algorithm below extracts a number less than 62**6
58    (approximately 2**35.725) from uint64_t, so ancient hosts where
59    uintmax_t is only 32 bits lose about 3.725 bits of randomness,
60    which is better than not having mkstemp at all.  */
61 #if 0
62 #if !defined UINT64_MAX && !defined uint64_t
63 # define uint64_t uintmax_t
64 #endif
65 #endif
66
67 /* These are the characters used in temporary filenames.  */
68 static const char letters[] =
69 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
70
71 int gen_tempname(char *tmpl, int dir)
72 {
73   static uint64_t value;
74
75   size_t len = strlen(tmpl);
76   if (len < 6 || strcmp(&tmpl[len - 6], "XXXXXX"))
77     return -1; /* EINVAL */
78
79   /* This is where the Xs start.  */
80   char *XXXXXX = &tmpl[len - 6];
81
82   /* Get some more or less random data.  */
83 #if HAVE_GETTIMEOFDAY
84   timeval tv;
85   gettimeofday(&tv, NULL);
86   uint64_t random_time_bits = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec;
87 #else
88   uint64_t random_time_bits = time(NULL);
89 #endif
90   value += random_time_bits ^ getpid();
91
92   for (int count = 0; count < TMP_MAX; value += 7777, ++count) {
93     uint64_t v = value;
94
95     /* Fill in the random bits.  */
96     XXXXXX[0] = letters[v % 62];
97     v /= 62;
98     XXXXXX[1] = letters[v % 62];
99     v /= 62;
100     XXXXXX[2] = letters[v % 62];
101     v /= 62;
102     XXXXXX[3] = letters[v % 62];
103     v /= 62;
104     XXXXXX[4] = letters[v % 62];
105     v /= 62;
106     XXXXXX[5] = letters[v % 62];
107
108     int fd = dir ? mkdir(tmpl, S_IRUSR | S_IWUSR | S_IXUSR)
109                  : open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
110
111     if (fd >= 0)
112       return fd;
113     else if (errno != EEXIST)
114       return -1;
115   }
116
117   /* We got out of the loop because we ran out of combinations to try.  */
118   return -1; /* EEXIST */
119 }