1 /* Copyright (C) 1994,95,97,2000,01 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. */
24 /* Read up to N chars into BUF from COOKIE.
25 Return how many chars were read, 0 for EOF or -1 for error. */
27 readio (void *cookie
, char *buf
, size_t n
)
29 mach_msg_type_number_t nread
;
34 if (err
= __io_read ((io_t
) cookie
, &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
)
52 mach_msg_type_number_t wrote
;
55 if (err
= __io_write ((io_t
) cookie
, buf
, n
, -1, &wrote
))
56 return __hurd_fail (err
);
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. */
76 /* XXX We don't really support large files on the Hurd. So if POS
77 doesn't fit in an `off_t', we'll return `-1' and set errno. EOVERFLOW
78 probably isn't the right error value, but seems appropriate here. */
79 if ((off_t
) *pos
!= *pos
)
81 __set_errno (EOVERFLOW
);
85 error
= __io_seek ((file_t
) cookie
, *pos
, whence
, &res
);
87 return __hurd_fail (error
);
92 /* Close the file associated with COOKIE.
93 Return 0 for success or -1 for failure. */
95 closeio (void *cookie
)
97 error_t error
= __mach_port_deallocate (__mach_task_self (),
98 (mach_port_t
) cookie
);
100 return __hurd_fail (error
);
105 #define cookie_io_functions_t __io_functions
107 static const cookie_io_functions_t funcsio
=
108 { readio
, writeio
, seekio
, closeio
};
111 /* Open a stream on PORT. MODE is as for fopen. */
114 fopenport (mach_port_t port
, const char *mode
)
120 const char *m
= mode
;
131 needflags
= O_WRITE
|O_APPEND
;
136 if (m
[0] == '+' || (m
[0] == 'b' && m
[1] == '+'))
139 /* Verify the PORT is valid allows the access MODE specifies. */
141 if (err
= __io_get_openmodes (port
, &pflags
))
142 return __hurd_fail (err
), NULL
;
144 /* Check the access mode. */
145 if ((pflags
& needflags
) != needflags
)
151 return fopencookie ((void *) port
, mode
, funcsio
);