2017-01-04 14:17:07 +01:00
|
|
|
/* Copyright (C) 1991-2017 Free Software Foundation, Inc.
|
1999-05-03 09:29:11 +02: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,
|
2005-05-10 17:33:34 +02:00
|
|
|
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA. */
|
1999-05-03 09:29:11 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2000-07-23 17:58:11 +02:00
|
|
|
#include <sys/types.h>
|
1999-05-03 09:29:11 +02: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>
|
2014-09-16 20:32:09 +02:00
|
|
|
#elif HAVE_TIME_H
|
|
|
|
#include <time.h>
|
1999-05-03 09:29:11 +02:00
|
|
|
#endif
|
|
|
|
#include "ansidecl.h"
|
|
|
|
|
|
|
|
/* We need to provide a type for gcc_uint64_t. */
|
|
|
|
#ifdef __GNUC__
|
2000-06-15 22:56:25 +02:00
|
|
|
__extension__ typedef unsigned long long gcc_uint64_t;
|
1999-05-03 09:29:11 +02:00
|
|
|
#else
|
|
|
|
typedef unsigned long gcc_uint64_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TMP_MAX
|
|
|
|
#define TMP_MAX 16384
|
|
|
|
#endif
|
|
|
|
|
2006-05-28 19:49:15 +02:00
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2001-10-16 04:55:31 +02:00
|
|
|
/*
|
1999-05-03 09:29:11 +02:00
|
|
|
|
2005-05-24 23:01:33 +02:00
|
|
|
@deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
|
1999-05-03 09:29:11 +02:00
|
|
|
|
2005-05-24 23:01:33 +02:00
|
|
|
Generate a unique temporary file name from @var{pattern}.
|
|
|
|
@var{pattern} has the form:
|
2001-10-16 04:55:31 +02:00
|
|
|
|
|
|
|
@example
|
2001-10-18 00:35:28 +02:00
|
|
|
@var{path}/ccXXXXXX@var{suffix}
|
2001-10-16 04:55:31 +02:00
|
|
|
@end example
|
|
|
|
|
2001-10-18 00:35:28 +02:00
|
|
|
@var{suffix_len} tells us how long @var{suffix} is (it can be zero
|
2005-05-24 23:01:33 +02:00
|
|
|
length). The last six characters of @var{pattern} before @var{suffix}
|
2001-10-18 00:35:28 +02:00
|
|
|
must be @samp{XXXXXX}; they are replaced with a string that makes the
|
2001-10-16 04:55:31 +02:00
|
|
|
filename unique. Returns a file descriptor open on the file for
|
|
|
|
reading and writing.
|
1999-05-03 09:29:11 +02:00
|
|
|
|
2001-10-16 04:55:31 +02:00
|
|
|
@end deftypefn
|
1999-05-03 09:29:11 +02:00
|
|
|
|
2001-10-16 04:55:31 +02:00
|
|
|
*/
|
1999-05-03 09:29:11 +02:00
|
|
|
|
|
|
|
int
|
2005-05-24 23:01:33 +02:00
|
|
|
mkstemps (char *pattern, int suffix_len)
|
1999-05-03 09:29:11 +02: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-24 23:01:33 +02:00
|
|
|
len = strlen (pattern);
|
1999-05-03 09:29:11 +02:00
|
|
|
|
|
|
|
if ((int) len < 6 + suffix_len
|
2005-05-24 23:01:33 +02:00
|
|
|
|| strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
|
1999-05-03 09:29:11 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-05-24 23:01:33 +02:00
|
|
|
XXXXXX = &pattern[len - 6 - suffix_len];
|
1999-05-03 09:29:11 +02: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-28 19:49:15 +02:00
|
|
|
fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
|
1999-05-03 09:29:11 +02:00
|
|
|
if (fd >= 0)
|
|
|
|
/* The file does not exist. */
|
|
|
|
return fd;
|
2008-08-01 00:01:30 +02:00
|
|
|
if (errno != EEXIST
|
|
|
|
#ifdef EISDIR
|
|
|
|
&& errno != EISDIR
|
|
|
|
#endif
|
|
|
|
)
|
2008-07-31 21:06:35 +02:00
|
|
|
/* Fatal error (EPERM, ENOSPC etc). Doesn't make sense to loop. */
|
|
|
|
break;
|
1999-05-03 09:29:11 +02: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-24 23:01:33 +02:00
|
|
|
pattern[0] = '\0';
|
1999-05-03 09:29:11 +02:00
|
|
|
return -1;
|
|
|
|
}
|