]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libiberty/mkstemps.c
merge from gcc
[thirdparty/binutils-gdb.git] / libiberty / mkstemps.c
CommitLineData
25319993 1/* Copyright (C) 1991, 1992, 1996, 1998, 2004 Free Software Foundation, Inc.
252b5132
RH
2 This file is derived from mkstemp.c from the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
979c05d3
NC
16 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
17 Boston, MA 02110-1301, USA. */
252b5132
RH
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
b1233257 23#include <sys/types.h>
252b5132
RH
24#ifdef HAVE_STDLIB_H
25#include <stdlib.h>
26#endif
27#ifdef HAVE_STRING_H
28#include <string.h>
29#endif
30#include <errno.h>
31#include <stdio.h>
32#include <fcntl.h>
33#ifdef HAVE_UNISTD_H
34#include <unistd.h>
35#endif
36#ifdef HAVE_SYS_TIME_H
37#include <sys/time.h>
38#endif
39#include "ansidecl.h"
40
41/* We need to provide a type for gcc_uint64_t. */
42#ifdef __GNUC__
eb383413 43__extension__ typedef unsigned long long gcc_uint64_t;
252b5132
RH
44#else
45typedef unsigned long gcc_uint64_t;
46#endif
47
48#ifndef TMP_MAX
49#define TMP_MAX 16384
50#endif
51
069c63e2
DD
52#ifndef O_BINARY
53# define O_BINARY 0
54#endif
55
ba19b94f 56/*
252b5132 57
abf6a75b 58@deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
252b5132 59
abf6a75b
DD
60Generate a unique temporary file name from @var{pattern}.
61@var{pattern} has the form:
ba19b94f
DD
62
63@example
5d852400 64 @var{path}/ccXXXXXX@var{suffix}
ba19b94f
DD
65@end example
66
5d852400 67@var{suffix_len} tells us how long @var{suffix} is (it can be zero
abf6a75b 68length). The last six characters of @var{pattern} before @var{suffix}
5d852400 69must be @samp{XXXXXX}; they are replaced with a string that makes the
ba19b94f
DD
70filename unique. Returns a file descriptor open on the file for
71reading and writing.
252b5132 72
ba19b94f 73@end deftypefn
252b5132 74
ba19b94f 75*/
252b5132 76
252b5132 77int
abf6a75b 78mkstemps (char *pattern, int suffix_len)
252b5132
RH
79{
80 static const char letters[]
81 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
82 static gcc_uint64_t value;
83#ifdef HAVE_GETTIMEOFDAY
84 struct timeval tv;
85#endif
86 char *XXXXXX;
87 size_t len;
88 int count;
89
abf6a75b 90 len = strlen (pattern);
252b5132
RH
91
92 if ((int) len < 6 + suffix_len
abf6a75b 93 || strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
252b5132
RH
94 {
95 return -1;
96 }
97
abf6a75b 98 XXXXXX = &pattern[len - 6 - suffix_len];
252b5132
RH
99
100#ifdef HAVE_GETTIMEOFDAY
101 /* Get some more or less random data. */
102 gettimeofday (&tv, NULL);
103 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
104#else
105 value += getpid ();
106#endif
107
108 for (count = 0; count < TMP_MAX; ++count)
109 {
110 gcc_uint64_t v = value;
111 int fd;
112
113 /* Fill in the random bits. */
114 XXXXXX[0] = letters[v % 62];
115 v /= 62;
116 XXXXXX[1] = letters[v % 62];
117 v /= 62;
118 XXXXXX[2] = letters[v % 62];
119 v /= 62;
120 XXXXXX[3] = letters[v % 62];
121 v /= 62;
122 XXXXXX[4] = letters[v % 62];
123 v /= 62;
124 XXXXXX[5] = letters[v % 62];
125
069c63e2 126 fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
252b5132
RH
127 if (fd >= 0)
128 /* The file does not exist. */
129 return fd;
f562800d
DD
130 if (errno != EEXIST)
131 /* Fatal error (EPERM, ENOSPC etc). Doesn't make sense to loop. */
132 break;
252b5132
RH
133
134 /* This is a random value. It is only necessary that the next
135 TMP_MAX values generated by adding 7777 to VALUE are different
136 with (module 2^32). */
137 value += 7777;
138 }
139
140 /* We return the null string if we can't find a unique file name. */
abf6a75b 141 pattern[0] = '\0';
252b5132
RH
142 return -1;
143}