1 /* Copyright (C) 1994, 1995 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
25 /* Read up to N chars into BUF from COOKIE.
26 Return how many chars were read, 0 for EOF or -1 for error. */
28 readio (void *cookie
, char *buf
, size_t n
)
30 mach_msg_type_number_t nread
;
35 if (err
= __io_read ((io_t
) cookie
, &bufp
, &nread
, -1, n
))
36 return __hurd_fail (err
);
40 memcpy (buf
, bufp
, nread
);
41 __vm_deallocate (__mach_task_self (),
42 (vm_address_t
) bufp
, (vm_size_t
) nread
);
48 /* Write up to N chars from BUF to COOKIE.
49 Return how many chars were written or -1 for error. */
51 writeio (void *cookie
, const char *buf
, size_t n
)
53 mach_msg_type_number_t wrote
;
56 if (err
= __io_write ((io_t
) cookie
, 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. */
66 seekio (void *cookie
, fpos_t *pos
, int whence
)
68 error_t error
= __io_seek ((file_t
) cookie
, *pos
, whence
, pos
);
70 return __hurd_fail (error
);
74 /* Close the file associated with COOKIE.
75 Return 0 for success or -1 for failure. */
77 closeio (void *cookie
)
79 error_t error
= __mach_port_deallocate (__mach_task_self (),
80 (mach_port_t
) cookie
);
82 return __hurd_fail (error
);
86 static const __io_functions funcsio
= { readio
, writeio
, seekio
, closeio
};
89 /* Defined in fopen.c. */
90 extern int EXFUN(__getmode
, (CONST
char *mode
, __io_mode
*mptr
));
93 /* Open a stream on PORT. MODE is as for fopen. */
96 __fopenport (mach_port_t port
, const char *mode
)
98 register FILE *stream
;
103 if (!__getmode (mode
, &m
))
106 /* Verify the PORT is valid allows the access MODE specifies. */
108 if (err
= __io_get_openmodes (port
, &pflags
))
109 return __hurd_fail (err
), NULL
;
111 /* Check the access mode. */
112 if ((m
.__read
&& !(pflags
& O_READ
)) || (m
.__write
&& !(pflags
& O_WRITE
)))
118 stream
= __newstream ();
122 stream
->__cookie
= (PTR
) port
;
124 stream
->__io_funcs
= funcsio
;
125 stream
->__room_funcs
= __default_room_functions
;
131 weak_alias (__fopenport
, fopenport
)