Update.
[glibc.git] / sysdeps / mach / hurd / sysd-stdio.c
blob9d5647392402d788c6e4b7d911ec5957e8ef15dd
1 /* Copyright (C) 1994, 1995, 1996, 1997 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <hurd.h>
23 #include <fcntl.h>
24 #include <hurd/fd.h>
26 /* Check ERR for wanting to generate a signal. */
28 int __stdio_fileno (void *);
30 static inline int
31 fd_fail (struct hurd_fd *fd, error_t err)
33 int signo = _hurd_fd_error_signal (err);
34 if (signo)
36 const struct hurd_signal_detail detail
37 = { code: __stdio_fileno (fd), error: err, exc: 0 };
38 _hurd_raise_signal (NULL, signo, &detail);
40 errno = err;
41 return -1;
45 /* Read up to N chars into BUF from COOKIE.
46 Return how many chars were read, 0 for EOF or -1 for error. */
47 ssize_t
48 __stdio_read (cookie, buf, n)
49 void *cookie;
50 char *buf;
51 size_t n;
53 error_t err;
54 struct hurd_fd *fd = cookie;
56 if (! fd)
57 return __hurd_fail (EBADF);
59 if (err = _hurd_fd_read (fd, buf, &n))
60 return fd_fail (fd, err);
62 return n;
65 /* Write up to N chars from BUF to COOKIE.
66 Return how many chars were written or -1 for error. */
67 ssize_t
68 __stdio_write (cookie, buf, n)
69 void *cookie;
70 const char *buf;
71 size_t n;
73 error_t err;
74 size_t wrote, nleft;
75 struct hurd_fd *fd = cookie;
77 if (! fd)
78 return __hurd_fail (EBADF);
80 nleft = n;
83 wrote = nleft;
84 if (err = _hurd_fd_write (fd, buf, &wrote))
85 return fd_fail (fd, err);
86 buf += wrote;
87 nleft -= wrote;
88 } while (nleft > 0);
90 return wrote;
93 /* Move COOKIE's file position *POS bytes, according to WHENCE.
94 The current file position is stored in *POS.
95 Returns zero if successful, nonzero if not. */
96 int
97 __stdio_seek (cookie, pos, whence)
98 void *cookie;
99 fpos_t *pos;
100 int whence;
102 error_t err;
103 struct hurd_fd *fd = cookie;
104 if (! fd)
105 return __hurd_fail (EBADF);
106 err = HURD_FD_PORT_USE (fd, __io_seek (port, *pos, whence, pos));
107 return err ? fd_fail (fd, err) : 0;
110 /* Close the file associated with COOKIE.
111 Return 0 for success or -1 for failure. */
113 __stdio_close (cookie)
114 void *cookie;
116 error_t error = cookie ? _hurd_fd_close (cookie) : EBADF;
117 return error ? fd_fail (cookie, error) : 0;
121 static inline int
122 modeflags (__io_mode m)
124 int flags = 0;
125 if (m.__read)
126 flags |= O_READ;
127 if (m.__write)
128 flags |= O_WRITE;
129 if (m.__append)
130 flags |= O_APPEND;
131 if (m.__create)
132 flags |= O_CREAT;
133 if (m.__truncate)
134 flags |= O_TRUNC;
135 if (m.__exclusive)
136 flags |= O_EXCL;
137 return flags;
140 /* Open FILENAME with the mode in M. */
142 __stdio_open (filename, m, cookieptr)
143 const char *filename;
144 __io_mode m;
145 void **cookieptr;
147 int flags;
148 file_t port;
149 struct hurd_fd *d;
151 flags = modeflags (m);
152 port = __file_name_lookup (filename, flags, 0666 & ~_hurd_umask);
153 if (port == MACH_PORT_NULL)
154 return -1;
156 HURD_CRITICAL_BEGIN;
157 d = _hurd_alloc_fd (NULL, 0);
158 if (d != NULL)
160 _hurd_port2fd (d, port, flags);
161 __spin_unlock (&d->port.lock);
163 HURD_CRITICAL_END;
165 *cookieptr = d;
166 return 0;
170 /* Open FILENAME with the mode in M. Use the same magic cookie
171 already in *COOKIEPTR if possible, closing the old cookie with CLOSEFN. */
173 __stdio_reopen (filename, m, cookieptr, closefn)
174 const char *filename;
175 __io_mode m;
176 void **cookieptr;
177 __io_close_fn closefn;
179 int flags;
180 file_t port;
181 struct hurd_fd *d;
183 if (closefn != __stdio_close)
185 /* The old cookie is Not Of The Body.
186 Just close it and do a normal open. */
187 (*closefn) (*cookieptr);
188 return __stdio_open (filename, m, cookieptr);
191 /* Open a new port on the file. */
192 flags = modeflags (m);
193 port = __file_name_lookup (filename, flags, 0666 & ~_hurd_umask);
195 /* Install the new port in the same file descriptor slot the old cookie
196 points to. If opening the file failed, PORT will be MACH_PORT_NULL
197 and installing it in the descriptor will have the effect of closing
198 the old descriptor. */
200 d = *cookieptr;
201 HURD_CRITICAL_BEGIN;
202 __spin_lock (&d->port.lock);
203 _hurd_port2fd (d, port, flags);
204 __spin_unlock (&d->port.lock);
205 HURD_CRITICAL_END;
207 return port == MACH_PORT_NULL ? -1 : 0;
211 /* Write a message to the error output.
212 Try hard to make it really get out. */
213 void
214 __stdio_errmsg (msg, len)
215 const char *msg;
216 size_t len;
218 io_t server;
219 mach_msg_type_number_t wrote;
221 server = __getdport (2);
222 __io_write (server, msg, len, -1, &wrote);
223 __mach_port_deallocate (__mach_task_self (), server);
227 /* Return the POSIX.1 file descriptor associated with COOKIE,
228 or -1 for errors. If COOKIE does not relate to any POSIX.1 file
229 descriptor, this should return -1 with errno set to EOPNOTSUPP. */
231 __stdio_fileno (cookie)
232 void *cookie;
234 int fd;
236 if (! cookie)
237 return __hurd_fail (EBADF);
239 __mutex_lock (&_hurd_dtable_lock);
240 for (fd = 0; fd < _hurd_dtablesize; ++fd)
241 if (_hurd_dtable[fd] == cookie)
243 __mutex_unlock (&_hurd_dtable_lock);
244 return fd;
246 __mutex_unlock (&_hurd_dtable_lock);
248 /* This should never happen, because this function should not be
249 installed as a stream's __fileno function unless that stream's cookie
250 points to a file descriptor. */
251 errno = EGRATUITOUS;
252 return -1;