8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lp / lib / msgs / fifo_buffs.c
blobbf5621bcab87c844145c65a766a889de52ec6889
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3 */
32 /* LINTLIBRARY */
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include "lp.h"
39 #include "msgs.h"
41 static fifobuffer_t **FifoBufferTable = NULL;
42 static int FifoBufferTableSize = 0;
45 ** Local functions
47 static int InitFifoBufferTable (void);
48 static int GrowFifoBufferTable (int);
49 static fifobuffer_t *NewFifoBuffer (int);
52 int
53 ResetFifoBuffer(int fd)
55 if ((!FifoBufferTableSize) && (InitFifoBufferTable () < 0))
56 return -1;
58 if (fd >= FifoBufferTableSize)
59 return 0;
61 if (FifoBufferTable [fd]) {
62 FifoBufferTable [fd]->full = 0;
63 FifoBufferTable [fd]->psave =
64 FifoBufferTable [fd]->psave_end =
65 FifoBufferTable [fd]->save;
67 return 0;
71 fifobuffer_t *
72 GetFifoBuffer(int fd)
74 if (fd < 0) {
75 errno = EINVAL;
76 return NULL;
78 if ((fd >= FifoBufferTableSize) && (GrowFifoBufferTable (fd) < 0))
79 return NULL;
81 if (!FifoBufferTable [fd]) {
82 if (!NewFifoBuffer (fd))
83 return NULL;
85 FifoBufferTable [fd]->full = 0;
86 FifoBufferTable [fd]->psave =
87 FifoBufferTable [fd]->psave_end =
88 FifoBufferTable [fd]->save;
91 return FifoBufferTable [fd];
95 static int
96 InitFifoBufferTable()
98 if (FifoBufferTableSize)
99 return 0;
101 FifoBufferTable = (fifobuffer_t **)
102 Calloc (100, sizeof (fifobuffer_t *));
103 if (!FifoBufferTable)
104 return -1; /* ENOMEM is already set. */
106 FifoBufferTableSize = 100;
108 return 0;
112 static int
113 GrowFifoBufferTable (int fd)
115 fifobuffer_t **newpp;
117 newpp = (fifobuffer_t **)
118 Realloc ((void*)FifoBufferTable,
119 (fd+10)*sizeof (fifobuffer_t *));
120 if (!newpp)
121 return -1; /* ENOMEM is already set. */
123 FifoBufferTableSize = fd+10;
125 return 0;
129 static fifobuffer_t *
130 NewFifoBuffer(int fd)
132 int i;
134 for (i=0; i < FifoBufferTableSize; i++)
136 if (FifoBufferTable [i] &&
137 Fcntl (i, F_GETFL) < 0 &&
138 errno == EBADF)
140 FifoBufferTable [fd] = FifoBufferTable [i];
141 FifoBufferTable [i] = NULL;
142 return FifoBufferTable [fd];
145 FifoBufferTable [fd] = (fifobuffer_t *)
146 Calloc (1, sizeof (fifobuffer_t));
148 return FifoBufferTable [fd];