1 /* stdio on a Mach device port.
2 Translates \n to \r\n on output.
4 Copyright (C) 1992, 1993 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. */
24 #include <device/device.h>
34 mach_msg_type_size_t nread;
37 if (f->__buffer == NULL)
45 to_read = f->__bufsize;
50 if (err = device_read_inband ((device_t) f->__cookie, 0, f->__target,
51 to_read, buffer, &nread))
54 f->__bufp = f->__get_limit = f->__put_limit = f->__buffer;
59 if (f->__buffer == NULL)
60 return (unsigned char) c;
62 f->__get_limit = f->__buffer + nread;
63 f->__bufp = f->__buffer;
64 f->__put_limit = f->__buffer + (f->__mode.__write ? f->__bufsize : 0);
65 return (unsigned char) *f->__bufp++;
70 output (FILE *f, int c)
72 inline void write_some (const char *p, size_t to_write)
78 e if (err = device_write_inband ((device_t) f->__cookie, 0,
79 f->__target, p, to_write, &wrote))
91 if (f->__buffer != NULL)
93 if (f->__put_limit == f->__buffer)
95 /* Prime the stream for writing. */
96 f->__put_limit = f->__buffer + f->__bufsize;
97 f->__bufp = f->__buffer;
100 *f->__bufp++ = (unsigned char) c;
106 /* Write out the buffer. */
108 write_some (start, f->__bufp - start);
110 f->__bufp = f->__buffer;
113 if (c != EOF && !ferror (f))
115 if (f->__linebuf && (unsigned char) c == '\n')
117 static const char nl = '\n';
121 *f->__bufp++ = (unsigned char) c;
127 #if 0 /* Translates \n to \r\n. */
129 output (FILE *f, int c)
131 void write_some (const char *p, size_t to_write)
137 if (err = device_write_inband ((device_t) f->__cookie, 0,
138 f->__target, p, to_write, &wrote))
146 f->__target += wrote;
149 void write_crlf (void)
151 static const char crlf[] = "\r\n";
152 write_some (crlf, 2);
155 if (f->__buffer == NULL)
157 /* The stream is unbuffered. */
163 char cc = (unsigned char) c;
170 if (f->__put_limit == f->__buffer)
172 /* Prime the stream for writing. */
173 f->__put_limit = f->__buffer + f->__bufsize;
174 f->__bufp = f->__buffer;
177 *f->__bufp++ = (unsigned char) c;
183 /* Search for newlines (LFs) in the buffer. */
185 char *start = f->__buffer, *p = start;
187 while (!ferror (f) && (p = memchr (p, '\n', f->__bufp - start)))
189 /* Found one. Replace it with a CR and write out through that CR. */
192 write_some (start, p + 1 - start);
194 /* Change it back to an LF; the next iteration will write it out
195 first thing. Start the next searching iteration one char later. */
201 /* Write the remainder of the buffer. */
204 write_some (start, f->__bufp - start);
207 f->__bufp = f->__buffer;
209 if (c != EOF && !ferror (f))
211 if (f->__linebuf && (unsigned char) c == '\n')
214 *f->__bufp++ = (unsigned char) c;
220 mach_open_devstream (mach_port_t dev, const char *mode)
222 FILE *stream = fopencookie ((void *) dev, mode, __default_io_functions);
226 stream->__room_funcs.__input = input;
227 stream->__room_funcs.__output = output;
228 stream->__io_funcs.__close = (__io_close_fn *) device_close;