xref: /illumos-gate/usr/src/lib/libc/port/stdio/_findbuf.c (revision 31aa620247ae407b2bee2dccd71693d1938f54d6)
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  */
33 
34 
35 #define	_LARGEFILE64_SOURCE	1
36 
37 #include "lint.h"
38 #include "file64.h"
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include "stdiom.h"
44 
45 /*
46  * If buffer space has been pre-allocated use it otherwise malloc space.
47  * PUSHBACK causes the base pointer to be bumped forward. At least 4 bytes
48  * of pushback are required to meet international specifications.
49  * Extra space at the end of the buffer allows for synchronization problems.
50  * If malloc() fails stdio bails out; assumption being the system is in trouble.
51  * Associate a buffer with stream; return NULL on error.
52  */
53 Uchar *
54 _findbuf(FILE *iop)
55 {
56 	int fd = _get_fd(iop);
57 	Uchar *buf;
58 	int size = BUFSIZ;
59 	Uchar *endbuf;
60 	int tty = -1;
61 
62 	if (iop->_flag & _IONBF) {	/* need a small buffer, at least */
63 	trysmall:
64 		size = _SMBFSZ - PUSHBACK;
65 		if (fd >= 0 && fd < _NFILE) {
66 			buf = _smbuf[fd];
67 		} else if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) !=
68 		    NULL) {
69 			iop->_flag |= _IOMYBUF;
70 		}
71 	} else if (fd >= 0 && fd < 2 && (tty = isatty(fd))) {
72 		/* Use special buffers for standard in and standard out */
73 		buf = (fd == 0) ? _sibuf : _sobuf;
74 	} else {
75 
76 		/*
77 		 * The operating system can tell us the right size for a buffer;
78 		 * avoid 0-size buffers as returned for some special files
79 		 * (doors). Use the default buffer size for memory streams.
80 		 */
81 		struct stat64 stbuf;
82 
83 		if (fd != -1 && fstat64(fd, &stbuf) == 0 && stbuf.st_blksize >
84 		    0) {
85 			size = stbuf.st_blksize;
86 		}
87 
88 		if ((buf = (Uchar *)malloc(sizeof (Uchar)*(size+_SMBFSZ))) !=
89 		    NULL) {
90 			iop->_flag |= _IOMYBUF;
91 		} else {
92 			goto trysmall;
93 		}
94 	}
95 	if (buf == NULL)
96 		return (NULL);	/* malloc() failed */
97 	iop->_base = buf + PUSHBACK;	/* bytes for pushback */
98 	iop->_ptr = buf + PUSHBACK;
99 	endbuf = iop->_base + size;
100 	_setbufend(iop, endbuf);
101 	if (!(iop->_flag & _IONBF) && ((tty != -1) ? tty : isatty(fd)))
102 		iop->_flag |= _IOLBF;
103 	return (endbuf);
104 }
105