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 / __open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  3.3 KB  |  131 lines

  1. /* Copyright (C) 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 <stdarg.h>
  23. #include <hurd.h>
  24.  
  25. /* Open FILE with access OFLAG.  If OFLAG includes O_CREAT,
  26.    a third argument is the file protection.  */
  27. int
  28. DEFUN(__open, (file, oflag), CONST char *file AND int oflag DOTS)
  29. {
  30.   error_t err;
  31.   mode_t mode;
  32.   int fl;
  33.   io_t port, ctty;
  34.  
  35.   switch (oflag & O_ACCMODE)
  36.     {
  37.     case O_RDONLY:
  38.       fl = FS_LOOKUP_READ;
  39.       break;
  40.     case O_WRONLY:
  41.       fl = FS_LOOKUP_WRITE;
  42.       break;
  43.     case O_RDWR:
  44.       fl = FS_LOOKUP_READ | FS_LOOKUP_WRITE;
  45.       break;
  46.     default:
  47.       errno = EINVAL;
  48.       return -1;
  49.     }
  50.  
  51.   if (oflag & O_CREAT)
  52.     {
  53.       va_list arg;
  54.       va_start (arg, oflag);
  55.       mode = va_arg (arg, mode_t);
  56.       va_end (arg);
  57.       fl |= FS_LOOKUP_CREATE;
  58.     }
  59.   else
  60.     mode = 0;
  61.  
  62.   if (oflag & O_NDELAY)
  63.     fl |= FS_LOOKUP_NDELAY;
  64.   if (oflag & O_APPEND)
  65.     fl |= FS_LOOKUP_APPEND;
  66.   if (oflag & O_CREATE)
  67.     fl |= FS_LOOKUP_CREATE;
  68.   if (oflag & O_TRUNC)
  69.     fl |= FS_LOOKUP_TRUNC;
  70.   if (oflag & O_EXCL)
  71.     fl |= FS_LOOKUP_EXCL;
  72.  
  73.   port = __path_lookup (file, fl, mode);
  74.   if (port == MACH_PORT_NULL)
  75.     return -1;
  76.  
  77.   ctty = MACH_PORT_NULL;
  78.   if (!(oflag & O_NOCTTY))
  79.     {
  80.       io_statbuf_t stb;
  81.       
  82.       err = __io_stat (port, &stb);
  83.       if (!err)
  84.     {
  85.       io_t fg_port = MACH_PORT_NULL;
  86.  
  87.       __mutex_lock (&_hurd_ctty_lock);
  88.       if (_hurd_ctty_fstype == 0)
  89.         {
  90.           /* We have no controlling tty.
  91.          Try to make this it.  */
  92.           mach_port_t cttyid;
  93.           err = __term_getctty (port, &cttyid);
  94.           if (!err)
  95.         {
  96.           err = __term_become_ctty (port, _hurd_pid, _hurd_pgrp,
  97.                         _hurd_sigport, &fg_port);
  98.           if (err)
  99.             __mach_port_deallocate (__mach_task_self (), cttyid);
  100.         }
  101.         }
  102.       else if (stb.stb_fstype == _hurd_ctty_fstype &&
  103.            stb.stb_fsid.val[0] == _hurd_ctty_fsid.val[0] &&
  104.            stb.stb_fsid.val[1] == _hurd_ctty_fsid.val[1] &&
  105.            stb.stb_fileno == _hurd_ctty_fileno)
  106.         /* This is our controlling tty.  */
  107.         err = __term_become_ctty (port, _hurd_pid, _hurd_pgrp,
  108.                       _hurd_sigport, &fg_port);
  109.  
  110.       if (fg_port != MACH_PORT_NULL)
  111.         {
  112.           int fd = _hurd_dalloc (fg_port, port, 0);
  113.           if (fd >= 0 && _hurd_ctty_fstype == 0)
  114.         {
  115.           /* We have a new controlling tty.  */
  116.           _hurd_ctty_port = cttyid;
  117.           _hurd_ctty_fstype = stb.stb_fstype;
  118.           _hurd_ctty_fsid = stb.stb_fsid;
  119.           _hurd_ctty_fileno = stb.stb_fileno;
  120.         }
  121.           __mutex_unlock (&_hurd_ctty_lock);
  122.           return fd;
  123.         }
  124.       else
  125.         __mutex_unlock (&_hurd_ctty_lock);
  126.     }
  127.     }
  128.  
  129.   return _hurd_dalloc (port, MACH_PORT_NULL, 0);
  130. }
  131.