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