xref: /netbsd-src/lib/libc/stdio/tempnam.c (revision 526d942790cc352b6ea7931f9884cc6bdbf20404)
1*526d9427Schristos /*	$NetBSD: tempnam.c,v 1.22 2012/03/15 18:22:30 christos Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*
4255db7b2Sjtc  * Copyright (c) 1988, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
320c339c44Schristos #include <sys/cdefs.h>
3361f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
34255db7b2Sjtc #if 0
35255db7b2Sjtc static char sccsid[] = "@(#)tempnam.c	8.1 (Berkeley) 6/4/93";
360c339c44Schristos #else
37*526d9427Schristos __RCSID("$NetBSD: tempnam.c,v 1.22 2012/03/15 18:22:30 christos Exp $");
38255db7b2Sjtc #endif
3961f28255Scgd #endif /* LIBC_SCCS and not lint */
4061f28255Scgd 
41ee1afe8bSkleink #include "namespace.h"
4261f28255Scgd #include <sys/param.h>
4361f28255Scgd #include <errno.h>
4461f28255Scgd #include <stdio.h>
4561f28255Scgd #include <stdlib.h>
464d09105fScgd #include <string.h>
4761f28255Scgd #include <unistd.h>
4861f28255Scgd #include <paths.h>
493fdac2b8Sthorpej #include "reentrant.h"
500c339c44Schristos #include "local.h"
511960b713Slukem 
521960b713Slukem __warn_references(tempnam,
5390cb894bSmycroft     "warning: tempnam() possibly used unsafely, use mkstemp() or mkdtemp()")
541960b713Slukem 
55f44796a7Schristos static const char *
trailsl(const char * f)56f44796a7Schristos trailsl(const char *f)
57f44796a7Schristos {
58f44796a7Schristos 	const char *s = f;
59f44796a7Schristos 	while (*s)
60f44796a7Schristos 		s++;
6106666adaSdrochner 	return (f != s && s[-1] == '/') ? "" : "/";
62f44796a7Schristos }
63f44796a7Schristos 
64f44796a7Schristos static char *
gentemp(char * name,size_t len,const char * tmp,const char * pfx)65f44796a7Schristos gentemp(char *name, size_t len, const char *tmp, const char  *pfx)
66f44796a7Schristos {
67f44796a7Schristos 	(void)snprintf(name, len, "%s%s%sXXXXXXXXXX", tmp, trailsl(tmp), pfx);
68f44796a7Schristos 	return _mktemp(name);
69f44796a7Schristos }
70f44796a7Schristos 
7161f28255Scgd char *
tempnam(const char * dir,const char * pfx)72f44796a7Schristos tempnam(const char *dir, const char *pfx)
7361f28255Scgd {
7461f28255Scgd 	int sverrno;
75f44796a7Schristos 	char *name, *f;
76f44796a7Schristos 	const char *tmp;
7761f28255Scgd 
78cfbb35edSchristos 	if (!(name = malloc((size_t)MAXPATHLEN)))
79f44796a7Schristos 		return NULL;
8061f28255Scgd 
8161f28255Scgd 	if (!pfx)
8261f28255Scgd 		pfx = "tmp.";
8361f28255Scgd 
84f44796a7Schristos 	if ((tmp = getenv("TMPDIR")) != NULL &&
85cfbb35edSchristos 	    (f = gentemp(name, (size_t)MAXPATHLEN, tmp, pfx)) != NULL)
86f44796a7Schristos 		return f;
8761f28255Scgd 
88f44796a7Schristos 	if (dir != NULL &&
89cfbb35edSchristos 	    (f = gentemp(name, (size_t)MAXPATHLEN, dir, pfx)) != NULL)
90f44796a7Schristos 		return f;
9161f28255Scgd 
92cfbb35edSchristos 	if ((f = gentemp(name, (size_t)MAXPATHLEN, P_tmpdir, pfx)) != NULL)
93f44796a7Schristos 		return f;
9461f28255Scgd 
95cfbb35edSchristos 	if ((f = gentemp(name, (size_t)MAXPATHLEN, _PATH_TMP, pfx)) != NULL)
96f44796a7Schristos 		return f;
9761f28255Scgd 
9861f28255Scgd 	sverrno = errno;
9961f28255Scgd 	free(name);
10061f28255Scgd 	errno = sverrno;
101*526d9427Schristos 	return NULL;
10261f28255Scgd }
103