tempname.c 5.7 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. /* Return nonzero if DIR is an existent directory. */
  32. static int direxists (const char *dir)
  33. {
  34. struct stat buf;
  35. return stat(dir, &buf) == 0 && S_ISDIR (buf.st_mode);
  36. }
  37. /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is
  38. non-null and exists, uses it; otherwise uses the first of $TMPDIR,
  39. P_tmpdir, /tmp that exists. Copies into TMPL a template suitable
  40. for use with mk[s]temp. Will fail (-1) if DIR is non-null and
  41. doesn't exist, none of the searched dirs exists, or there's not
  42. enough space in TMPL. */
  43. int __path_search (char *tmpl, size_t tmpl_len, const char *dir,
  44. const char *pfx, int try_tmpdir)
  45. {
  46. //const char *d;
  47. size_t dlen, plen;
  48. if (!pfx || !pfx[0])
  49. {
  50. pfx = "file";
  51. plen = 4;
  52. }
  53. else
  54. {
  55. plen = strlen (pfx);
  56. if (plen > 5)
  57. plen = 5;
  58. }
  59. #if 0
  60. if (try_tmpdir)
  61. {
  62. d = __secure_getenv ("TMPDIR");
  63. if (d != NULL && direxists (d))
  64. dir = d;
  65. else if (dir != NULL && direxists (dir))
  66. /* nothing */ ;
  67. else
  68. dir = NULL;
  69. }
  70. #endif
  71. if (dir == NULL)
  72. {
  73. if (direxists (P_tmpdir))
  74. dir = P_tmpdir;
  75. else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
  76. dir = "/tmp";
  77. else
  78. {
  79. __set_errno (ENOENT);
  80. return -1;
  81. }
  82. }
  83. dlen = strlen (dir);
  84. while (dlen > 1 && dir[dlen - 1] == '/')
  85. dlen--; /* remove trailing slashes */
  86. /* check we have room for "${dir}/${pfx}XXXXXX\0" */
  87. if (tmpl_len < dlen + 1 + plen + 6 + 1)
  88. {
  89. __set_errno (EINVAL);
  90. return -1;
  91. }
  92. sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx);
  93. return 0;
  94. }
  95. /* These are the characters used in temporary filenames. */
  96. static const char letters[] =
  97. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  98. /* Generate a temporary file name based on TMPL. TMPL must match the
  99. rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
  100. does not exist at the time of the call to __gen_tempname. TMPL is
  101. overwritten with the result.
  102. KIND may be one of:
  103. __GT_NOCREATE: simply verify that the name does not exist
  104. at the time of the call.
  105. __GT_FILE: create the file using open(O_CREAT|O_EXCL)
  106. and return a read-write fd. The file is mode 0600.
  107. __GT_BIGFILE: same as __GT_FILE but use open64().
  108. __GT_DIR: create a directory, which will be mode 0700.
  109. We use a clever algorithm to get hard-to-predict names. */
  110. int __gen_tempname (char *tmpl, int kind)
  111. {
  112. int len;
  113. char *XXXXXX;
  114. static uint64_t value;
  115. struct timeval tv;
  116. uint32_t high, low, rh;
  117. unsigned int k;
  118. int count, fd;
  119. int save_errno = errno;
  120. int i;
  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_HAVE_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. }