Update.
[glibc.git] / sysdeps / posix / stdio_init.c
blob4dc7028133eb7e73f9fc83c74b4c2d71aa53d5de
1 /* Copyright (C) 1991, 1992, 1993, 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>
24 /* Initialize STREAM as necessary.
25 This may change I/O functions, give a buffer, etc.
26 If no buffer is allocated, but the bufsize is set,
27 the bufsize will be used to allocate the buffer. */
28 void
29 __stdio_init_stream (FILE *stream)
31 const int fd = (int) stream->__cookie;
32 struct stat statb;
34 if (stream->__buffer != NULL || stream->__userbuf)
35 /* If's unbuffered by request, we can't do anything useful. */
36 return;
38 /* Find out what sort of file this is. */
39 if (__fstat (fd, &statb) < 0)
40 return;
42 if (S_ISCHR (statb.st_mode))
44 /* It's a character device.
45 Make it line-buffered if it's a terminal. */
46 if (__isatty (fd))
48 stream->__linebuf = 1;
50 /* Unix terminal devices have the bad habit of claiming to be
51 seekable. On systems I have tried, seeking on a terminal
52 device seems to set its file position as specified, such that
53 a later tell says the same thing. This is in no way related
54 to actual seekability--the ability to seek back and read old
55 data. Unix terminal devices will let you "seek back", and
56 then read more new data from the terminal. I can think of
57 nothing to do about this lossage except to preemptively disable
58 seeking on terminal devices. */
60 stream->__io_funcs.__seek = NULL; /* Seeks get ESPIPE. */
64 #ifdef _STATBUF_ST_BLKSIZE
65 /* Use the block-size field to determine
66 the system's optimal buffering size. */
67 stream->__bufsize = statb.st_blksize;
68 #endif