Fix typo.
[glibc/history.git] / hurd / fopenport.c
blobb86f06db62dcd6d7852d03e4eda331e1cbbdecac
1 /* Copyright (C) 1994, 1995, 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 <hurd.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <string.h>
24 /* Read up to N chars into BUF from COOKIE.
25 Return how many chars were read, 0 for EOF or -1 for error. */
26 static ssize_t
27 readio (void *cookie, char *buf, size_t n)
29 mach_msg_type_number_t nread;
30 error_t err;
31 char *bufp = buf;
33 nread = n;
34 if (err = __io_read ((io_t) cookie, &bufp, &nread, -1, n))
35 return __hurd_fail (err);
37 if (bufp != buf)
39 memcpy (buf, bufp, nread);
40 __vm_deallocate (__mach_task_self (),
41 (vm_address_t) bufp, (vm_size_t) nread);
44 return nread;
47 /* Write up to N chars from BUF to COOKIE.
48 Return how many chars were written or -1 for error. */
49 static ssize_t
50 writeio (void *cookie, const char *buf, size_t n)
52 mach_msg_type_number_t wrote;
53 error_t err;
55 if (err = __io_write ((io_t) cookie, buf, n, -1, &wrote))
56 return __hurd_fail (err);
58 return wrote;
61 /* Move COOKIE's file position *POS bytes, according to WHENCE.
62 The current file position is stored in *POS.
63 Returns zero if successful, nonzero if not. */
64 static int
65 seekio (void *cookie, fpos_t *pos, int whence)
67 error_t error = __io_seek ((file_t) cookie, *pos, whence, pos);
68 if (error)
69 return __hurd_fail (error);
70 return 0;
73 /* Close the file associated with COOKIE.
74 Return 0 for success or -1 for failure. */
75 static int
76 closeio (void *cookie)
78 error_t error = __mach_port_deallocate (__mach_task_self (),
79 (mach_port_t) cookie);
80 if (error)
81 return __hurd_fail (error);
82 return 0;
85 static const __io_functions funcsio = { readio, writeio, seekio, closeio };
88 /* Defined in fopen.c. */
89 extern int __getmode (const char *mode, __io_mode *mptr);
92 /* Open a stream on PORT. MODE is as for fopen. */
94 FILE *
95 __fopenport (mach_port_t port, const char *mode)
97 register FILE *stream;
98 __io_mode m;
99 int pflags;
100 error_t err;
102 if (!__getmode (mode, &m))
103 return NULL;
105 /* Verify the PORT is valid allows the access MODE specifies. */
107 if (err = __io_get_openmodes (port, &pflags))
108 return __hurd_fail (err), NULL;
110 /* Check the access mode. */
111 if ((m.__read && !(pflags & O_READ)) || (m.__write && !(pflags & O_WRITE)))
113 errno = EBADF;
114 return NULL;
117 stream = __newstream ();
118 if (stream == NULL)
119 return NULL;
121 stream->__cookie = (void *) port;
122 stream->__mode = m;
123 stream->__io_funcs = funcsio;
124 stream->__room_funcs = __default_room_functions;
125 stream->__seen = 1;
127 return stream;
130 weak_alias (__fopenport, fopenport)