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