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