1 /* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
2 /*
3 * Copyright (c) 1987, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
37 #endif /* LIBC_SCCS and not lint */
38
39 #include "k5-platform.h"
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <ctype.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #ifndef O_BINARY
52 #define O_BINARY 0
53 #endif
54
55 #if !defined S_ISDIR
56 #if defined S_IFMT
57 #define S_ISDIR(MODE) (((MODE) & S_IFMT) == S_IFDIR)
58 #elif defined _S_IFMT
59 #define S_ISDIR(MODE) (((MODE) & _S_IFMT) == _S_IFDIR)
60 #else
61 /* Hope that there's a S_ISDIR function defined. */
62 #endif
63 #endif
64
65 static int _gettemp(char *, int *);
66
mkstemp(path)67 int mkstemp(path)
68 char *path;
69 {
70 int fd;
71
72 return (_gettemp(path, &fd) ? fd : -1);
73 }
74
75 static int
_gettemp(path,doopen)76 _gettemp(path, doopen)
77 char *path;
78 int *doopen;
79 {
80 char *start, *trv;
81 struct stat sbuf;
82 u_int pid;
83
84 pid = getpid();
85 for (trv = path; *trv; ++trv); /* extra X's get set to 0's */
86 while (*--trv == 'X') {
87 *trv = (pid % 10) + '0';
88 pid /= 10;
89 }
90
91 /*
92 * check the target directory; if you have six X's and it
93 * doesn't exist this runs for a *very* long time.
94 */
95 for (start = trv + 1;; --trv) {
96 if (trv <= path)
97 break;
98 if (*trv == '/') {
99 *trv = '\0';
100 if (stat(path, &sbuf))
101 return(0);
102 if (!S_ISDIR(sbuf.st_mode)) {
103 errno = ENOTDIR;
104 return(0);
105 }
106 *trv = '/';
107 break;
108 }
109 }
110
111 for (;;) {
112 if (doopen) {
113 if ((*doopen =
114 open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)
115 return(1);
116 if (errno != EEXIST)
117 return(0);
118 }
119 else if (stat(path, &sbuf))
120 return(errno == ENOENT ? 1 : 0);
121
122 /* tricky little algorithm for backward compatibility */
123 for (trv = start;;) {
124 if (!*trv)
125 return(0);
126 if (*trv == 'z')
127 *trv++ = 'a';
128 else {
129 if (isdigit(*trv))
130 *trv = 'a';
131 else
132 ++*trv;
133 break;
134 }
135 }
136 }
137 /*NOTREACHED*/
138 }
139