xref: /illumos-gate/usr/src/cmd/lp/lib/msgs/fifo_buffs.c (revision 7c478bd9)
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 1997 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3	*/
32 /* LINTLIBRARY */
33 
34 
35 #include	<errno.h>
36 #include	<fcntl.h>
37 #include	<stdio.h>
38 #include	"lp.h"
39 #include	"msgs.h"
40 
41 static	fifobuffer_t	**FifoBufferTable	= NULL;
42 static	int		FifoBufferTableSize	= 0;
43 
44 /*
45 **	Local functions
46 */
47 static	int		InitFifoBufferTable (void);
48 static	int		GrowFifoBufferTable (int);
49 static	fifobuffer_t	*NewFifoBuffer (int);
50 
51 
52 int
ResetFifoBuffer(int fd)53 ResetFifoBuffer(int fd)
54 {
55 	if ((!FifoBufferTableSize) && (InitFifoBufferTable () < 0))
56 		return	-1;
57 
58 	if (fd >= FifoBufferTableSize)
59 		return	0;
60 
61 	if (FifoBufferTable [fd]) {
62 		FifoBufferTable [fd]->full = 0;
63 		FifoBufferTable [fd]->psave =
64 		FifoBufferTable [fd]->psave_end =
65 			FifoBufferTable [fd]->save;
66 	}
67 	return	0;
68 }
69 
70 
71 fifobuffer_t *
GetFifoBuffer(int fd)72 GetFifoBuffer(int fd)
73 {
74 	if (fd < 0) {
75 		errno = EINVAL;
76 		return	NULL;
77 	}
78 	if ((fd >= FifoBufferTableSize) && (GrowFifoBufferTable (fd) < 0))
79 		return	NULL;
80 
81 	if (!FifoBufferTable [fd]) {
82 		if (!NewFifoBuffer (fd))
83 			return	NULL;
84 
85 		FifoBufferTable [fd]->full = 0;
86 		FifoBufferTable [fd]->psave =
87 		FifoBufferTable [fd]->psave_end =
88 			FifoBufferTable [fd]->save;
89 	}
90 
91 	return	FifoBufferTable [fd];
92 }
93 
94 
95 static	int
InitFifoBufferTable()96 InitFifoBufferTable()
97 {
98 	if (FifoBufferTableSize)
99 		return	0;
100 
101 	FifoBufferTable = (fifobuffer_t **)
102 		Calloc (100, sizeof (fifobuffer_t *));
103 	if (!FifoBufferTable)
104 		return	-1;	/* ENOMEM is already set. */
105 
106 	FifoBufferTableSize = 100;
107 
108 	return	0;
109 }
110 
111 
112 static int
GrowFifoBufferTable(int fd)113 GrowFifoBufferTable (int fd)
114 {
115 	fifobuffer_t	**newpp;
116 
117 	newpp = (fifobuffer_t **)
118 		Realloc ((void*)FifoBufferTable,
119 		(fd+10)*sizeof (fifobuffer_t *));
120 	if (!newpp)
121 		return	-1;	/* ENOMEM is already set. */
122 
123 	FifoBufferTableSize = fd+10;
124 
125 	return	0;
126 }
127 
128 
129 static fifobuffer_t *
NewFifoBuffer(int fd)130 NewFifoBuffer(int fd)
131 {
132 	int	i;
133 
134 	for (i=0; i < FifoBufferTableSize; i++)
135 	{
136 		if (FifoBufferTable [i] &&
137 		    Fcntl (i, F_GETFL) < 0 &&
138                     errno == EBADF)
139 		{
140 			FifoBufferTable [fd] = FifoBufferTable [i];
141 			FifoBufferTable [i] = NULL;
142 			return	FifoBufferTable [fd];
143 		}
144 	}
145 	FifoBufferTable [fd] = (fifobuffer_t *)
146 		Calloc (1, sizeof (fifobuffer_t));
147 
148 	return	FifoBufferTable [fd];
149 }
150