| 1 | /* Copyright (C) 1991, 1992, 1996, 1998, 2001, 2006, 2013, |
| 2 | 2014 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is derived from mkstemps.c from the GNU Libiberty Library |
| 5 | which in turn is derived from the GNU C Library. |
| 6 | |
| 7 | The GNU C Library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Library General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | The GNU C Library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Library General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Library General Public |
| 18 | License along with the GNU C Library; see the file COPYING.LIB. If not, |
| 19 | write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "libguile/__scm.h" |
| 28 | |
| 29 | #ifdef HAVE_STDLIB_H |
| 30 | #include <stdlib.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_STRING_H |
| 33 | #include <string.h> |
| 34 | #endif |
| 35 | #include <errno.h> |
| 36 | #include <stdio.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <unistd.h> |
| 39 | #ifdef HAVE_SYS_TIME_H |
| 40 | #include <sys/time.h> |
| 41 | #endif |
| 42 | #ifdef __MINGW32__ |
| 43 | #include <process.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifndef TMP_MAX |
| 47 | #define TMP_MAX 16384 |
| 48 | #endif |
| 49 | |
| 50 | /* We provide this prototype to avoid compiler warnings. If this ever |
| 51 | conflicts with a declaration in a system header file, we'll find |
| 52 | out, because we should include that header file here. */ |
| 53 | int mkstemp (char *); |
| 54 | |
| 55 | /* Generate a unique temporary file name from TEMPLATE. |
| 56 | |
| 57 | TEMPLATE has the form: |
| 58 | |
| 59 | <path>/ccXXXXXX |
| 60 | |
| 61 | The last six characters of TEMPLATE must be "XXXXXX"; they are |
| 62 | replaced with a string that makes the filename unique. |
| 63 | |
| 64 | Returns a file descriptor open on the file for reading and writing. */ |
| 65 | int |
| 66 | mkstemp (template) |
| 67 | char *template; |
| 68 | { |
| 69 | static const char letters[] |
| 70 | = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 71 | static scm_t_uint64 value; |
| 72 | #ifdef HAVE_GETTIMEOFDAY |
| 73 | struct timeval tv; |
| 74 | #endif |
| 75 | char *XXXXXX; |
| 76 | size_t len; |
| 77 | int count; |
| 78 | |
| 79 | len = strlen (template); |
| 80 | |
| 81 | if ((int) len < 6 |
| 82 | || strncmp (&template[len - 6], "XXXXXX", 6)) |
| 83 | { |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | XXXXXX = &template[len - 6]; |
| 88 | |
| 89 | #ifdef HAVE_GETTIMEOFDAY |
| 90 | /* Get some more or less random data. */ |
| 91 | gettimeofday (&tv, NULL); |
| 92 | value += ((scm_t_uint64) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); |
| 93 | #else |
| 94 | value += getpid (); |
| 95 | #endif |
| 96 | |
| 97 | for (count = 0; count < TMP_MAX; ++count) |
| 98 | { |
| 99 | scm_t_uint64 v = value; |
| 100 | int fd; |
| 101 | |
| 102 | /* Fill in the random bits. */ |
| 103 | XXXXXX[0] = letters[v % 62]; |
| 104 | v /= 62; |
| 105 | XXXXXX[1] = letters[v % 62]; |
| 106 | v /= 62; |
| 107 | XXXXXX[2] = letters[v % 62]; |
| 108 | v /= 62; |
| 109 | XXXXXX[3] = letters[v % 62]; |
| 110 | v /= 62; |
| 111 | XXXXXX[4] = letters[v % 62]; |
| 112 | v /= 62; |
| 113 | XXXXXX[5] = letters[v % 62]; |
| 114 | |
| 115 | fd = open (template, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600); |
| 116 | if (fd >= 0) |
| 117 | /* The file does not exist. */ |
| 118 | return fd; |
| 119 | |
| 120 | /* This is a random value. It is only necessary that the next |
| 121 | TMP_MAX values generated by adding 7777 to VALUE are different |
| 122 | with (module 2^32). */ |
| 123 | value += 7777; |
| 124 | } |
| 125 | |
| 126 | /* We return the null string if we can't find a unique file name. */ |
| 127 | template[0] = '\0'; |
| 128 | return -1; |
| 129 | } |