date and time created 91/03/07 10:23:53 by bostic
[unix-history] / usr / src / lib / libc / stdio / tempnam.c
CommitLineData
503553be 1/*
2267ab9a
KB
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
019bea33 5 * %sccs.include.redist.c%
503553be
KM
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
96ea859a 9static char sccsid[] = "@(#)tempnam.c 5.1 (Berkeley) %G%";
2267ab9a 10#endif /* LIBC_SCCS and not lint */
503553be 11
2267ab9a 12#include <sys/param.h>
96ea859a 13#include <errno.h>
2267ab9a 14#include <stdio.h>
96ea859a
KB
15#include <stdlib.h>
16#include <unistd.h>
17#include <paths.h>
2267ab9a
KB
18
19char *
20tempnam(dir, pfx)
96ea859a 21 const char *dir, *pfx;
93ced3e8 22{
96ea859a
KB
23 int sverrno;
24 char *f, *name;
93ced3e8 25
96ea859a 26 if (!(name = malloc(MAXPATHLEN)))
2267ab9a 27 return(NULL);
49b91625 28
96ea859a
KB
29 if (!pfx)
30 pfx = "tmp.";
31
49b91625 32 if (f = getenv("TMPDIR")) {
96ea859a 33 (void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
49b91625
KB
34 if (f = mktemp(name))
35 return(f);
2267ab9a 36 }
96ea859a
KB
37
38 if (f = (char *)dir) {
39 (void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
49b91625
KB
40 if (f = mktemp(name))
41 return(f);
2267ab9a 42 }
96ea859a
KB
43
44 f = P_tmpdir;
45 (void)snprintf(name, MAXPATHLEN, "%s/%sXXXXXX", f, pfx);
49b91625
KB
46 if (f = mktemp(name))
47 return(f);
96ea859a
KB
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);
93ced3e8 58}