]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1994-2024 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 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://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; | |
62d6c330 | 31 | io_t io = (io_t) (uintptr_t) cookie; |
28f540f4 RM |
32 | |
33 | nread = n; | |
62d6c330 | 34 | if (err = __io_read (io, &bufp, &nread, -1, n)) |
28f540f4 RM |
35 | return __hurd_fail (err); |
36 | ||
37 | if (bufp != buf) | |
38 | { | |
39 | memcpy (buf, bufp, nread); | |
40 | __vm_deallocate (__mach_task_self (), | |
41 | (vm_address_t) bufp, (vm_size_t) nread); | |
42 | } | |
43 | ||
44 | return nread; | |
45 | } | |
46 | ||
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) | |
51 | { | |
063f7462 | 52 | vm_size_t wrote; |
28f540f4 | 53 | error_t err; |
62d6c330 | 54 | io_t io = (io_t) (uintptr_t) cookie; |
28f540f4 | 55 | |
62d6c330 | 56 | if (err = __io_write (io, buf, n, -1, &wrote)) |
28f540f4 RM |
57 | return __hurd_fail (err); |
58 | ||
59 | return wrote; | |
60 | } | |
61 | ||
62 | /* Move COOKIE's file position *POS bytes, according to WHENCE. | |
63 | The current file position is stored in *POS. | |
64 | Returns zero if successful, nonzero if not. */ | |
65 | static int | |
8a0746ae | 66 | seekio (void *cookie, |
9964a145 | 67 | off64_t *pos, |
8a0746ae | 68 | int whence) |
28f540f4 | 69 | { |
62d6c330 SB |
70 | io_t io = (io_t) (uintptr_t) cookie; |
71 | error_t err = __io_seek (io, *pos, whence, pos); | |
337738b7 | 72 | return err ? __hurd_fail (err) : 0; |
28f540f4 RM |
73 | } |
74 | ||
75 | /* Close the file associated with COOKIE. | |
76 | Return 0 for success or -1 for failure. */ | |
77 | static int | |
78 | closeio (void *cookie) | |
79 | { | |
62d6c330 | 80 | io_t io = (io_t) (uintptr_t) cookie; |
28f540f4 | 81 | error_t error = __mach_port_deallocate (__mach_task_self (), |
62d6c330 | 82 | io); |
28f540f4 RM |
83 | if (error) |
84 | return __hurd_fail (error); | |
85 | return 0; | |
86 | } | |
87 | ||
27114e20 RM |
88 | #include "../libio/libioP.h" |
89 | #define fopencookie _IO_fopencookie | |
418f095a RM |
90 | static const cookie_io_functions_t funcsio = |
91 | { readio, writeio, seekio, closeio }; | |
28f540f4 RM |
92 | \f |
93 | ||
28f540f4 RM |
94 | /* Open a stream on PORT. MODE is as for fopen. */ |
95 | ||
96 | FILE * | |
27114e20 | 97 | __fopenport (mach_port_t port, const char *mode) |
28f540f4 | 98 | { |
28f540f4 | 99 | int pflags; |
418f095a | 100 | int needflags; |
28f540f4 RM |
101 | error_t err; |
102 | ||
418f095a RM |
103 | const char *m = mode; |
104 | ||
105 | switch (*m++) | |
106 | { | |
107 | case 'r': | |
108 | needflags = O_READ; | |
109 | break; | |
110 | case 'w': | |
111 | needflags = O_WRITE; | |
112 | break; | |
113 | case 'a': | |
114 | needflags = O_WRITE|O_APPEND; | |
115 | break; | |
116 | default: | |
117 | return NULL; | |
118 | } | |
119 | if (m[0] == '+' || (m[0] == 'b' && m[1] == '+')) | |
120 | needflags |= O_RDWR; | |
28f540f4 RM |
121 | |
122 | /* Verify the PORT is valid allows the access MODE specifies. */ | |
123 | ||
124 | if (err = __io_get_openmodes (port, &pflags)) | |
125 | return __hurd_fail (err), NULL; | |
126 | ||
127 | /* Check the access mode. */ | |
418f095a | 128 | if ((pflags & needflags) != needflags) |
9ec31e57 | 129 | return __hurd_fail (EBADF), NULL; |
28f540f4 | 130 | |
62d6c330 SB |
131 | return fopencookie ((void *) (uintptr_t) port, |
132 | mode, funcsio); | |
28f540f4 | 133 | } |
27114e20 | 134 | weak_alias (__fopenport, fopenport) |