update from main archive 970210
[glibc.git] / libio / filedoalloc.c
blob836c56b63b0b55cad94fa94f0fda9bd90185249e
1 /*
2 Copyright (C) 1993 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
26 * Copyright (c) 1990 The Regents of the University of California.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms are permitted
30 * provided that the above copyright notice and this paragraph are
31 * duplicated in all such forms and that any documentation,
32 * advertising materials, and other materials related to such
33 * distribution and use acknowledge that the software was developed
34 * by the University of California, Berkeley. The name of the
35 * University may not be used to endorse or promote products derived
36 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
39 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42 /* Modified for GNU iostream by Per Bothner 1991, 1992. */
44 #ifndef _POSIX_SOURCE
45 # define _POSIX_SOURCE
46 #endif
47 #include "libioP.h"
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #ifdef __STDC__
51 #include <stdlib.h>
52 #include <unistd.h>
53 #endif
55 #ifdef _LIBC
56 # undef isatty
57 # define isatty(Fd) __isatty (Fd)
58 #endif
61 * Allocate a file buffer, or switch to unbuffered I/O.
62 * Per the ANSI C standard, ALL tty devices default to line buffered.
64 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
65 * optimisation) right after the _fstat() that finds the buffer size.
68 int
69 DEFUN(_IO_file_doallocate, (fp),
70 register _IO_FILE *fp)
72 register _IO_size_t size;
73 int couldbetty;
74 register char *p;
75 struct stat st;
77 #ifndef _LIBC
78 /* If _IO_cleanup_registration_needed is non-zero, we should call the
79 function it points to. This is to make sure _IO_cleanup gets called
80 on exit. We call it from _IO_file_doallocate, since that is likely
81 to get called by any program that does buffered I/O. */
82 if (_IO_cleanup_registration_needed)
83 (*_IO_cleanup_registration_needed)();
84 #endif
86 if (fp->_fileno < 0 || _IO_SYSSTAT (fp, &st) < 0)
88 couldbetty = 0;
89 size = _IO_BUFSIZ;
90 #if 0
91 /* do not try to optimise fseek() */
92 fp->_flags |= __SNPT;
93 #endif
95 else
97 couldbetty = S_ISCHR(st.st_mode);
98 #if _IO_HAVE_ST_BLKSIZE
99 size = st.st_blksize <= 0 ? _IO_BUFSIZ : st.st_blksize;
100 #else
101 size = _IO_BUFSIZ;
102 #endif
104 ALLOC_BUF(p, size, EOF);
105 _IO_setb(fp, p, p+size, 1);
106 if (couldbetty && isatty(fp->_fileno))
107 fp->_flags |= _IO_LINE_BUF;
108 return 1;