mkstemps.c 3.7 KB

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