* scripts/config.guess: Update from master, timestamp='2006-02-27'.
[glibc.git] / mach / devstream.c
blob1c51695a87683db9bcdd10f8513383cb1b822080
1 /* stdio on a Mach device port.
2 Translates \n to \r\n on output, echos and translates \r to \n on input.
3 Copyright (C) 1992,93,94,96,97,2000 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <stdio.h>
22 #include <mach.h>
23 #include <device/device.h>
24 #include <errno.h>
25 #include <string.h>
28 static ssize_t
29 devstream_write (void *cookie, const char *buffer, size_t n)
31 const device_t dev = (device_t) cookie;
33 int write_some (const char *p, size_t to_write)
35 kern_return_t err;
36 int wrote;
37 int thiswrite;
39 while (to_write > 0)
41 thiswrite = to_write;
42 if (thiswrite > IO_INBAND_MAX)
43 thiswrite = IO_INBAND_MAX;
45 if (err = device_write_inband (dev, 0, 0, p, thiswrite, &wrote))
47 errno = err;
48 return 0;
50 p += wrote;
51 to_write -= wrote;
53 return 1;
55 int write_crlf (void)
57 static const char crlf[] = "\r\n";
58 return write_some (crlf, 2);
61 /* Search for newlines (LFs) in the buffer. */
63 const char *start = buffer, *p;
64 while ((p = memchr (start, '\n', n)) != NULL)
66 /* Found one. Write out through the preceding character,
67 and then write a CR/LF pair. */
69 if ((p > start && !write_some (start, p - start))
70 || !write_crlf ())
71 return (start - buffer) ?: -1;
73 n -= p + 1 - start;
74 start = p + 1;
77 /* Write the remainder of the buffer. */
78 if (write_some (start, n))
79 start += n;
80 return (start - buffer) ?: -1;
83 static ssize_t
84 devstream_read (void *cookie, char *buffer, size_t to_read)
86 const device_t dev = (device_t) cookie;
88 kern_return_t err;
89 mach_msg_type_number_t nread = to_read;
91 err = device_read_inband (dev, 0, 0, to_read, buffer, &nread);
92 if (err)
94 errno = err;
95 return -1;
98 /* Translate CR to LF. */
100 char *p;
101 for (p = memchr (buffer, '\r', nread); p;
102 p = memchr (p + 1, '\r', (buffer + nread) - (p + 1)))
103 *p = '\n';
106 /* Echo back what we read. */
107 (void) devstream_write (cookie, buffer, nread);
109 return nread;
112 static int
113 dealloc_ref (void *cookie)
115 if (mach_port_deallocate (mach_task_self (), (mach_port_t) cookie))
117 errno = EINVAL;
118 return -1;
120 return 0;
123 #ifndef USE_IN_LIBIO
124 #define cookie_io_functions_t __io_functions
125 #define write __write
126 #define read __read
127 #define close __close
128 #endif
130 FILE *
131 mach_open_devstream (mach_port_t dev, const char *mode)
133 FILE *stream;
135 if (mach_port_mod_refs (mach_task_self (), dev, MACH_PORT_RIGHT_SEND, 1))
137 errno = EINVAL;
138 return NULL;
141 stream = fopencookie ((void *) dev, mode,
142 (cookie_io_functions_t) { write: devstream_write,
143 read: devstream_read,
144 close: dealloc_ref });
145 if (stream == NULL)
147 mach_port_deallocate (mach_task_self (), dev);
148 return NULL;
151 return stream;