]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/posix/stdio_init.c
Update to LGPL v2.1.
[thirdparty/glibc.git] / sysdeps / posix / stdio_init.c
1 /* Copyright (C) 1991, 1992, 1993, 1995, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
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)
30 {
31 const int fd = (int) stream->__cookie;
32 struct stat statb;
33
34 if (stream->__buffer != NULL || stream->__userbuf)
35 /* If's unbuffered by request, we can't do anything useful. */
36 return;
37
38 /* Find out what sort of file this is. */
39 if (__fstat (fd, &statb) < 0)
40 return;
41
42 if (S_ISCHR (statb.st_mode))
43 {
44 /* It's a character device.
45 Make it line-buffered if it's a terminal. */
46 if (__isatty (fd))
47 {
48 stream->__linebuf = 1;
49
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. */
59
60 stream->__io_funcs.__seek = NULL; /* Seeks get ESPIPE. */
61 }
62 }
63
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
69 }