11573Srgrimes/*
21573Srgrimes * Copyright (c) 1988, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
13249808Semaste * 3. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311573Srgrimesstatic char sccsid[] = "@(#)tempnam.c	8.1 (Berkeley) 6/4/93";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3392986Sobrien#include <sys/cdefs.h>
3492986Sobrien__FBSDID("$FreeBSD$");
351573Srgrimes
361573Srgrimes#include <sys/param.h>
371573Srgrimes#include <errno.h>
381573Srgrimes#include <stdio.h>
391573Srgrimes#include <stdlib.h>
4016586Sjraynard#include <string.h>
411573Srgrimes#include <unistd.h>
421573Srgrimes#include <paths.h>
431573Srgrimes
4450121Simp__warn_references(tempnam,
4550121Simp    "warning: tempnam() possibly used unsafely; consider using mkstemp()");
4650121Simp
4792905Sobrienextern char *_mktemp(char *);
4850121Simp
491573Srgrimeschar *
50249810Semastetempnam(const char *dir, const char *pfx)
511573Srgrimes{
521573Srgrimes	int sverrno;
531573Srgrimes	char *f, *name;
541573Srgrimes
551573Srgrimes	if (!(name = malloc(MAXPATHLEN)))
561573Srgrimes		return(NULL);
571573Srgrimes
581573Srgrimes	if (!pfx)
591573Srgrimes		pfx = "tmp.";
601573Srgrimes
6150121Simp	if (issetugid() == 0 && (f = getenv("TMPDIR"))) {
621573Srgrimes		(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
631573Srgrimes		    *(f + strlen(f) - 1) == '/'? "": "/", pfx);
6450121Simp		if ((f = _mktemp(name)))
651573Srgrimes			return(f);
661573Srgrimes	}
671573Srgrimes
6816337Sjraynard	if ((f = (char *)dir)) {
691573Srgrimes		(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
701573Srgrimes		    *(f + strlen(f) - 1) == '/'? "": "/", pfx);
7152466Sache		if ((f = _mktemp(name)))
721573Srgrimes			return(f);
731573Srgrimes	}
741573Srgrimes
751573Srgrimes	f = P_tmpdir;
761573Srgrimes	(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
7752466Sache	if ((f = _mktemp(name)))
781573Srgrimes		return(f);
791573Srgrimes
801573Srgrimes	f = _PATH_TMP;
811573Srgrimes	(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
8252466Sache	if ((f = _mktemp(name)))
831573Srgrimes		return(f);
841573Srgrimes
851573Srgrimes	sverrno = errno;
861573Srgrimes	free(name);
871573Srgrimes	errno = sverrno;
881573Srgrimes	return(NULL);
891573Srgrimes}
90