xref: /titanic_41/usr/src/ucblib/libucb/port/gen/setbuffer.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include <sys/types.h>
45 #include "file64.h"
46 #include <stdio.h>
47 #include "stdiom.h"
48 #include <stdlib.h>
49 
50 extern Uchar _smbuf[][_NFILE];
51 
52 void
53 setbuffer(FILE *iop, char *abuf, int asize)
54 {
55 	Uchar *buf = (Uchar *)abuf;
56 	int fno = iop->_file;  /* file number */
57 	int size = asize - _SMBFSZ;
58 	Uchar *temp;
59 
60 	if (iop->_base != 0 && iop->_flag & _IOMYBUF)
61 		free((char *)iop->_base - PUSHBACK);
62 	iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
63 	if (buf == 0) {
64 		iop->_flag |= _IONBF;
65 #ifndef _STDIO_ALLOCATE
66 		if (fno < 2) {
67 			/* use special buffer for std{in,out} */
68 			buf = (fno == 0) ? _sibuf : _sobuf;
69 			size = BUFSIZ - _SMBFSZ;
70 		} else /* needed for ifdef */
71 #endif
72 		if (fno < _NFILE) {
73 			buf = _smbuf[fno];
74 			size = _SMBFSZ - PUSHBACK;
75 		} else if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) !=
76 		    0) {
77 			iop->_flag |= _IOMYBUF;
78 			size = _SMBFSZ - PUSHBACK;
79 		}
80 	} else /* regular buffered I/O, specified buffer size */ {
81 		if (size <= 0)
82 			return;
83 	}
84 	if (buf == 0)
85 		return; /* malloc() failed */
86 	temp = buf + PUSHBACK;
87 	iop->_base = temp;
88 	_setbufend(iop, temp + size);
89 	iop->_ptr = temp;
90 	iop->_cnt = 0;
91 }
92 
93 /*
94  * set line buffering
95  */
96 
97 int
98 setlinebuf(FILE *iop)
99 {
100 	char *buf;
101 
102 	(void) fflush(iop);
103 	setbuffer(iop, (char *)NULL, 0);
104 	buf = (char *)malloc(128);
105 	if (buf != NULL) {
106 		setbuffer(iop, buf, 128);
107 		iop->_flag |= _IOLBF|_IOMYBUF;
108 	}
109 	return (0);	/* returns no useful value, keep the same prototype */
110 }
111