2001-11-13 10:07:33 +00:00
|
|
|
/* Open a stdio stream on an anonymous temporary file. Generic/POSIX version.
|
2018-01-01 00:32:25 +00:00
|
|
|
Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
1996-12-12 03:24:48 +00:00
|
|
|
This file is part of the GNU C Library.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1996-12-12 03:24:48 +00:00
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1996-12-12 03:24:48 +00:00
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2007-08-11 02:48:28 +00:00
|
|
|
#include <fcntl.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
#include <stdio.h>
|
1998-08-04 17:22:28 +00:00
|
|
|
#include <unistd.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2011-09-10 14:34:15 -04:00
|
|
|
#include <iolibio.h>
|
2012-05-23 13:33:15 +02:00
|
|
|
#define __fdopen _IO_fdopen
|
2011-09-10 14:34:15 -04:00
|
|
|
#ifndef tmpfile
|
|
|
|
|
# define tmpfile __new_tmpfile
|
1998-08-03 16:47:01 +00:00
|
|
|
#endif
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2002-06-11 23:04:51 +00:00
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* This returns a new stream opened on a temporary file (generated
|
1998-08-08 20:02:34 +00:00
|
|
|
by tmpnam). The file is opened with mode "w+b" (binary read/write).
|
1995-02-18 01:27:10 +00:00
|
|
|
If we couldn't generate a unique filename or the file couldn't
|
|
|
|
|
be opened, NULL is returned. */
|
|
|
|
|
FILE *
|
1998-08-08 20:02:34 +00:00
|
|
|
tmpfile (void)
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
1998-08-03 16:47:01 +00:00
|
|
|
int fd;
|
1995-02-18 01:27:10 +00:00
|
|
|
FILE *f;
|
2007-08-11 02:48:28 +00:00
|
|
|
int flags = 0;
|
|
|
|
|
#ifdef FLAGS
|
|
|
|
|
flags = FLAGS;
|
|
|
|
|
#endif
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2017-06-26 11:52:14 -03:00
|
|
|
/* First try a system specific method. */
|
|
|
|
|
fd = __gen_tempfd (flags);
|
|
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
char buf[FILENAME_MAX];
|
|
|
|
|
|
|
|
|
|
if (__path_search (buf, sizeof buf, NULL, "tmpf", 0))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
fd = __gen_tempname (buf, 0, flags, __GT_FILE);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* Note that this relies on the Unix semantics that
|
|
|
|
|
a file is not really removed until it is closed. */
|
|
|
|
|
(void) __unlink (buf);
|
|
|
|
|
}
|
1998-08-03 16:47:01 +00:00
|
|
|
|
1998-08-08 20:02:34 +00:00
|
|
|
if ((f = __fdopen (fd, "w+b")) == NULL)
|
|
|
|
|
__close (fd);
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
|
return f;
|
|
|
|
|
}
|
1998-08-08 20:02:34 +00:00
|
|
|
|
2012-07-30 16:09:11 -07:00
|
|
|
#if !defined O_LARGEFILE || O_LARGEFILE == 0
|
|
|
|
|
weak_alias (__new_tmpfile, tmpfile64)
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-09-10 14:34:15 -04:00
|
|
|
#ifndef FLAGS /* Not for tmpfile64. */
|
1998-08-08 20:02:34 +00:00
|
|
|
# undef tmpfile
|
2000-03-27 16:51:19 +00:00
|
|
|
# include <shlib-compat.h>
|
|
|
|
|
versioned_symbol (libc, __new_tmpfile, tmpfile, GLIBC_2_1);
|
1998-08-08 20:02:34 +00:00
|
|
|
#endif
|