mkstemps.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 1991-2022 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. #elif HAVE_TIME_H
  34. #include <time.h>
  35. #endif
  36. #include "ansidecl.h"
  37. /* We need to provide a type for gcc_uint64_t. */
  38. #ifdef __GNUC__
  39. __extension__ typedef unsigned long long gcc_uint64_t;
  40. #else
  41. typedef unsigned long gcc_uint64_t;
  42. #endif
  43. #ifndef TMP_MAX
  44. #define TMP_MAX 16384
  45. #endif
  46. #ifndef O_BINARY
  47. # define O_BINARY 0
  48. #endif
  49. /*
  50. @deftypefn Replacement int mkstemps (char *@var{pattern}, int @var{suffix_len})
  51. Generate a unique temporary file name from @var{pattern}.
  52. @var{pattern} has the form:
  53. @example
  54. @var{path}/ccXXXXXX@var{suffix}
  55. @end example
  56. @var{suffix_len} tells us how long @var{suffix} is (it can be zero
  57. length). The last six characters of @var{pattern} before @var{suffix}
  58. must be @samp{XXXXXX}; they are replaced with a string that makes the
  59. filename unique. Returns a file descriptor open on the file for
  60. reading and writing.
  61. @end deftypefn
  62. */
  63. int
  64. mkstemps (char *pattern, int suffix_len)
  65. {
  66. static const char letters[]
  67. = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  68. static gcc_uint64_t value;
  69. #ifdef HAVE_GETTIMEOFDAY
  70. struct timeval tv;
  71. #endif
  72. char *XXXXXX;
  73. size_t len;
  74. int count;
  75. len = strlen (pattern);
  76. if ((int) len < 6 + suffix_len
  77. || strncmp (&pattern[len - 6 - suffix_len], "XXXXXX", 6))
  78. {
  79. return -1;
  80. }
  81. XXXXXX = &pattern[len - 6 - suffix_len];
  82. #ifdef HAVE_GETTIMEOFDAY
  83. /* Get some more or less random data. */
  84. gettimeofday (&tv, NULL);
  85. value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
  86. #else
  87. value += getpid ();
  88. #endif
  89. for (count = 0; count < TMP_MAX; ++count)
  90. {
  91. gcc_uint64_t v = value;
  92. int fd;
  93. /* Fill in the random bits. */
  94. XXXXXX[0] = letters[v % 62];
  95. v /= 62;
  96. XXXXXX[1] = letters[v % 62];
  97. v /= 62;
  98. XXXXXX[2] = letters[v % 62];
  99. v /= 62;
  100. XXXXXX[3] = letters[v % 62];
  101. v /= 62;
  102. XXXXXX[4] = letters[v % 62];
  103. v /= 62;
  104. XXXXXX[5] = letters[v % 62];
  105. fd = open (pattern, O_BINARY|O_RDWR|O_CREAT|O_EXCL, 0600);
  106. if (fd >= 0)
  107. /* The file does not exist. */
  108. return fd;
  109. if (errno != EEXIST
  110. #ifdef EISDIR
  111. && errno != EISDIR
  112. #endif
  113. )
  114. /* Fatal error (EPERM, ENOSPC etc). Doesn't make sense to loop. */
  115. break;
  116. /* This is a random value. It is only necessary that the next
  117. TMP_MAX values generated by adding 7777 to VALUE are different
  118. with (module 2^32). */
  119. value += 7777;
  120. }
  121. /* We return the null string if we can't find a unique file name. */
  122. pattern[0] = '\0';
  123. return -1;
  124. }