mkstemp.c 3.3 KB

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