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