xref: /original-bsd/lib/libc/stdio/tempnam.c (revision b7cc7b86)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)tempnam.c	5.1 (Berkeley) 02/22/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <paths.h>
18 
19 char *
20 tempnam(dir, pfx)
21 	const char *dir, *pfx;
22 {
23 	int sverrno;
24 	char *f, *name;
25 
26 	if (!(name = malloc(MAXPATHLEN)))
27 		return(NULL);
28 
29 	if (!pfx)
30 		pfx = "tmp.";
31 
32 	if (f = getenv("TMPDIR")) {
33 		(void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
34 		if (f = mktemp(name))
35 			return(f);
36 	}
37 
38 	if (f = (char *)dir) {
39 		(void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
40 		if (f = mktemp(name))
41 			return(f);
42 	}
43 
44 	f = P_tmpdir;
45 	(void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
46 	if (f = mktemp(name))
47 		return(f);
48 
49 	f = _PATH_TMP;
50 	(void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
51 	if (f = mktemp(name))
52 		return(f);
53 
54 	sverrno = errno;
55 	free(name);
56 	errno = sverrno;
57 	return(NULL);
58 }
59