]>
Commit | Line | Data |
---|---|---|
f7a9f785 | 1 | /* Copyright (C) 1994-2016 Free Software Foundation, Inc. |
c84142e8 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
c84142e8 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
c84142e8 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
28f540f4 RM |
18 | #include <hurd.h> |
19 | #include <stdio.h> | |
20 | #include <fcntl.h> | |
21 | #include <string.h> | |
22 | ||
23 | /* Read up to N chars into BUF from COOKIE. | |
24 | Return how many chars were read, 0 for EOF or -1 for error. */ | |
25 | static ssize_t | |
26 | readio (void *cookie, char *buf, size_t n) | |
27 | { | |
28 | mach_msg_type_number_t nread; | |
29 | error_t err; | |
30 | char *bufp = buf; | |
31 | ||
32 | nread = n; | |
33 | if (err = __io_read ((io_t) cookie, &bufp, &nread, -1, n)) | |
34 | return __hurd_fail (err); | |
35 | ||
36 | if (bufp != buf) | |
37 | { | |
38 | memcpy (buf, bufp, nread); | |
39 | __vm_deallocate (__mach_task_self (), | |
40 | (vm_address_t) bufp, (vm_size_t) nread); | |
41 | } | |
42 | ||
43 | return nread; | |
44 | } | |
45 | ||
46 | /* Write up to N chars from BUF to COOKIE. | |
47 | Return how many chars were written or -1 for error. */ | |
48 | static ssize_t | |
49 | writeio (void *cookie, const char *buf, size_t n) | |
50 | { | |
51 | mach_msg_type_number_t wrote; | |
52 | error_t err; | |
53 | ||
54 | if (err = __io_write ((io_t) cookie, buf, n, -1, &wrote)) | |
55 | return __hurd_fail (err); | |
56 | ||
57 | return wrote; | |
58 | } | |
59 | ||
60 | /* Move COOKIE's file position *POS bytes, according to WHENCE. | |
61 | The current file position is stored in *POS. | |
62 | Returns zero if successful, nonzero if not. */ | |
63 | static int | |
8a0746ae | 64 | seekio (void *cookie, |
8a0746ae | 65 | _IO_off64_t *pos, |
8a0746ae | 66 | int whence) |
28f540f4 | 67 | { |
337738b7 RM |
68 | error_t err = __io_seek ((file_t) cookie, *pos, whence, pos); |
69 | return err ? __hurd_fail (err) : 0; | |
28f540f4 RM |
70 | } |
71 | ||
72 | /* Close the file associated with COOKIE. | |
73 | Return 0 for success or -1 for failure. */ | |
74 | static int | |
75 | closeio (void *cookie) | |
76 | { | |
77 | error_t error = __mach_port_deallocate (__mach_task_self (), | |
78 | (mach_port_t) cookie); | |
79 | if (error) | |
80 | return __hurd_fail (error); | |
81 | return 0; | |
82 | } | |
83 | ||
27114e20 RM |
84 | #include "../libio/libioP.h" |
85 | #define fopencookie _IO_fopencookie | |
418f095a RM |
86 | static const cookie_io_functions_t funcsio = |
87 | { readio, writeio, seekio, closeio }; | |
28f540f4 RM |
88 | \f |
89 | ||
28f540f4 RM |
90 | /* Open a stream on PORT. MODE is as for fopen. */ |
91 | ||
92 | FILE * | |
27114e20 | 93 | __fopenport (mach_port_t port, const char *mode) |
28f540f4 | 94 | { |
28f540f4 | 95 | int pflags; |
418f095a | 96 | int needflags; |
28f540f4 RM |
97 | error_t err; |
98 | ||
418f095a RM |
99 | const char *m = mode; |
100 | ||
101 | switch (*m++) | |
102 | { | |
103 | case 'r': | |
104 | needflags = O_READ; | |
105 | break; | |
106 | case 'w': | |
107 | needflags = O_WRITE; | |
108 | break; | |
109 | case 'a': | |
110 | needflags = O_WRITE|O_APPEND; | |
111 | break; | |
112 | default: | |
113 | return NULL; | |
114 | } | |
115 | if (m[0] == '+' || (m[0] == 'b' && m[1] == '+')) | |
116 | needflags |= O_RDWR; | |
28f540f4 RM |
117 | |
118 | /* Verify the PORT is valid allows the access MODE specifies. */ | |
119 | ||
120 | if (err = __io_get_openmodes (port, &pflags)) | |
121 | return __hurd_fail (err), NULL; | |
122 | ||
123 | /* Check the access mode. */ | |
418f095a | 124 | if ((pflags & needflags) != needflags) |
28f540f4 RM |
125 | { |
126 | errno = EBADF; | |
127 | return NULL; | |
128 | } | |
129 | ||
418f095a | 130 | return fopencookie ((void *) port, mode, funcsio); |
28f540f4 | 131 | } |
27114e20 | 132 | weak_alias (__fopenport, fopenport) |