xref: /openbsd-src/lib/libc/stdio/tempnam.c (revision e34cb67ca6a03d6ed8f381b6dd30bd8428efd463)
1*e34cb67cStb /*	$OpenBSD: tempnam.c,v 1.20 2017/11/28 06:55:49 tb Exp $ */
2df930be7Sderaadt /*
3df930be7Sderaadt  * Copyright (c) 1988, 1993
4df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
5df930be7Sderaadt  *
6df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
7df930be7Sderaadt  * modification, are permitted provided that the following conditions
8df930be7Sderaadt  * are met:
9df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
10df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
11df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
13df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
146580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
15df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
16df930be7Sderaadt  *    without specific prior written permission.
17df930be7Sderaadt  *
18df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df930be7Sderaadt  * SUCH DAMAGE.
29df930be7Sderaadt  */
30df930be7Sderaadt 
31df930be7Sderaadt #include <errno.h>
3269245ebdSmillert #include <limits.h>
3369245ebdSmillert #include <paths.h>
34df930be7Sderaadt #include <stdio.h>
35df930be7Sderaadt #include <stdlib.h>
36df930be7Sderaadt #include <string.h>
37df930be7Sderaadt #include <unistd.h>
38df930be7Sderaadt 
395cf55735Sderaadt __warn_references(tempnam,
40*e34cb67cStb     "tempnam() possibly used unsafely; consider using mkstemp()");
415cf55735Sderaadt 
42df930be7Sderaadt char *
tempnam(const char * dir,const char * pfx)434e503363Sotto tempnam(const char *dir, const char *pfx)
44df930be7Sderaadt {
45aa156354Smoritz 	int sverrno, len;
46df930be7Sderaadt 	char *f, *name;
47df930be7Sderaadt 
4869245ebdSmillert 	if (!(name = malloc(PATH_MAX)))
49df930be7Sderaadt 		return(NULL);
50df930be7Sderaadt 
51df930be7Sderaadt 	if (!pfx)
52df930be7Sderaadt 		pfx = "tmp.";
53df930be7Sderaadt 
5415f69b10Smoritz 	if (issetugid() == 0 && (f = getenv("TMPDIR")) && *f != '\0') {
5569245ebdSmillert 		len = snprintf(name, PATH_MAX, "%s%s%sXXXXXXXXXX", f,
56aa156354Smoritz 		    f[strlen(f) - 1] == '/' ? "" : "/", pfx);
5769245ebdSmillert 		if (len < 0 || len >= PATH_MAX) {
58aa156354Smoritz 			errno = ENAMETOOLONG;
5998e59ffbSjsg 			goto fail;
60aa156354Smoritz 		}
6120675415Sderaadt 		if ((f = _mktemp(name)))
62df930be7Sderaadt 			return(f);
63df930be7Sderaadt 	}
64df930be7Sderaadt 
6515f69b10Smoritz 	if (dir != NULL) {
6615f69b10Smoritz 		f = *dir ? (char *)dir : ".";
6769245ebdSmillert 		len = snprintf(name, PATH_MAX, "%s%s%sXXXXXXXXXX", f,
68aa156354Smoritz 		    f[strlen(f) - 1] == '/' ? "" : "/", pfx);
6969245ebdSmillert 		if (len < 0 || len >= PATH_MAX) {
70aa156354Smoritz 			errno = ENAMETOOLONG;
7198e59ffbSjsg 			goto fail;
72aa156354Smoritz 		}
7320675415Sderaadt 		if ((f = _mktemp(name)))
74df930be7Sderaadt 			return(f);
75df930be7Sderaadt 	}
76df930be7Sderaadt 
77df930be7Sderaadt 	f = P_tmpdir;
7869245ebdSmillert 	len = snprintf(name, PATH_MAX, "%s%sXXXXXXXXX", f, pfx);
7969245ebdSmillert 	if (len < 0 || len >= PATH_MAX) {
80aa156354Smoritz 		errno = ENAMETOOLONG;
8198e59ffbSjsg 		goto fail;
82aa156354Smoritz 	}
8320675415Sderaadt 	if ((f = _mktemp(name)))
84df930be7Sderaadt 		return(f);
85df930be7Sderaadt 
86df930be7Sderaadt 	f = _PATH_TMP;
8769245ebdSmillert 	len = snprintf(name, PATH_MAX, "%s%sXXXXXXXXX", f, pfx);
8869245ebdSmillert 	if (len < 0 || len >= PATH_MAX) {
89aa156354Smoritz 		errno = ENAMETOOLONG;
9098e59ffbSjsg 		goto fail;
91aa156354Smoritz 	}
9220675415Sderaadt 	if ((f = _mktemp(name)))
93df930be7Sderaadt 		return(f);
94df930be7Sderaadt 
9598e59ffbSjsg fail:
96df930be7Sderaadt 	sverrno = errno;
97df930be7Sderaadt 	free(name);
98df930be7Sderaadt 	errno = sverrno;
99df930be7Sderaadt 	return(NULL);
100df930be7Sderaadt }
101