Back to index

cell-binutils  2.17cvs20070401
mkstemps.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1996, 1998, 2004 Free Software Foundation, Inc.
00002    This file is derived from mkstemp.c from the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public License as
00006    published by the Free Software Foundation; either version 2 of the
00007    License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00016    write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00017    Boston, MA 02110-1301, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include "config.h"
00021 #endif
00022 
00023 #include <sys/types.h>
00024 #ifdef HAVE_STDLIB_H
00025 #include <stdlib.h>
00026 #endif
00027 #ifdef HAVE_STRING_H
00028 #include <string.h>
00029 #endif
00030 #include <errno.h>
00031 #include <stdio.h>
00032 #include <fcntl.h>
00033 #ifdef HAVE_UNISTD_H
00034 #include <unistd.h>
00035 #endif
00036 #ifdef HAVE_SYS_TIME_H
00037 #include <sys/time.h>
00038 #endif
00039 #include "ansidecl.h"
00040 
00041 /* We need to provide a type for gcc_uint64_t.  */
00042 #ifdef __GNUC__
00043 __extension__ typedef unsigned long long gcc_uint64_t;
00044 #else
00045 typedef unsigned long gcc_uint64_t;
00046 #endif
00047 
00048 #ifndef TMP_MAX
00049 #define TMP_MAX 16384
00050 #endif
00051 
00052 #ifndef O_BINARY
00053 # define O_BINARY 0
00054 #endif
00055 
00056 /*
00057 
00058 @deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
00059 
00060 Generate a unique temporary file name from @var{pattern}.
00061 @var{pattern} has the form:
00062 
00063 @example
00064    @var{path}/ccXXXXXX@var{suffix}
00065 @end example
00066 
00067 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
00068 length).  The last six characters of @var{pattern} before @var{suffix}
00069 must be @samp{XXXXXX}; they are replaced with a string that makes the
00070 filename unique.  Returns a file descriptor open on the file for
00071 reading and writing.
00072 
00073 @end deftypefn
00074 
00075 */
00076 
00077 int
00078 mkstemps (char *pattern, int suffix_len)
00079 {
00080   static const char letters[]
00081     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00082   static gcc_uint64_t value;
00083 #ifdef HAVE_GETTIMEOFDAY
00084   struct timeval tv;
00085 #endif
00086   char *XXXXXX;
00087   size_t len;
00088   int count;
00089 
00090   len = strlen (pattern);
00091 
00092   if ((int) len < 6 + suffix_len
00093       || strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
00094     {
00095       return -1;
00096     }
00097 
00098   XXXXXX = &pattern[len - 6 - suffix_len];
00099 
00100 #ifdef HAVE_GETTIMEOFDAY
00101   /* Get some more or less random data.  */
00102   gettimeofday (&tv, NULL);
00103   value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
00104 #else
00105   value += getpid ();
00106 #endif
00107 
00108   for (count = 0; count < TMP_MAX; ++count)
00109     {
00110       gcc_uint64_t v = value;
00111       int fd;
00112 
00113       /* Fill in the random bits.  */
00114       XXXXXX[0] = letters[v % 62];
00115       v /= 62;
00116       XXXXXX[1] = letters[v % 62];
00117       v /= 62;
00118       XXXXXX[2] = letters[v % 62];
00119       v /= 62;
00120       XXXXXX[3] = letters[v % 62];
00121       v /= 62;
00122       XXXXXX[4] = letters[v % 62];
00123       v /= 62;
00124       XXXXXX[5] = letters[v % 62];
00125 
00126       fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
00127       if (fd >= 0)
00128        /* The file does not exist.  */
00129        return fd;
00130 
00131       /* This is a random value.  It is only necessary that the next
00132         TMP_MAX values generated by adding 7777 to VALUE are different
00133         with (module 2^32).  */
00134       value += 7777;
00135     }
00136 
00137   /* We return the null string if we can't find a unique file name.  */
00138   pattern[0] = '\0';
00139   return -1;
00140 }