binutils-gdb/libiberty/mkstemps.c

148 lines
3.7 KiB
C
Raw Normal View History

2004-04-30 00:08:50 +08:00
/* Copyright (C) 1991, 1992, 1996, 1998, 2004 Free Software Foundation, Inc.
1999-05-03 15:29:11 +08:00
This file is derived from mkstemp.c from the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
1999-05-03 15:29:11 +08:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
1999-05-03 15:29:11 +08:00
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "ansidecl.h"
/* We need to provide a type for gcc_uint64_t. */
#ifdef __GNUC__
__extension__ typedef unsigned long long gcc_uint64_t;
1999-05-03 15:29:11 +08:00
#else
typedef unsigned long gcc_uint64_t;
#endif
#ifndef TMP_MAX
#define TMP_MAX 16384
#endif
2006-05-29 01:49:15 +08:00
#ifndef O_BINARY
# define O_BINARY 0
#endif
2001-10-16 10:55:31 +08:00
/*
1999-05-03 15:29:11 +08:00
2005-05-25 05:01:33 +08:00
@deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
1999-05-03 15:29:11 +08:00
2005-05-25 05:01:33 +08:00
Generate a unique temporary file name from @var{pattern}.
@var{pattern} has the form:
2001-10-16 10:55:31 +08:00
@example
2001-10-18 06:35:28 +08:00
@var{path}/ccXXXXXX@var{suffix}
2001-10-16 10:55:31 +08:00
@end example
2001-10-18 06:35:28 +08:00
@var{suffix_len} tells us how long @var{suffix} is (it can be zero
2005-05-25 05:01:33 +08:00
length). The last six characters of @var{pattern} before @var{suffix}
2001-10-18 06:35:28 +08:00
must be @samp{XXXXXX}; they are replaced with a string that makes the
2001-10-16 10:55:31 +08:00
filename unique. Returns a file descriptor open on the file for
reading and writing.
1999-05-03 15:29:11 +08:00
2001-10-16 10:55:31 +08:00
@end deftypefn
1999-05-03 15:29:11 +08:00
2001-10-16 10:55:31 +08:00
*/
1999-05-03 15:29:11 +08:00
int
2005-05-25 05:01:33 +08:00
mkstemps (char *pattern, int suffix_len)
1999-05-03 15:29:11 +08:00
{
static const char letters[]
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static gcc_uint64_t value;
#ifdef HAVE_GETTIMEOFDAY
struct timeval tv;
#endif
char *XXXXXX;
size_t len;
int count;
2005-05-25 05:01:33 +08:00
len = strlen (pattern);
1999-05-03 15:29:11 +08:00
if ((int) len < 6 + suffix_len
2005-05-25 05:01:33 +08:00
|| strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
1999-05-03 15:29:11 +08:00
{
return -1;
}
2005-05-25 05:01:33 +08:00
XXXXXX = &pattern[len - 6 - suffix_len];
1999-05-03 15:29:11 +08:00
#ifdef HAVE_GETTIMEOFDAY
/* Get some more or less random data. */
gettimeofday (&tv, NULL);
value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
#else
value += getpid ();
#endif
for (count = 0; count < TMP_MAX; ++count)
{
gcc_uint64_t v = value;
int fd;
/* Fill in the random bits. */
XXXXXX[0] = letters[v % 62];
v /= 62;
XXXXXX[1] = letters[v % 62];
v /= 62;
XXXXXX[2] = letters[v % 62];
v /= 62;
XXXXXX[3] = letters[v % 62];
v /= 62;
XXXXXX[4] = letters[v % 62];
v /= 62;
XXXXXX[5] = letters[v % 62];
2006-05-29 01:49:15 +08:00
fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
1999-05-03 15:29:11 +08:00
if (fd >= 0)
/* The file does not exist. */
return fd;
2008-08-01 06:01:30 +08:00
if (errno != EEXIST
#ifdef EISDIR
&& errno != EISDIR
#endif
)
2008-08-01 03:06:35 +08:00
/* Fatal error (EPERM, ENOSPC etc). Doesn't make sense to loop. */
break;
1999-05-03 15:29:11 +08:00
/* This is a random value. It is only necessary that the next
TMP_MAX values generated by adding 7777 to VALUE are different
with (module 2^32). */
value += 7777;
}
/* We return the null string if we can't find a unique file name. */
2005-05-25 05:01:33 +08:00
pattern[0] = '\0';
1999-05-03 15:29:11 +08:00
return -1;
}