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 / __dup2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  2.3 KB  |  82 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.  
  26. /* Duplicate FD to FD2, closing the old FD2 and making FD2 be
  27.    open on the same file as FD is.  Return FD2 or -1.  */
  28. int
  29. DEFUN(__dup2, (fd, fd2), int fd AND int fd2)
  30. {
  31.   int dealloc_dt;
  32.   struct _hurd_fd_user d, d2;
  33.   io_t port, ctty;
  34.   int dealloc, dealloc_ctty;
  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->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.   __mutex_lock (&_hurd_dtable_lock);
  49.   if (fd2 < 0 || fd2 >= _hurd_dtable.size)
  50.     {
  51.       errno = EBADF;
  52.       fd2 = -1;
  53.     }
  54.   else
  55.     {
  56.       d2 = &_hurd_dtable.d[fd2];
  57.       __spin_lock (&d2->port.lock);
  58.       d2->flags = flags;
  59.       _hurd_port_set (&d2->ctty, ctty);
  60.       _hurd_port_locked_set (&d2->port, port); /* Unlocks D2.  */
  61.     }
  62.   __mutex_unlock (&_hurd_dtable_lock);
  63.  
  64.   if (fd2 >= 0)
  65.     {
  66.       /* Give the ports each a user ref for the new descriptor.  */
  67.       __mach_port_mod_refs (__mach_task_self (), port,
  68.                 MACH_PORT_RIGHT_SEND, 1);
  69.       if (ctty != MACH_PORT_NULL)
  70.     __mach_port_mod_refs (__mach_task_self (), ctty,
  71.                   MACH_PORT_RIGHT_SEND, 1);
  72.     }
  73.  
  74.   _hurd_port_free (&d.d->port, port, &dealloc);
  75.   if (ctty != MACH_PORT_NULL)
  76.     _hurd_port_free (&d.d->ctty, ctty, &dealloc_ctty);
  77.  
  78.   _hurd_fd_done (d, &dealloc_dt);
  79.  
  80.   return fd2;
  81. }
  82.