2004-04-29 17:28:42 +02:00
|
|
|
/* Copyright (C) 1991, 1992, 1996, 1998, 2004 Free Software Foundation, Inc.
|
1998-09-05 13:09:09 +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,
|
|
|
|
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
Boston, MA 02111-1307, USA. */
|
|
|
|
|
1998-09-07 22:37:13 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2000-07-23 17:57:38 +02:00
|
|
|
#include <sys/types.h>
|
1998-09-07 22:37:13 +02:00
|
|
|
#ifdef HAVE_STDLIB_H
|
1998-09-05 13:09:09 +02:00
|
|
|
#include <stdlib.h>
|
1998-09-07 22:37:13 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
1998-09-05 13:09:09 +02:00
|
|
|
#include <string.h>
|
1998-09-07 22:37:13 +02:00
|
|
|
#endif
|
1998-09-05 13:09:09 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
1998-09-07 22:37:13 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1998-09-05 13:09:09 +02:00
|
|
|
#include <unistd.h>
|
1998-09-07 22:37:13 +02:00
|
|
|
#endif
|
1998-10-14 01:08:18 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
1998-09-05 13:09:09 +02:00
|
|
|
#include <sys/time.h>
|
1998-10-14 01:08:18 +02:00
|
|
|
#endif
|
1998-09-07 22:37:13 +02:00
|
|
|
#include "ansidecl.h"
|
1998-09-05 13:09:09 +02:00
|
|
|
|
|
|
|
/* We need to provide a type for gcc_uint64_t. */
|
|
|
|
#ifdef __GNUC__
|
2000-06-07 23:13:14 +02:00
|
|
|
__extension__ typedef unsigned long long gcc_uint64_t;
|
1998-09-05 13:09:09 +02:00
|
|
|
#else
|
|
|
|
typedef unsigned long gcc_uint64_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TMP_MAX
|
|
|
|
#define TMP_MAX 16384
|
|
|
|
#endif
|
|
|
|
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
/*
|
1998-09-05 13:09:09 +02:00
|
|
|
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
@deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
|
1998-09-05 13:09:09 +02:00
|
|
|
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
Generate a unique temporary file name from @var{template}.
|
|
|
|
@var{template} has the form:
|
|
|
|
|
|
|
|
@example
|
argv.c, [...]: Improve manual formatting.
* argv.c, asprintf.c, choose-temp.c, concat.c, cplus-dem.c,
ffs.c, fnmatch.txh, getruntime.c, make-temp-file.c,
mkstemps.c, pexecute.c, random.c, strsitnal.c, vasprintf.c:
Improve manual formatting.
* functions.texi: Regenerate.
From-SVN: r46323
2001-10-17 23:15:41 +02:00
|
|
|
@var{path}/ccXXXXXX@var{suffix}
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
@end example
|
|
|
|
|
argv.c, [...]: Improve manual formatting.
* argv.c, asprintf.c, choose-temp.c, concat.c, cplus-dem.c,
ffs.c, fnmatch.txh, getruntime.c, make-temp-file.c,
mkstemps.c, pexecute.c, random.c, strsitnal.c, vasprintf.c:
Improve manual formatting.
* functions.texi: Regenerate.
From-SVN: r46323
2001-10-17 23:15:41 +02:00
|
|
|
@var{suffix_len} tells us how long @var{suffix} is (it can be zero
|
|
|
|
length). The last six characters of @var{template} before @var{suffix}
|
|
|
|
must be @samp{XXXXXX}; they are replaced with a string that makes the
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
filename unique. Returns a file descriptor open on the file for
|
|
|
|
reading and writing.
|
1998-09-05 13:09:09 +02:00
|
|
|
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
@end deftypefn
|
1998-09-05 13:09:09 +02:00
|
|
|
|
Makefile.in (TEXIFILES): Add fnmatch.txh.
* Makefile.in (TEXIFILES): Add fnmatch.txh.
(maint-undoc): New.
maint-tool: Add "undoc" tool.
* alloca.c, argv.c, asprintf.c, choose-temp.c, concat.c,
fdmatch.c, ffs.c, getruntime.c, insque.c, lbasename.c,
make-temp-file.c, mkstemps.c, pexecute.c, random.c, spaces.c,
strerror.s, strsignal.c, strtol.c, vasprintf.c: Add or update
documentation.
* fnmatch.txh: New.
* functions.texi: Regenerate.
From-SVN: r46274
2001-10-16 04:50:13 +02:00
|
|
|
*/
|
1998-09-05 13:09:09 +02:00
|
|
|
|
|
|
|
int
|
partition.h: Remove use of PARAMS.
include/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
* partition.h: Remove use of PARAMS.
* obstack.h: Remove conditional prototypes __STDC__.
* objalloc.h: Remove use of PARAMS.
* splay-tree.h: Likewise.
libiberty/
2005-03-27 Gabriel Dos Reis <gdr@integrable-solutions.net>
Convert libiberty to use ISO C prototype style 5/n.
* random.c (srandom, initstate, setstate, random): Use ISO C
prototypes.
* putenv.c (putenv): Likewise.
* physmem.c (physmem_available, physmem_total, main):
Likewise.
* pex-win32.c (fix_argv, pexecute, pwait): Likewise.
* pex-unix.c (pexecute, pwait): Likewise.
* pex-msdos.c (pexecute, pwait): Likewise.
* pex-djgpp.c (pexecute, pwait): Likewise.
* partition.c (partition_new, partition_delete,
partition_union)
(elem_compare, partition_print): Likewise.
* obstack.c (_obstack_begin, _obstack_begin_1,
_obstack_newchunk,
_obstack_allocated_p, _obstack_free, obstack_free,
_obstack_memory_used, print_and_abort, obstack_next_free,
obstack_object_size, obstack_base): Likewise. Remove codes
predicated on !defined(__STDC__).
* objalloc.c (objalloc_create, _objalloc_alloc, objalloc_free,
objalloc_free_block): Use ISO C prototypes.
* mkstemps.c (mkstemps): Likewise.
* memset.c (memset): Likewise.
* mempcpy.c (mempcpy): Likewise.
* rename.c (rename): Likewise.
* rindex.c (rindex): Likewise.
* setenv.c (setenv, unsetenv): Likewise.
* sigsetmask.c (sigsetmask): Likewise.
* snprintf.c (snprintf): Likewise.
* sort.c (sort_pointers, xmalloc): Likewise.
* spaces.c (spaces): Likewise.
* splay-tree.c (splay_tree_delete_helper,
splay_tree_splay_helper, splay_tree_splay,
splay_tree_foreach_helper, splay_tree_xmalloc_allocate,
splay_tree_new, splay_tree_xmalloc_allocate,
splay_tree_new_with_allocator, splay_tree_delete,
splay_tree_insert, splay_tree_remove, splay_tree_lookup,
splay_tree_max, splay_tree_min, splay_tree_predecessor,
splay_tree_successor, splay_tree_foreach,
splay_tree_compare_ints, splay_tree_compare_pointers):
Likewise.
* stpcpy.c (stpcpy): Likewise.
* stpncpy.c (stpncpy): Likewise.
* strcasecmp.c (strcasecmp): Likewise.
* strchr.c (strchr): Likewise.
* strdup.c (strdup): Likewise.
From-SVN: r97125
2005-03-28 06:22:33 +02:00
|
|
|
mkstemps (char *template, int suffix_len)
|
1998-09-05 13:09:09 +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;
|
|
|
|
|
|
|
|
len = strlen (template);
|
|
|
|
|
1998-10-15 17:55:04 +02:00
|
|
|
if ((int) len < 6 + suffix_len
|
1998-09-05 13:09:09 +02:00
|
|
|
|| strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
XXXXXX = &template[len - 6 - suffix_len];
|
|
|
|
|
|
|
|
#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];
|
|
|
|
|
|
|
|
fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
|
|
|
|
if (fd >= 0)
|
|
|
|
/* The file does not exist. */
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
template[0] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|