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