Back to index

glibc  2.9
tmpfile.c
Go to the documentation of this file.
00001 /* Open a stdio stream on an anonymous temporary file.  Hurd version.
00002    Copyright (C) 2001,2002,2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <hurd.h>
00022 #include <hurd/fs.h>
00023 #include <hurd/fd.h>
00024 #include <fcntl.h>
00025 #include <unistd.h>
00026 #include <iolibio.h>
00027 
00028 /* This returns a new stream opened on a temporary file (generated
00029    by tmpnam).  The file is opened with mode "w+b" (binary read/write).
00030    If we couldn't generate a unique filename or the file couldn't
00031    be opened, NULL is returned.  */
00032 FILE *
00033 __tmpfile (void)
00034 {
00035   error_t err;
00036   file_t file;
00037   int fd;
00038   FILE *f;
00039 
00040   /* Get a port to the directory that will contain the file.  */
00041   const char *dirname = __secure_getenv ("TMPDIR") ?: P_tmpdir;
00042   file_t dir = __file_name_lookup (dirname, 0, 0);
00043   if (dir == MACH_PORT_NULL)
00044     return NULL;
00045 
00046   /* Create an unnamed file in the temporary directory.  */
00047   err = __dir_mkfile (dir, O_RDWR, S_IRUSR | S_IWUSR, &file);
00048   __mach_port_deallocate (__mach_task_self (), dir);
00049   if (err)
00050     return __hurd_fail (err), NULL;
00051 
00052   /* Get a file descriptor for that port.  POSIX.1 requires that streams
00053      returned by tmpfile allocate file descriptors as fopen would.  */
00054   fd = _hurd_intern_fd (file, O_RDWR, 1); /* dealloc on error */
00055   if (fd < 0)
00056     return NULL;
00057 
00058   /* Open a stream on the unnamed file.
00059      It will cease to exist when this stream is closed.  */
00060   if ((f = INTUSE(_IO_fdopen) (fd, "w+b")) == NULL)
00061     __close (fd);
00062 
00063   return f;
00064 }
00065 
00066 #include <shlib-compat.h>
00067 versioned_symbol (libc, __tmpfile, tmpfile, GLIBC_2_1);
00068 
00069 weak_alias (__tmpfile, tmpfile64)