tempname.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
  2. This file is part of 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., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. /* March 11, 2002 Manuel Novoa III
  16. *
  17. * Modify code to remove dependency on libgcc long long arith support funcs.
  18. */
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <sys/time.h>
  30. /* Return nonzero if DIR is an existent directory. */
  31. static int direxists (const char *dir)
  32. {
  33. struct stat buf;
  34. return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  35. }
  36. /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
  37. non-null and exists, uses it; otherwise uses the first of $TMPDIR,
  38. P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
  39. for use with mk[s]temp. Will fail (-1) if DIR is non-null and
  40. doesn't exist, none of the searched dirs exists, or there's not
  41. enough space in TMPL. */
  42. int __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
  43. int try_tmpdir)
  44. {
  45. //const char *d;
  46. size_t dlen, plen;
  47. if (!pfx || !pfx[0])
  48. {
  49. pfx = "file";
  50. plen = 4;
  51. }
  52. else
  53. {
  54. plen = strlen (pfx);
  55. if (plen > 5)
  56. plen = 5;
  57. }
  58. #if 0
  59. if (try_tmpdir)
  60. {
  61. d = __secure_getenv ("TMPDIR");
  62. if (d != NULL && direxists (d))
  63. dir = d;
  64. else if (dir != NULL && direxists (dir))
  65. /* nothing */ ;
  66. else
  67. dir = NULL;
  68. }
  69. #endif
  70. if (dir == NULL)
  71. {
  72. if (direxists (P_tmpdir))
  73. dir = P_tmpdir;
  74. else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
  75. dir = "/tmp";
  76. else
  77. {
  78. __set_errno (ENOENT);
  79. return -1;
  80. }
  81. }
  82. dlen = strlen (dir);
  83. while (dlen > 1 && dir[dlen - 1] == '/')
  84. dlen--; /* remove trailing slashes */
  85. /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  86. if (tmpl_len < dlen + 1 + plen + 6 + 1)
  87. {
  88. __set_errno (EINVAL);
  89. return -1;
  90. }
  91. sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
  92. return 0;
  93. }
  94. /* These are the characters used in temporary filenames. */
  95. static const char letters[] =
  96. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  97. /* Generate a temporary file name based on TMPL. TMPL must match the
  98. rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
  99. does not exist at the time of the call to __gen_tempname. TMPL is
  100. overwritten with the result. If OPENIT is nonzero, creates the
  101. file and returns a read-write fd; the file is mode 0600 modulo
  102. umask. If LARGEFILE is nonzero, uses open64() instead of open().
  103. We use a clever algorithm to get hard-to-predict names. */
  104. int __gen_tempname (char *tmpl, int openit)
  105. {
  106. int len;
  107. char *XXXXXX;
  108. static uint64_t value;
  109. struct timeval tv;
  110. uint32_t high, low, rh;
  111. unsigned int k;
  112. int count, fd;
  113. int save_errno = errno;
  114. int i;
  115. len = strlen (tmpl);
  116. if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
  117. {
  118. __set_errno (EINVAL);
  119. return -1;
  120. }
  121. /* This is where the Xs start. */
  122. XXXXXX = &tmpl[len - 6];
  123. /* Get some more or less random data. */
  124. gettimeofday (&tv, NULL);
  125. value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
  126. for (count = 0; count < TMP_MAX; value += 7777, ++count)
  127. {
  128. low = value & UINT32_MAX;
  129. high = value >> 32;
  130. for (i = 0 ; i < 6 ; i++) {
  131. rh = high % 62;
  132. high /= 62;
  133. #define L ((UINT32_MAX % 62 + 1) % 62)
  134. k = (low % 62) + (L * rh);
  135. #undef L
  136. #define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62))
  137. low = (low / 62) + (H * rh) + (k / 62);
  138. #undef H
  139. k %= 62;
  140. XXXXXX[i] = letters[k];
  141. }
  142. if (openit)
  143. {
  144. fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, 0600);
  145. if (fd >= 0)
  146. {
  147. __set_errno (save_errno);
  148. return fd;
  149. }
  150. else if (errno != EEXIST)
  151. /* Any other error will apply also to other names we might
  152. try, and there are 2^32 or so of them, so give up now. */
  153. return -1;
  154. }
  155. else
  156. {
  157. struct stat st;
  158. if (stat (tmpl, &st) < 0)
  159. {
  160. if (errno == ENOENT)
  161. {
  162. __set_errno (save_errno);
  163. return 0;
  164. }
  165. else
  166. /* Give up now. */
  167. return -1;
  168. }
  169. }
  170. }
  171. /* We got out of the loop because we ran out of combinations to try. */
  172. __set_errno (EEXIST);
  173. return -1;
  174. }