]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/gettemp.c
2 Internal function to generate temporary file name for tmpnam.
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.
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.
13 Copyright (c) 1987, 1993
14 The Regents of the University of California. All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
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.
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
40 NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp
42 #include <LibConfig.h>
44 #if HAVE_NBTOOL_CONFIG_H
45 #include "nbtool_config.h"
48 #if !defined(HAVE_NBTOOL_CONFIG_H) || !defined(HAVE_MKSTEMP) || !defined(HAVE_MKDTEMP)
50 #include <sys/EfiCdefs.h>
51 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid
[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
55 __RCSID("$NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp $");
57 #endif /* LIBC_SCCS and not lint */
59 #include <sys/types.h>
68 #include <sys/EfiSysCall.h>
70 #if HAVE_NBTOOL_CONFIG_H
71 #define GETTEMP gettemp
73 #include "reentrant.h"
75 #define GETTEMP __gettemp
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";
94 _DIAGASSERT(path
!= NULL
);
95 /* doopen may be NULL */
97 /* Move to end of path and count trailing X's. */
98 for (trv
= path
; *trv
; ++trv
) {
107 /* Use at least one from xtra. Use 2 if more than 6 X's. */
108 if (*(trv
- 1) == 'X')
110 if (xcnt
> 6 && *(trv
- 1) == 'X')
113 /* Set remaining X's to 0's. */
114 while (*--trv
== 'X') {
118 /* update xtra for next call. */
130 * check the target directory; if you have six X's and it
131 * doesn't exist this runs for a *very* long time.
133 for (start
= trv
+ 1;; --trv
) {
138 if (stat(path
, &sbuf
))
140 if (!S_ISDIR(sbuf
.st_mode
)) {
152 open(path
, O_CREAT
| O_EXCL
| O_RDWR
, 0600)) >= 0)
156 } else if (domkdir
) {
157 if (mkdir(path
, 0700) >= 0)
161 } else if (lstat(path
, &sbuf
))
162 return (errno
== ENOENT
? 1 : 0);
164 /* tricky little algorithm for backward compatibility */
165 for (trv
= start
;;) {
172 if (isdigit((unsigned char)*trv
))
183 #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */