tempname.c 4.8 KB

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