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