tempname.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <fcntl.h>
  26. #include <unistd.h>
  27. #include <assert.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/time.h>
  31. #include "tempname.h"
  32. /* Return nonzero if DIR is an existent directory. */
  33. static int direxists (const char *dir)
  34. {
  35. struct stat buf;
  36. return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  37. }
  38. /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
  39. non-null and exists, uses it; otherwise uses the first of $TMPDIR,
  40. P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
  41. for use with mk[s]temp. Will fail (-1) if DIR is non-null and
  42. doesn't exist, none of the searched dirs exists, or there's not
  43. enough space in TMPL. */
  44. int __path_search (char *tmpl, size_t tmpl_len, const char *dir,
  45. const char *pfx, int try_tmpdir)
  46. {
  47. //const char *d;
  48. size_t dlen, plen;
  49. if (!pfx || !pfx[0])
  50. {
  51. pfx = "file";
  52. plen = 4;
  53. }
  54. else
  55. {
  56. plen = strlen (pfx);
  57. if (plen > 5)
  58. plen = 5;
  59. }
  60. #if 0
  61. if (try_tmpdir)
  62. {
  63. d = __secure_getenv ("TMPDIR");
  64. if (d != NULL && direxists (d))
  65. dir = d;
  66. else if (dir != NULL && direxists (dir))
  67. /* nothing */ ;
  68. else
  69. dir = NULL;
  70. }
  71. #endif
  72. if (dir == NULL)
  73. {
  74. if (direxists (P_tmpdir))
  75. dir = P_tmpdir;
  76. else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
  77. dir = "/tmp";
  78. else
  79. {
  80. __set_errno (ENOENT);
  81. return -1;
  82. }
  83. }
  84. dlen = strlen (dir);
  85. while (dlen > 1 && dir[dlen - 1] == '/')
  86. dlen--; /* remove trailing slashes */
  87. /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  88. if (tmpl_len < dlen + 1 + plen + 6 + 1)
  89. {
  90. __set_errno (EINVAL);
  91. return -1;
  92. }
  93. sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
  94. return 0;
  95. }
  96. /* These are the characters used in temporary filenames. */
  97. static const char letters[] =
  98. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  99. /* Generate a temporary file name based on TMPL. TMPL must match the
  100. rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
  101. does not exist at the time of the call to __gen_tempname. TMPL is
  102. overwritten with the result.
  103. KIND may be one of:
  104. __GT_NOCREATE: simply verify that the name does not exist
  105. at the time of the call.
  106. __GT_FILE: create the file using open(O_CREAT|O_EXCL)
  107. and return a read-write fd. The file is mode 0600.
  108. __GT_BIGFILE: same as __GT_FILE but use open64().
  109. __GT_DIR: create a directory, which will be mode 0700.
  110. We use a clever algorithm to get hard-to-predict names. */
  111. int __gen_tempname (char *tmpl, int kind)
  112. {
  113. char *XXXXXX;
  114. struct timeval tv;
  115. uint32_t high, low, rh;
  116. unsigned int k;
  117. int len, i, count, fd, save_errno = errno;
  118. static uint64_t value; /* Do not initialize this,
  119. or lock it for multi-threaded
  120. apps -- the messier the better */
  121. len = strlen (tmpl);
  122. if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
  123. {
  124. __set_errno (EINVAL);
  125. return -1;
  126. }
  127. /* This is where the Xs start. */
  128. XXXXXX = &tmpl[len - 6];
  129. /* Get some more or less random data. */
  130. gettimeofday (&tv, NULL);
  131. value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
  132. for (count = 0; count < TMP_MAX; value += 7777, ++count)
  133. {
  134. low = value & UINT32_MAX;
  135. high = value >> 32;
  136. for (i = 0 ; i < 6 ; i++) {
  137. rh = high % 62;
  138. high /= 62;
  139. #define L ((UINT32_MAX % 62 + 1) % 62)
  140. k = (low % 62) + (L * rh);
  141. #undef L
  142. #define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62))
  143. low = (low / 62) + (H * rh) + (k / 62);
  144. #undef H
  145. k %= 62;
  146. XXXXXX[i] = letters[k];
  147. }
  148. switch(kind) {
  149. case __GT_NOCREATE:
  150. {
  151. struct stat st;
  152. if (stat (tmpl, &st) < 0)
  153. {
  154. if (errno == ENOENT)
  155. {
  156. __set_errno (save_errno);
  157. return 0;
  158. }
  159. else
  160. /* Give up now. */
  161. return -1;
  162. }
  163. else
  164. continue;
  165. }
  166. case __GT_FILE:
  167. fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  168. break;
  169. #if defined __UCLIBC_HAS_LFS__
  170. case __GT_BIGFILE:
  171. fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  172. break;
  173. #endif
  174. case __GT_DIR:
  175. fd = mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
  176. break;
  177. default:
  178. fd = -1;
  179. assert (! "invalid KIND in __gen_tempname");
  180. }
  181. if (fd >= 0) {
  182. __set_errno (save_errno);
  183. return fd;
  184. }
  185. else if (errno != EEXIST)
  186. /* Any other error will apply also to other names we might
  187. try, and there are 2^32 or so of them, so give up now. */
  188. return -1;
  189. }
  190. /* We got out of the loop because we ran out of combinations to try. */
  191. __set_errno (EEXIST);
  192. return -1;
  193. }