tempname.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. int len;
  114. char *XXXXXX;
  115. static uint64_t value;
  116. struct timeval tv;
  117. uint32_t high, low, rh;
  118. unsigned int k;
  119. int count, fd;
  120. int save_errno = errno;
  121. int i;
  122. len = strlen (tmpl);
  123. if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
  124. {
  125. __set_errno (EINVAL);
  126. return -1;
  127. }
  128. /* This is where the Xs start. */
  129. XXXXXX = &tmpl[len - 6];
  130. /* Get some more or less random data. */
  131. gettimeofday (&tv, NULL);
  132. value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
  133. for (count = 0; count < TMP_MAX; value += 7777, ++count)
  134. {
  135. low = value & UINT32_MAX;
  136. high = value >> 32;
  137. for (i = 0 ; i < 6 ; i++) {
  138. rh = high % 62;
  139. high /= 62;
  140. #define L ((UINT32_MAX % 62 + 1) % 62)
  141. k = (low % 62) + (L * rh);
  142. #undef L
  143. #define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62))
  144. low = (low / 62) + (H * rh) + (k / 62);
  145. #undef H
  146. k %= 62;
  147. XXXXXX[i] = letters[k];
  148. }
  149. switch(kind) {
  150. case __GT_NOCREATE:
  151. {
  152. struct stat st;
  153. if (stat (tmpl, &st) < 0)
  154. {
  155. if (errno == ENOENT)
  156. {
  157. __set_errno (save_errno);
  158. return 0;
  159. }
  160. else
  161. /* Give up now. */
  162. return -1;
  163. }
  164. else
  165. continue;
  166. }
  167. case __GT_FILE:
  168. fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  169. break;
  170. #if defined __UCLIBC_HAVE_LFS__
  171. case __GT_BIGFILE:
  172. fd = open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  173. break;
  174. #endif
  175. case __GT_DIR:
  176. fd = mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
  177. break;
  178. default:
  179. fd = -1;
  180. assert (! "invalid KIND in __gen_tempname");
  181. }
  182. if (fd >= 0) {
  183. __set_errno (save_errno);
  184. return fd;
  185. }
  186. else if (errno != EEXIST)
  187. /* Any other error will apply also to other names we might
  188. try, and there are 2^32 or so of them, so give up now. */
  189. return -1;
  190. }
  191. /* We got out of the loop because we ran out of combinations to try. */
  192. __set_errno (EEXIST);
  193. return -1;
  194. }