mkstemp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Adapted from NetBSB libc by Dieter Baron */
  2. /* NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp */
  3. /*
  4. * Copyright (c) 1987, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <assert.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #ifdef _WIN32
  38. #include <io.h>
  39. #endif
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #ifndef O_BINARY
  43. #define O_BINARY 0
  44. #endif
  45. int
  46. _zip_mkstemp(char *path)
  47. {
  48. #ifdef _WIN32
  49. int ret;
  50. ret = _creat(_mktemp(path), _S_IREAD|_S_IWRITE);
  51. if (ret == -1) {
  52. return 0;
  53. } else {
  54. return ret;
  55. }
  56. #else
  57. int fd;
  58. char *start, *trv;
  59. struct stat sbuf;
  60. pid_t pid;
  61. /* To guarantee multiple calls generate unique names even if
  62. the file is not created. 676 different possibilities with 7
  63. or more X's, 26 with 6 or less. */
  64. static char xtra[2] = "aa";
  65. int xcnt = 0;
  66. pid = getpid();
  67. /* Move to end of path and count trailing X's. */
  68. for (trv = path; *trv; ++trv)
  69. if (*trv == 'X')
  70. xcnt++;
  71. else
  72. xcnt = 0;
  73. /* Use at least one from xtra. Use 2 if more than 6 X's. */
  74. if (*(trv - 1) == 'X')
  75. *--trv = xtra[0];
  76. if (xcnt > 6 && *(trv - 1) == 'X')
  77. *--trv = xtra[1];
  78. /* Set remaining X's to pid digits with 0's to the left. */
  79. while (*--trv == 'X') {
  80. *trv = (pid % 10) + '0';
  81. pid /= 10;
  82. }
  83. /* update xtra for next call. */
  84. if (xtra[0] != 'z')
  85. xtra[0]++;
  86. else {
  87. xtra[0] = 'a';
  88. if (xtra[1] != 'z')
  89. xtra[1]++;
  90. else
  91. xtra[1] = 'a';
  92. }
  93. /*
  94. * check the target directory; if you have six X's and it
  95. * doesn't exist this runs for a *very* long time.
  96. */
  97. for (start = trv + 1;; --trv) {
  98. if (trv <= path)
  99. break;
  100. if (*trv == '/') {
  101. *trv = '\0';
  102. if (stat(path, &sbuf))
  103. return (0);
  104. if (!S_ISDIR(sbuf.st_mode)) {
  105. errno = ENOTDIR;
  106. return (0);
  107. }
  108. *trv = '/';
  109. break;
  110. }
  111. }
  112. for (;;) {
  113. if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)
  114. return (fd);
  115. if (errno != EEXIST)
  116. return (0);
  117. /* tricky little algorithm for backward compatibility */
  118. for (trv = start;;) {
  119. if (!*trv)
  120. return (0);
  121. if (*trv == 'z')
  122. *trv++ = 'a';
  123. else {
  124. if (isdigit((unsigned char)*trv))
  125. *trv = 'a';
  126. else
  127. ++*trv;
  128. break;
  129. }
  130. }
  131. }
  132. /*NOTREACHED*/
  133. #endif
  134. }