1 /* Copyright (C) 1994-2023 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 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.
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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
23 /* Read up to N chars into BUF from COOKIE.
24 Return how many chars were read, 0 for EOF or -1 for error. */
26 readio (void *cookie
, char *buf
, size_t n
)
28 mach_msg_type_number_t nread
;
31 io_t io
= (io_t
) (uintptr_t) cookie
;
34 if (err
= __io_read (io
, &bufp
, &nread
, -1, n
))
35 return __hurd_fail (err
);
39 memcpy (buf
, bufp
, nread
);
40 __vm_deallocate (__mach_task_self (),
41 (vm_address_t
) bufp
, (vm_size_t
) nread
);
47 /* Write up to N chars from BUF to COOKIE.
48 Return how many chars were written or -1 for error. */
50 writeio (void *cookie
, const char *buf
, size_t n
)
54 io_t io
= (io_t
) (uintptr_t) cookie
;
56 if (err
= __io_write (io
, buf
, n
, -1, &wrote
))
57 return __hurd_fail (err
);
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. */
70 io_t io
= (io_t
) (uintptr_t) cookie
;
71 error_t err
= __io_seek (io
, *pos
, whence
, pos
);
72 return err
? __hurd_fail (err
) : 0;
75 /* Close the file associated with COOKIE.
76 Return 0 for success or -1 for failure. */
78 closeio (void *cookie
)
80 io_t io
= (io_t
) (uintptr_t) cookie
;
81 error_t error
= __mach_port_deallocate (__mach_task_self (),
84 return __hurd_fail (error
);
88 #include "../libio/libioP.h"
89 #define fopencookie _IO_fopencookie
90 static const cookie_io_functions_t funcsio
=
91 { readio
, writeio
, seekio
, closeio
};
94 /* Open a stream on PORT. MODE is as for fopen. */
97 __fopenport (mach_port_t port
, const char *mode
)
103 const char *m
= mode
;
114 needflags
= O_WRITE
|O_APPEND
;
119 if (m
[0] == '+' || (m
[0] == 'b' && m
[1] == '+'))
122 /* Verify the PORT is valid allows the access MODE specifies. */
124 if (err
= __io_get_openmodes (port
, &pflags
))
125 return __hurd_fail (err
), NULL
;
127 /* Check the access mode. */
128 if ((pflags
& needflags
) != needflags
)
129 return __hurd_fail (EBADF
), NULL
;
131 return fopencookie ((void *) (uintptr_t) port
,
134 weak_alias (__fopenport
, fopenport
)