xref: /titanic_52/usr/src/lib/libresolv2/common/bsd/mktemp.c (revision d58fda4376e4bf67072ce2e69f6f47036f9dbb68)
1 /*
2  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
3  * All rights reserved.
4  */
5 
6 #if defined(LIBC_SCCS) && !defined(lint)
7 static const char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
8 static const char rcsid[] = "$Id: mktemp.c,v 8.4 1999/10/13 16:39:21 vixie Exp $";
9 #endif /* LIBC_SCCS and not lint */
10 
11 /*
12  * Copyright (c) 1987, 1993
13  *    The Regents of the University of California.  All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  * 	This product includes software developed by the University of
26  * 	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  */
43 
44 /*
45  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
46  *
47  * Permission to use, copy, modify, and distribute this software for any
48  * purpose with or without fee is hereby granted, provided that the above
49  * copyright notice and this permission notice appear in all copies, and that
50  * the name of Digital Equipment Corporation not be used in advertising or
51  * publicity pertaining to distribution of the document or software without
52  * specific, written prior permission.
53  *
54  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
55  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
57  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
58  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
59  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
60  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
61  * SOFTWARE.
62  */
63 
64 
65 #pragma ident	"%Z%%M%	%I%	%E% SMI"
66 
67 #include "port_before.h"
68 
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 
72 #include <ctype.h>
73 #include <errno.h>
74 #include <fcntl.h>
75 #include <stdio.h>
76 
77 #include "port_after.h"
78 
79 #if (!defined(NEED_MKTEMP)) && (!defined(NEED_MKSTEMP))
80 int __mktemp_unneeded__;
81 #else
82 
83 static int gettemp(char *path, int *doopen);
84 
85 #ifdef NEED_MKSTEMP
86 mkstemp(char *path) {
87 	int fd;
88 
89 	return (gettemp(path, &fd) ? fd : -1);
90 }
91 #endif
92 
93 #ifdef NEED_MKTEMP
94 char *
95 mktemp(char *path) {
96 	return(gettemp(path, (int *)NULL) ? path : (char *)NULL);
97 }
98 #endif
99 
100 static int
101 gettemp(char *path, int *doopen) {
102 	char *start, *trv;
103 	struct stat sbuf;
104 	u_int pid;
105 
106 	pid = getpid();
107 	for (trv = path; *trv; ++trv);		/* extra X's get set to 0's */
108 	while (*--trv == 'X') {
109 		*trv = (pid % 10) + '0';
110 		pid /= 10;
111 	}
112 
113 	/*
114 	 * check the target directory; if you have six X's and it
115 	 * doesn't exist this runs for a *very* long time.
116 	 */
117 	for (start = trv + 1;; --trv) {
118 		if (trv <= path)
119 			break;
120 		if (*trv == '/') {
121 			*trv = '\0';
122 			if (stat(path, &sbuf))
123 				return(0);
124 			if (!S_ISDIR(sbuf.st_mode)) {
125 				errno = ENOTDIR;
126 				return(0);
127 			}
128 			*trv = '/';
129 			break;
130 		}
131 	}
132 
133 	for (;;) {
134 		if (doopen) {
135 			if ((*doopen =
136 			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
137 				return(1);
138 			if (errno != EEXIST)
139 				return(0);
140 		}
141 		else if (stat(path, &sbuf))
142 			return(errno == ENOENT ? 1 : 0);
143 
144 		/* tricky little algorithm for backward compatibility */
145 		for (trv = start;;) {
146 			if (!*trv)
147 				return(0);
148 			if (*trv == 'z')
149 				*trv++ = 'a';
150 			else {
151 				if (isdigit(*trv))
152 					*trv = 'a';
153 				else
154 					++*trv;
155 				break;
156 			}
157 		}
158 	}
159 	/*NOTREACHED*/
160 }
161 
162 #endif /*NEED_MKTEMP*/
163