]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/tempnam.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Stdio / tempnam.c
CommitLineData
2aa62f2b 1/* $NetBSD: tempnam.c,v 1.19 2005/07/27 13:23:07 drochner Exp $ */\r
2\r
3/*\r
4 * Copyright (c) 1988, 1993\r
5 * The Regents of the University of California. All rights reserved.\r
6 *\r
7 * Redistribution and use in source and binary forms, with or without\r
8 * modification, are permitted provided that the following conditions\r
9 * are met:\r
10 * 1. Redistributions of source code must retain the above copyright\r
11 * notice, this list of conditions and the following disclaimer.\r
12 * 2. Redistributions in binary form must reproduce the above copyright\r
13 * notice, this list of conditions and the following disclaimer in the\r
14 * documentation and/or other materials provided with the distribution.\r
15 * 3. Neither the name of the University nor the names of its contributors\r
16 * may be used to endorse or promote products derived from this software\r
17 * without specific prior written permission.\r
18 *\r
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
29 * SUCH DAMAGE.\r
30 */\r
31#include <LibConfig.h>\r
32#include <sys/EfiCdefs.h>\r
33#if defined(LIBC_SCCS) && !defined(lint)\r
34#if 0\r
35static char sccsid[] = "@(#)tempnam.c 8.1 (Berkeley) 6/4/93";\r
36#else\r
37__RCSID("$NetBSD: tempnam.c,v 1.19 2005/07/27 13:23:07 drochner Exp $");\r
38#endif\r
39#endif /* LIBC_SCCS and not lint */\r
40\r
41#include "namespace.h"\r
42#include <sys/param.h>\r
43#include <errno.h>\r
44#include <stdio.h>\r
45#include <stdlib.h>\r
46#include <string.h>\r
47#include <sys/EfiSysCall.h>\r
48#include <paths.h>\r
49#include "reentrant.h"\r
50#include "local.h"\r
51\r
52__warn_references(tempnam,\r
53 "warning: tempnam() possibly used unsafely, use mkstemp() or mkdtemp()")\r
54\r
55static const char *\r
56trailsl(const char *f)\r
57{\r
58 const char *s = f;\r
59 while (*s)\r
60 s++;\r
61 return (f != s && s[-1] == '/') ? "" : "/";\r
62}\r
63\r
64static char *\r
65gentemp(char *name, size_t len, const char *tmp, const char *pfx)\r
66{\r
67 (void)snprintf(name, len, "%s%s%sXXXX", tmp, trailsl(tmp), pfx);\r
68 return _mktemp(name);\r
69}\r
70\r
71char *\r
72tempnam(const char *dir, const char *pfx)\r
73{\r
74 int sverrno;\r
75 char *name, *f;\r
76 const char *tmp;\r
77\r
78 if (!(name = malloc((size_t)MAXPATHLEN)))\r
79 return NULL;\r
80\r
81 if (!pfx)\r
82 pfx = "tmp.";\r
83\r
84 if ((tmp = getenv("TMPDIR")) != NULL &&\r
85 (f = gentemp(name, (size_t)MAXPATHLEN, tmp, pfx)) != NULL)\r
86 return f;\r
87\r
88 if (dir != NULL &&\r
89 (f = gentemp(name, (size_t)MAXPATHLEN, dir, pfx)) != NULL)\r
90 return f;\r
91\r
92 //if ((f = gentemp(name, (size_t)MAXPATHLEN, P_tmpdir, pfx)) != NULL)\r
93 // return f;\r
94\r
95 if ((f = gentemp(name, (size_t)MAXPATHLEN, _PATH_TMP, pfx)) != NULL)\r
96 return f;\r
97\r
98 sverrno = errno;\r
99 free(name);\r
100 errno = sverrno;\r
101 return(NULL);\r
102}\r