Update.
[glibc.git] / sysdeps / mach / hurd / stdio_init.c
blobd353460a15f7539987c8fa3712a473944fdc442f
1 /* Copyright (C) 1995, 1997 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. */
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <hurd/fd.h>
24 #include <hurd/io.h>
25 #include <hurd/term.h>
27 /* Initialize STREAM as necessary.
28 This may change I/O functions, give a buffer, etc.
29 If no buffer is allocated, but the bufsize is set,
30 the bufsize will be used to allocate the buffer. */
31 void
32 __stdio_init_stream (stream)
33 FILE *stream;
35 struct hurd_fd *const d = stream->__cookie;
36 struct stat statb;
37 error_t err;
39 if (stream->__buffer != NULL || stream->__userbuf)
40 /* If's unbuffered by request, we can't do anything useful. */
41 return;
43 /* Find out what sort of file this is. */
44 if (err = HURD_FD_PORT_USE (d, __io_stat (port, &statb)))
45 return;
47 if (S_ISCHR (statb.st_mode))
49 /* It's a character device.
50 Make it line-buffered if it's a terminal. */
51 mach_port_t cttyid;
52 err = HURD_FD_PORT_USE (d, __term_getctty (port, &cttyid));
53 if (! err)
55 __mach_port_deallocate (__mach_task_self (), cttyid);
56 stream->__linebuf = 1;
60 /* Use the block-size field to determine
61 the system's optimal buffering size. */
62 stream->__bufsize = statb.st_blksize;