bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fileno(tmpfile()) returns EBADF


From: Roland McGrath
Subject: Re: fileno(tmpfile()) returns EBADF
Date: Wed, 17 Apr 2002 14:11:44 -0400 (EDT)

I think the Hurd libc implementation of tmpfile is at fault.
POSIX.1 says there must be a file descriptor.  Try this libc patch.


Index: tmpfile.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/mach/hurd/tmpfile.c,v
retrieving revision 1.1
diff -u -b -p -r1.1 tmpfile.c
--- tmpfile.c   13 Nov 2001 10:07:11 -0000      1.1
+++ tmpfile.c   17 Apr 2002 18:09:29 -0000
@@ -1,5 +1,5 @@
 /* Open a stdio stream on an anonymous temporary file.  Hurd version.
-   Copyright (C) 2001 Free Software Foundation, Inc.
+   Copyright (C) 2001,02 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <hurd.h>
 #include <hurd/fs.h>
+#include <hurd/fd.h>
 #include <fcntl.h>
 
 #ifdef USE_IN_LIBIO
@@ -37,6 +38,7 @@ tmpfile (void)
 {
   error_t err;
   file_t file;
+  int fd;
   FILE *f;
 
   /* Get a port to the directory that will contain the file.  */
@@ -51,10 +53,15 @@ tmpfile (void)
   if (err)
     return __hurd_fail (err), NULL;
 
-  /* Open a stream on the port to the unnamed file.
+  /* Get a file descriptor for that port.  POSIX.1 requires that streams
+     returned by tmpfile allocate file descriptors as fopen would.  */
+  if (_hurd_intern_fd (file, O_RDWR, &fd, 1) < 0) /* dealloc on error */
+    return -1;
+
+  /* Open a stream on the unnamed file.
      It will cease to exist when this stream is closed.  */
-  if ((f = __fopenport (file, "w+b")) == NULL)
-    __mach_port_deallocate (__mach_task_self (), file);
+  if ((f = __fdopen (fd, "w+b")) == NULL)
+    __close (fd);
 
   return f;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]