1 /* stdio on a Mach device port.
2 Translates \n to \r on output.
4 Copyright (C) 1992 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
25 #include <mach/device.h>
35 if (f->__buffer == NULL)
43 to_read = f->__bufsize;
48 if (err = device_read_inband ((device_t) cookie, 0, f->__target, to_read,
52 f->__bufp = f->__get_limit = f->__put_limit = f->__buffer;
57 if (f->__buffer == NULL)
58 return (unsigned char) c;
60 f->__get_limit = f->__buffer + nread;
61 f->__bufp = f->__buffer;
62 f->__put_limit = f->__buffer + (f->__mode.__write ? f->__bufsize : 0);
63 return (unsigned char *) *f->__bufp++;
67 output (FILE *f, int c)
73 if (f->__buffer == NULL)
77 char cc = (unsigned char) c;
81 if ((err = device_write_inband ((device_t) f->__cookie, 0,
82 f->__target, &cc, 1, &wrote)) ||
93 if (f->__put_limit == f->__buffer)
95 f->__put_limit = f->__buffer + f->__bufsize;
96 f->__bufp = f->__buffer;
99 *f->__bufp++ = (unsigned char) c;
104 to_write = f->__bufp - f->__buffer;
108 char *p2 = memchr (p, '\n', to_write);
116 to_write = f->__bufp - f->__buffer;
121 if (err = device_write_inband ((device_t) f->__cookie, 0, f->__target,
122 p, to_write, &wrote))
130 f->__target += wrote;
133 f->__bufp = f->__buffer;
135 if (c != EOF && !ferror (f))
137 if (f->__linebuf && (unsigned char) c == '\n')
139 static const char nl = '\r';
140 if ((err = device_write_inband ((device_t) f->__cookie, 0,
141 f->__target, &nl, 1, &wrote)) ||
151 *f->__bufp++ = (unsigned char) c;
156 mach_open_devstream (device_t dev, const char *mode)
158 FILE *stream = fopencookie (dev, mode, __default_io_functions);
162 stream->__room_funcs.__input = input;
163 stream->__room_funcs.__output = output;
164 stream->__io_funcs.__close = device_close;