mkstemp.c 3.3 KB

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