xref: /titanic_51/usr/src/cmd/lp/lib/msgs/mgetputm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 1994 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.6	*/
32 
33 # include	<unistd.h>
34 # include	<errno.h>
35 # include	<stdlib.h>
36 
37 #if	defined(__STDC__)
38 # include	<stdarg.h>
39 #else
40 # include	<varargs.h>
41 #endif
42 
43 # include	"lp.h"
44 # include	"msgs.h"
45 
46 
47 /*
48 **	Size and pointer for mgetm()
49 */
50 static int	MBGSize = 0;
51 static char *	MBG = NULL;
52 
53 /*
54 **	Size and pointer for mputm()
55 */
56 static int	MBPSize = 0;
57 static char *	MBP = NULL;
58 
59 int		peek3_2();
60 
61 #if	defined(__STDC__)
62 int mgetm ( MESG * md, int type, ... )
63 #else
64 int mgetm (md, type, va_alist)
65     MESG	*md;
66     int		type;
67     va_dcl
68 #endif
69 {
70     va_list	vp;
71     int		ret;
72     int		needsize;
73 
74 #if	defined(__STDC__)
75     va_start(vp, type);
76 #else
77     va_start(vp);
78 #endif
79 
80     needsize = mpeek(md);
81     if (needsize <=0 || needsize > MSGMAX)
82 	needsize = MSGMAX;
83     if (needsize > MBGSize)
84     {
85 	if (MBG)
86 	    Free(MBG);
87 	if ((MBG = (char *)Malloc(needsize)) == NULL)
88 	{
89 	    MBGSize = 0;
90 	    MBG = NULL;
91 	    errno = ENOMEM;
92 	    return(-1);
93 	}
94 	MBGSize = needsize;
95     }
96     if (mread(md, MBG, MBGSize) < 0)
97 	return(-1);
98 
99     ret = _getmessage(MBG, type, vp);
100 
101     va_end(vp);
102 
103     return(ret);
104 }
105 
106 #if	defined(__STDC__)
107 int mputm ( MESG * md, int type, ... )
108 #else
109 int mputm (md, type, va_alist)
110     MESG	*md;
111     int		type;
112     va_dcl
113 #endif
114 {
115     va_list	vp;
116     int		needsize;
117 
118 #if	defined(__STDC__)
119     va_start(vp, type);
120 #else
121     va_start(vp);
122 #endif
123     needsize = _putmessage(NULL, type, vp);
124     va_end(vp);
125     if (needsize <= 0)
126 	return(-1);
127 
128     if (needsize > MBPSize)
129     {
130 	if (MBP)
131 	    Free(MBP);
132 	if ((MBP = (char *)Malloc(needsize)) == NULL)
133 	{
134 	    MBPSize = 0;
135 	    MBP = NULL;
136 	    errno = ENOMEM;
137 	    return(-1);
138 	}
139 	MBPSize = needsize;
140     }
141 
142 #if	defined(__STDC__)
143     va_start(vp, type);
144 #else
145     va_start(vp);
146 #endif
147     needsize = _putmessage(MBP, type, vp);
148     va_end(vp);
149     if (needsize <= 0)
150 	return(-1);
151 
152 
153     return(mwrite(md, MBP));
154 }
155 
156 #if	defined(__STDC__)
157 void __mbfree ( void )
158 #else
159 void __mbfree ()
160 #endif
161 {
162     MBGSize = MBPSize = 0;
163     if (MBG)
164 	Free (MBG);
165     if (MBP)
166 	Free (MBP);
167     MBG = MBP = NULL;
168 }
169 
170 #if	defined(__STDC__)
171 short mpeek ( MESG * md )
172 #else
173 short mpeek (md)
174     MESG	*md;
175 #endif
176 {
177     int size;
178 
179     if (md->type == MD_USR_FIFO || md->type == MD_SYS_FIFO)
180 	return(peek3_2(md->readfd) - EXCESS_3_2_LEN);
181 
182     if (ioctl(md->readfd, I_NREAD, &size))
183 	return((short)size);
184 
185     return(-1);
186 }
187