xref: /illumos-gate/usr/src/lib/libc/port/stdio/_findbuf.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * Copyright 2020 Robert Mustacchi
32  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
33  */
34 
35 
36 #define	_LARGEFILE64_SOURCE	1
37 
38 #include "lint.h"
39 #include "file64.h"
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include "stdiom.h"
46 
47 /*
48  * If buffer space has been pre-allocated use it otherwise malloc space.
49  * PUSHBACK causes the base pointer to be bumped forward. At least 4 bytes
50  * of pushback are required to meet international specifications.
51  * Extra space at the end of the buffer allows for synchronization problems.
52  * If malloc() fails stdio bails out; assumption being the system is in trouble.
53  * Associate a buffer with stream; return NULL on error.
54  */
55 Uchar *
56 _findbuf(FILE *iop)
57 {
58 	int fd = _get_fd(iop);
59 	Uchar *buf;
60 	int size = BUFSIZ;
61 	Uchar *endbuf;
62 	int tty, err;
63 
64 	/*
65 	 * isatty(3C) will set errno as a side-effect of returning 0. Hide this
66 	 * from callers so that they do not see errno changing for no apparent
67 	 * reason.
68 	 */
69 	err = errno;
70 	tty = isatty(fd);
71 	errno = err;
72 
73 	if (iop->_flag & _IONBF) {	/* need a small buffer, at least */
74 	trysmall:
75 		size = _SMBFSZ - PUSHBACK;
76 		if (fd >= 0 && fd < _NFILE) {
77 			buf = _smbuf[fd];
78 		} else if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) !=
79 		    NULL) {
80 			iop->_flag |= _IOMYBUF;
81 		}
82 	} else if (fd >= 0 && fd < 2 && tty != 0) {
83 		/* Use special buffers for standard in and standard out */
84 		buf = (fd == 0) ? _sibuf : _sobuf;
85 	} else {
86 
87 		/*
88 		 * The operating system can tell us the right size for a buffer;
89 		 * avoid 0-size buffers as returned for some special files
90 		 * (doors). Use the default buffer size for memory streams.
91 		 */
92 		struct stat64 stbuf;
93 
94 		if (fd != -1 && fstat64(fd, &stbuf) == 0 && stbuf.st_blksize >
95 		    0) {
96 			size = stbuf.st_blksize;
97 		}
98 
99 		if ((buf = (Uchar *)malloc(sizeof (Uchar)*(size+_SMBFSZ))) !=
100 		    NULL) {
101 			iop->_flag |= _IOMYBUF;
102 		} else {
103 			goto trysmall;
104 		}
105 	}
106 	if (buf == NULL)
107 		return (NULL);	/* malloc() failed */
108 	iop->_base = buf + PUSHBACK;	/* bytes for pushback */
109 	iop->_ptr = buf + PUSHBACK;
110 	endbuf = iop->_base + size;
111 	_setbufend(iop, endbuf);
112 	if (!(iop->_flag & _IONBF) && tty != 0)
113 		iop->_flag |= _IOLBF;
114 	return (endbuf);
115 }
116