update from main archive 961217
[glibc.git] / sysdeps / posix / stdio_init.c
blob422d9b69efaa905c4926481b9e58b291c80ee182
1 /* Copyright (C) 1991, 1992, 1993, 1995 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
25 /* Initialize STREAM as necessary.
26 This may change I/O functions, give a buffer, etc.
27 If no buffer is allocated, but the bufsize is set,
28 the bufsize will be used to allocate the buffer. */
29 void
30 DEFUN(__stdio_init_stream, (stream), FILE *stream)
32 register CONST int fd = (int) stream->__cookie;
33 struct stat statb;
35 if (stream->__buffer != NULL || stream->__userbuf)
36 /* If's unbuffered by request, we can't do anything useful. */
37 return;
39 /* Find out what sort of file this is. */
40 if (__fstat (fd, &statb) < 0)
41 return;
43 if (S_ISCHR (statb.st_mode))
45 /* It's a character device.
46 Make it line-buffered if it's a terminal. */
47 if (__isatty (fd))
49 stream->__linebuf = 1;
51 /* Unix terminal devices have the bad habit of claiming to be
52 seekable. On systems I have tried, seeking on a terminal
53 device seems to set its file position as specified, such that
54 a later tell says the same thing. This is in no way related
55 to actual seekability--the ability to seek back and read old
56 data. Unix terminal devices will let you "seek back", and
57 then read more new data from the terminal. I can think of
58 nothing to do about this lossage except to preemptively disable
59 seeking on terminal devices. */
61 stream->__io_funcs.__seek = NULL; /* Seeks get ESPIPE. */
65 #ifdef _STATBUF_ST_BLKSIZE
66 /* Use the block-size field to determine
67 the system's optimal buffering size. */
68 stream->__bufsize = statb.st_blksize;
69 #endif