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 / __close.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  1.7 KB  |  62 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 <unistd.h>
  22. #include <hurd.h>
  23.  
  24. /* Close the file descriptor FD.  */
  25. int
  26. DEFUN(__close, (fd), int fd)
  27. {
  28.   struct _hurd_fd *d;
  29.   io_t port, ctty;
  30.  
  31.   __mutex_lock (&_hurd_dtable_lock);
  32.   if (fd < 0 || fd >= _hurd_dtable.size)
  33.     {
  34.       __mutex_unlock (&_hurd_dtable_lock);
  35.       errno = EBADF;
  36.       return -1;
  37.     }
  38.  
  39.   d = &_hurd_dtable.d[fd];
  40.   __spin_lock (d->port);
  41.   port = d->port.port;
  42.   ctty = d->ctty.port;
  43.   d->port.port = d->ctty.port = MACH_PORT_NULL;
  44.   d->port.user_dealloc = d->ctty.user_dealloc = NULL;
  45.   d->flags = 0;
  46.   __spin_unlock (&d->port);
  47.  
  48.   __mutex_unlock (&_hurd_dtable_lock);
  49.  
  50.   if (port == MACH_PORT_NULL)
  51.     {
  52.       errno = EBADF;
  53.       return -1;
  54.     }
  55.  
  56.   __mach_port_deallocate (__mach_task_self (), port);
  57.   if (ctty != MACH_PORT_NULL)
  58.     __mach_port_deallocate (__mach_task_self (), ctty);
  59.  
  60.   return 0;
  61. }
  62.