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