home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / mach / hurd / __dup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  2.0 KB  |  71 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <hurd.h>
  24.  
  25. /* Duplicate FD, returning a new file descriptor open on the same file.  */
  26. int
  27. DEFUN(__dup, (fd), int fd)
  28. {
  29.   int new;
  30.   int dealloc, dealloc_ctty;
  31.   io_t port, ctty;
  32.   int flags;
  33.   int dealloc_dt;
  34.   struct _hurd_fd_user d;
  35.  
  36.   /* Extract the ports and flags from FD.  */
  37.   d = _hurd_fd (fd, &dealloc_dt);
  38.   if (d.d == NULL)
  39.     {
  40.     badf:
  41.       errno = EBADF;
  42.       return -1;
  43.     }
  44.   flags = d.d->flags;
  45.   ctty = _hurd_port_get (&d.d->ctty, &dealloc_ctty);
  46.   port = _hurd_port_locked_get (&d.d->port, &dealloc); /* Unlocks D.d.  */
  47.  
  48.   if (port == MACH_PORT_NULL)
  49.     goto badf;
  50.  
  51.   /* Get a new descriptor.  */
  52.   new = _hurd_dalloc (port, ctty, flags);
  53.   if (new >= 0)
  54.     {
  55.       /* Give the ports each a user ref for the new descriptor.  */
  56.       __mach_port_mod_refs (__mach_task_self (), port,
  57.                 MACH_PORT_RIGHT_SEND, 1);
  58.       if (ctty != MACH_PORT_NULL)
  59.     __mach_port_mod_refs (__mach_task_self (), ctty,
  60.                   MACH_PORT_RIGHT_SEND, 1);
  61.     }
  62.  
  63.   _hurd_port_free (&d->port, port, &dealloc);
  64.   if (ctty != MACH_PORT_NULL)
  65.     _hurd_port_free (&d->ctty, ctty, &dealloc_ctty);
  66.  
  67.   _hurd_fd_done (d, &dealloc_dt);
  68.  
  69.   return new;
  70. }
  71.