]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Stdio/gettemp.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / Stdio / gettemp.c
CommitLineData
2aa62f2b 1/** @file\r
2 Internal function to generate temporary file name for tmpnam.\r
3\r
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available\r
6 under the terms and conditions of the BSD License that accompanies this\r
7 distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 Copyright (c) 1987, 1993\r
14 The Regents of the University of California. All rights reserved.\r
15\r
16 Redistribution and use in source and binary forms, with or without\r
17 modification, are permitted provided that the following conditions\r
18 are met:\r
19 1. Redistributions of source code must retain the above copyright\r
20 notice, this list of conditions and the following disclaimer.\r
21 2. Redistributions in binary form must reproduce the above copyright\r
22 notice, this list of conditions and the following disclaimer in the\r
23 documentation and/or other materials provided with the distribution.\r
24 3. Neither the name of the University nor the names of its contributors\r
25 may be used to endorse or promote products derived from this software\r
26 without specific prior written permission.\r
27\r
28 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
29 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
31 ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
32 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
34 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
35 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
36 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
37 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
38 SUCH DAMAGE.\r
39\r
40 NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp\r
41**/\r
42#include <LibConfig.h>\r
43\r
44#if HAVE_NBTOOL_CONFIG_H\r
45#include "nbtool_config.h"\r
46#endif\r
47\r
d7ce7006 48#if !defined(HAVE_NBTOOL_CONFIG_H) || !defined(HAVE_MKSTEMP) || !defined(HAVE_MKDTEMP)\r
2aa62f2b 49\r
50#include <sys/EfiCdefs.h>\r
51#if defined(LIBC_SCCS) && !defined(lint)\r
52#if 0\r
53static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";\r
54#else\r
55__RCSID("$NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp $");\r
56#endif\r
57#endif /* LIBC_SCCS and not lint */\r
58\r
59#include <sys/types.h>\r
60#include <sys/stat.h>\r
61\r
62#include <assert.h>\r
63#include <ctype.h>\r
64#include <errno.h>\r
65#include <fcntl.h>\r
66#include <stdio.h>\r
67#include <stdlib.h>\r
68#include <sys/EfiSysCall.h>\r
69\r
70#if HAVE_NBTOOL_CONFIG_H\r
71#define GETTEMP gettemp\r
72#else\r
73#include "reentrant.h"\r
74#include "local.h"\r
75#define GETTEMP __gettemp\r
76#endif\r
77\r
78int\r
79GETTEMP(\r
80 char *path,\r
81 int *doopen,\r
82 int domkdir\r
83 )\r
84{\r
85 char *start, *trv;\r
86 struct stat sbuf;\r
87\r
88 /* To guarantee multiple calls generate unique names even if\r
89 the file is not created. 676 different possibilities with 7\r
90 or more X's, 26 with 6 or less. */\r
91 static char xtra[] = "aa";\r
92 int xcnt = 0;\r
93\r
94 _DIAGASSERT(path != NULL);\r
95 /* doopen may be NULL */\r
96\r
97 /* Move to end of path and count trailing X's. */\r
98 for (trv = path; *trv; ++trv) {\r
99 if (*trv == 'X') {\r
100 xcnt++;\r
101 }\r
102 else {\r
103 xcnt = 0;\r
104 }\r
105 }\r
106\r
107 /* Use at least one from xtra. Use 2 if more than 6 X's. */\r
108 if (*(trv - 1) == 'X')\r
109 *--trv = xtra[0];\r
110 if (xcnt > 6 && *(trv - 1) == 'X')\r
111 *--trv = xtra[1];\r
112\r
113 /* Set remaining X's to 0's. */\r
114 while (*--trv == 'X') {\r
115 *trv = '0';\r
116 }\r
117\r
118 /* update xtra for next call. */\r
119 if (xtra[0] != 'z')\r
120 xtra[0]++;\r
121 else {\r
122 xtra[0] = 'a';\r
123 if (xtra[1] != 'z')\r
124 xtra[1]++;\r
125 else\r
126 xtra[1] = 'a';\r
127 }\r
128\r
129 /*\r
130 * check the target directory; if you have six X's and it\r
131 * doesn't exist this runs for a *very* long time.\r
132 */\r
133 for (start = trv + 1;; --trv) {\r
134 if (trv <= path)\r
135 break;\r
136 if (*trv == '/') {\r
137 *trv = '\0';\r
138 if (stat(path, &sbuf))\r
139 return (0);\r
140 if (!S_ISDIR(sbuf.st_mode)) {\r
141 errno = ENOTDIR;\r
142 return (0);\r
143 }\r
144 *trv = '/';\r
145 break;\r
146 }\r
147 }\r
148\r
149 for (;;) {\r
150 if (doopen) {\r
151 if ((*doopen =\r
152 open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)\r
153 return (1);\r
154 if (errno != EEXIST)\r
155 return (0);\r
156 } else if (domkdir) {\r
157 if (mkdir(path, 0700) >= 0)\r
158 return (1);\r
159 if (errno != EEXIST)\r
160 return (0);\r
161 } else if (lstat(path, &sbuf))\r
162 return (errno == ENOENT ? 1 : 0);\r
163\r
164 /* tricky little algorithm for backward compatibility */\r
165 for (trv = start;;) {\r
166 if (!*trv)\r
167 return (0);\r
168 if (*trv == 'z') {\r
169 *trv++ = 'a';\r
170 }\r
171 else {\r
172 if (isdigit((unsigned char)*trv))\r
173 *trv = 'a';\r
174 else\r
175 ++*trv;\r
176 break;\r
177 }\r
178 }\r
179 }\r
180 /*NOTREACHED*/\r
181}\r
182\r
183#endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */\r