]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/mkstemps.c
config.gcc: Update c4x and i370 for C front end-specific dependencies.
[thirdparty/gcc.git] / libiberty / mkstemps.c
CommitLineData
16ba4214
JL
1/* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc.
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,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
96e88994
KG
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
8502a100 23#include <sys/types.h>
96e88994 24#ifdef HAVE_STDLIB_H
16ba4214 25#include <stdlib.h>
96e88994
KG
26#endif
27#ifdef HAVE_STRING_H
16ba4214 28#include <string.h>
96e88994 29#endif
16ba4214
JL
30#include <errno.h>
31#include <stdio.h>
32#include <fcntl.h>
96e88994 33#ifdef HAVE_UNISTD_H
16ba4214 34#include <unistd.h>
96e88994 35#endif
ae76f1be 36#ifdef HAVE_SYS_TIME_H
16ba4214 37#include <sys/time.h>
ae76f1be 38#endif
96e88994 39#include "ansidecl.h"
16ba4214
JL
40
41/* We need to provide a type for gcc_uint64_t. */
42#ifdef __GNUC__
69197e7e 43__extension__ typedef unsigned long long gcc_uint64_t;
16ba4214
JL
44#else
45typedef unsigned long gcc_uint64_t;
46#endif
47
48#ifndef TMP_MAX
49#define TMP_MAX 16384
50#endif
51
aac04c15 52/*
16ba4214 53
aac04c15 54@deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
16ba4214 55
aac04c15
DD
56Generate a unique temporary file name from @var{template}.
57@var{template} has the form:
58
59@example
16ba4214 60 <path>/ccXXXXXX<suffix>
aac04c15
DD
61@end example
62
63@var{suffix_len} tells us how long <suffix> is (it can be zero
64length). The last six characters of @var{template} before <suffix>
65must be @code{XXXXXX}; they are replaced with a string that makes the
66filename unique. Returns a file descriptor open on the file for
67reading and writing.
16ba4214 68
aac04c15 69@end deftypefn
16ba4214 70
aac04c15 71*/
16ba4214 72
16ba4214
JL
73int
74mkstemps (template, suffix_len)
75 char *template;
76 int suffix_len;
77{
78 static const char letters[]
79 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
80 static gcc_uint64_t value;
81#ifdef HAVE_GETTIMEOFDAY
82 struct timeval tv;
83#endif
84 char *XXXXXX;
85 size_t len;
86 int count;
87
88 len = strlen (template);
89
91e0f659 90 if ((int) len < 6 + suffix_len
16ba4214
JL
91 || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
92 {
93 return -1;
94 }
95
96 XXXXXX = &template[len - 6 - suffix_len];
97
98#ifdef HAVE_GETTIMEOFDAY
99 /* Get some more or less random data. */
100 gettimeofday (&tv, NULL);
101 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
102#else
103 value += getpid ();
104#endif
105
106 for (count = 0; count < TMP_MAX; ++count)
107 {
108 gcc_uint64_t v = value;
109 int fd;
110
111 /* Fill in the random bits. */
112 XXXXXX[0] = letters[v % 62];
113 v /= 62;
114 XXXXXX[1] = letters[v % 62];
115 v /= 62;
116 XXXXXX[2] = letters[v % 62];
117 v /= 62;
118 XXXXXX[3] = letters[v % 62];
119 v /= 62;
120 XXXXXX[4] = letters[v % 62];
121 v /= 62;
122 XXXXXX[5] = letters[v % 62];
123
124 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
125 if (fd >= 0)
126 /* The file does not exist. */
127 return fd;
128
129 /* This is a random value. It is only necessary that the next
130 TMP_MAX values generated by adding 7777 to VALUE are different
131 with (module 2^32). */
132 value += 7777;
133 }
134
135 /* We return the null string if we can't find a unique file name. */
136 template[0] = '\0';
137 return -1;
138}