mkstemps.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) 1991, 1992, 1996, 1998 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 St, Fifth Floor,
  14. Boston, MA 02110-1301, USA */
  15. #include "config.h"
  16. #include "wine/port.h"
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #ifdef HAVE_SYS_TIME_H
  27. #include <sys/time.h>
  28. #endif
  29. #ifndef TMP_MAX
  30. #define TMP_MAX 16384
  31. #endif
  32. /*
  33. @deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
  34. Generate a unique temporary file name from @var{template}.
  35. @var{template} has the form:
  36. @example
  37. @var{path}/ccXXXXXX@var{suffix}
  38. @end example
  39. @var{suffix_len} tells us how long @var{suffix} is (it can be zero
  40. length). The last six characters of @var{template} before @var{suffix}
  41. must be @samp{XXXXXX}; they are replaced with a string that makes the
  42. filename unique. Returns a file descriptor open on the file for
  43. reading and writing.
  44. @end deftypefn
  45. */
  46. int
  47. mkstemps (
  48. char *template,
  49. int suffix_len)
  50. {
  51. static const char letters[]
  52. = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  53. static unsigned __int64 value;
  54. char *XXXXXX;
  55. size_t len;
  56. int count;
  57. len = strlen (template);
  58. if ((int) len < 6 + suffix_len
  59. || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
  60. {
  61. return -1;
  62. }
  63. XXXXXX = &template[len - 6 - suffix_len];
  64. #ifndef _WIN32
  65. {
  66. struct timeval tv;
  67. gettimeofday( &tv, NULL );
  68. value += ((unsigned __int64) tv.tv_usec << 16) ^ tv.tv_sec;
  69. }
  70. #endif
  71. value += getpid();
  72. for (count = 0; count < TMP_MAX; ++count)
  73. {
  74. unsigned __int64 v = value;
  75. int fd;
  76. /* Fill in the random bits. */
  77. XXXXXX[0] = letters[v % 62];
  78. v /= 62;
  79. XXXXXX[1] = letters[v % 62];
  80. v /= 62;
  81. XXXXXX[2] = letters[v % 62];
  82. v /= 62;
  83. XXXXXX[3] = letters[v % 62];
  84. v /= 62;
  85. XXXXXX[4] = letters[v % 62];
  86. v /= 62;
  87. XXXXXX[5] = letters[v % 62];
  88. fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
  89. if (fd >= 0)
  90. /* The file does not exist. */
  91. return fd;
  92. /* This is a random value. It is only necessary that the next
  93. TMP_MAX values generated by adding 7777 to VALUE are different
  94. with (module 2^32). */
  95. value += 7777;
  96. }
  97. return -1;
  98. }