Fix typos; move stray paragraph to Feature Test Macros.
[glibc/history.git] / sysdeps / mach / hurd / open.c
blob0425b15a3f0b3fe60fbca58402210f4302ca9300
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdarg.h>
23 #include <hurd.h>
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)
30 error_t err;
31 mode_t mode;
32 int fl;
33 int fd;
34 file_t port;
36 switch (oflag & O_ACCMODE)
38 case O_RDONLY:
39 fl = FS_LOOKUP_READ;
40 break;
41 case O_WRONLY:
42 fl = FS_LOOKUP_WRITE;
43 break;
44 case O_RDWR:
45 fl = FS_LOOKUP_READ | FS_LOOKUP_WRITE;
46 break;
47 default:
48 errno = EINVAL;
49 return -1;
52 if (oflag & O_CREAT)
54 va_list arg;
55 va_start (arg, oflag);
56 mode = va_arg (arg, mode_t);
57 va_end (arg);
58 fl |= FS_LOOKUP_CREATE;
60 else
61 mode = 0;
63 if (oflag & O_NDELAY)
64 fl |= FS_LOOKUP_NDELAY;
65 if (oflag & O_APPEND)
66 fl |= FS_LOOKUP_APPEND;
67 if (oflag & O_CREATE)
68 fl |= FS_LOOKUP_CREATE;
69 if (oflag & O_TRUNC)
70 fl |= FS_LOOKUP_TRUNC;
71 if (oflag & O_EXCL)
72 fl |= FS_LOOKUP_EXCL;
74 __mutex_lock (&_hurd_dtable.lock);
75 fd = _hurd_dalloc ();
76 if (fd < 0)
78 __mutex_unlock (&_hurd_dtable.lock);
79 return -1;
82 port = __hurd_path_lookup (file, fl, mode);
84 if (port != MACH_PORT_NULL && !_hurd_hasctty && !(oflag & O_NOCTTY))
86 mach_port_t cttyid;
87 io_statbuf_t stb;
89 err = __io_stat (port, &stb);
90 if (!err)
91 err = __term_getctty (port, &cttyid);
92 if (!err)
94 err = __proc_set_ctty (_hurd_proc, cttyid);
95 __mach_port_deallocate (__mach_task_self (), cttyid);
96 if (!err)
98 _hurd_dtable.d[fd].isctty = 1;
99 _hurd_hasctty = 1;
100 _hurd_ctty_fstype = stb.stb_fstype;
101 _hurd_ctty_fsid = stb.stb_fsid;
102 _hurd_ctty_fileid = stb.stb_file_id;
107 _hurd_dtable.d[fd].server = port;
108 __mutex_unlock (&_hurd_dtable.lock);
109 return port == MACH_PORT_NULL ? -1 : fd;