/contrib/groff/src/libs/libgroff/tmpname.cpp

https://bitbucket.org/freebsd/freebsd-head/ · C++ · 116 lines · 65 code · 21 blank · 30 comment · 8 complexity · 04900b5ef967a21642050d7fa92d3b77 MD5 · raw file

  1. /* Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
  2. Written by Werner Lemberg (wl@gnu.org)
  3. This file is part of groff.
  4. groff is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with groff; see the file COPYING. If not, write to the Free Software
  14. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* This file is heavily based on the function __gen_tempname() in the
  16. file tempname.c which is part of the fileutils package. */
  17. #include "lib.h"
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include "posix.h"
  23. #include "nonposix.h"
  24. #ifndef TMP_MAX
  25. # define TMP_MAX 238328
  26. #endif
  27. #if HAVE_SYS_TIME_H
  28. # include <sys/time.h>
  29. #endif
  30. #ifdef HAVE_GETTIMEOFDAY
  31. #ifdef NEED_DECLARATION_GETTIMEOFDAY
  32. extern "C" {
  33. int gettimeofday(struct timeval *, void *);
  34. }
  35. #endif
  36. #endif
  37. #if HAVE_CC_INTTYPES_H
  38. # include <inttypes.h>
  39. #endif
  40. /* Use the widest available unsigned type if uint64_t is not
  41. available. The algorithm below extracts a number less than 62**6
  42. (approximately 2**35.725) from uint64_t, so ancient hosts where
  43. uintmax_t is only 32 bits lose about 3.725 bits of randomness,
  44. which is better than not having mkstemp at all. */
  45. #if !defined UINT64_MAX && !defined uint64_t
  46. # define uint64_t uintmax_t
  47. #endif
  48. /* These are the characters used in temporary filenames. */
  49. static const char letters[] =
  50. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  51. int gen_tempname(char *tmpl, int dir)
  52. {
  53. static uint64_t value;
  54. size_t len = strlen(tmpl);
  55. if (len < 6 || strcmp(&tmpl[len - 6], "XXXXXX"))
  56. return -1; /* EINVAL */
  57. /* This is where the Xs start. */
  58. char *XXXXXX = &tmpl[len - 6];
  59. /* Get some more or less random data. */
  60. #if HAVE_GETTIMEOFDAY
  61. timeval tv;
  62. gettimeofday(&tv, NULL);
  63. uint64_t random_time_bits = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec;
  64. #else
  65. uint64_t random_time_bits = time(NULL);
  66. #endif
  67. value += random_time_bits ^ getpid();
  68. for (int count = 0; count < TMP_MAX; value += 7777, ++count) {
  69. uint64_t v = value;
  70. /* Fill in the random bits. */
  71. XXXXXX[0] = letters[v % 62];
  72. v /= 62;
  73. XXXXXX[1] = letters[v % 62];
  74. v /= 62;
  75. XXXXXX[2] = letters[v % 62];
  76. v /= 62;
  77. XXXXXX[3] = letters[v % 62];
  78. v /= 62;
  79. XXXXXX[4] = letters[v % 62];
  80. v /= 62;
  81. XXXXXX[5] = letters[v % 62];
  82. int fd = dir ? mkdir(tmpl, S_IRUSR | S_IWUSR | S_IXUSR)
  83. : open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  84. if (fd >= 0)
  85. return fd;
  86. else if (errno != EEXIST)
  87. return -1;
  88. }
  89. /* We got out of the loop because we ran out of combinations to try. */
  90. return -1; /* EEXIST */
  91. }