xref: /titanic_50/usr/src/cmd/lp/lib/msgs/mread.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6	*/
27*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <stropts.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "lp.h"
36*7c478bd9Sstevel@tonic-gate #include "msgs.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate extern int	Lp_prio_msg;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate **	Function:	int mread( MESG *, char *, int)
42*7c478bd9Sstevel@tonic-gate **	Args:		message descriptor
43*7c478bd9Sstevel@tonic-gate **			message buffer (var)
44*7c478bd9Sstevel@tonic-gate **			buffer size
45*7c478bd9Sstevel@tonic-gate **	Return:		The size of the message in message buffer.
46*7c478bd9Sstevel@tonic-gate **			or -1 on error.  Possible errnos are:
47*7c478bd9Sstevel@tonic-gate **		EINVAL	Bad value for md or msgbuf.
48*7c478bd9Sstevel@tonic-gate **		E2BIG	Not enough space for message.
49*7c478bd9Sstevel@tonic-gate **		EPIPE	Far end dropped the connection.
50*7c478bd9Sstevel@tonic-gate **		ENOMSG	No valid message available on fifo.
51*7c478bd9Sstevel@tonic-gate **
52*7c478bd9Sstevel@tonic-gate **	mread examines message descriptor and either calls read3_2
53*7c478bd9Sstevel@tonic-gate **	to read 3.2 HPI messages or getmsg(2) to read 4.0 HPI messages.
54*7c478bd9Sstevel@tonic-gate **	If a message is read, it is returned in message buffer.
55*7c478bd9Sstevel@tonic-gate */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
mread(MESG * md,char * msgbuf,int size)58*7c478bd9Sstevel@tonic-gate int mread ( MESG * md, char * msgbuf, int size )
59*7c478bd9Sstevel@tonic-gate #else
60*7c478bd9Sstevel@tonic-gate int mread ( md, msgbuf, size )
61*7c478bd9Sstevel@tonic-gate MESG	*md;
62*7c478bd9Sstevel@tonic-gate char	*msgbuf;
63*7c478bd9Sstevel@tonic-gate int	size;
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate     int			flag = 0;
67*7c478bd9Sstevel@tonic-gate     char		buff [MSGMAX];
68*7c478bd9Sstevel@tonic-gate     struct strbuf	dat;
69*7c478bd9Sstevel@tonic-gate     struct strbuf	ctl;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate     if (md == NULL || msgbuf == NULL)
72*7c478bd9Sstevel@tonic-gate     {
73*7c478bd9Sstevel@tonic-gate 	errno = EINVAL;
74*7c478bd9Sstevel@tonic-gate 	return(-1);
75*7c478bd9Sstevel@tonic-gate     }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate     switch(md->type)
78*7c478bd9Sstevel@tonic-gate     {
79*7c478bd9Sstevel@tonic-gate       case MD_CHILD:
80*7c478bd9Sstevel@tonic-gate       case MD_STREAM:
81*7c478bd9Sstevel@tonic-gate       case MD_BOUND:
82*7c478bd9Sstevel@tonic-gate 	if (size <= 0)
83*7c478bd9Sstevel@tonic-gate 	{
84*7c478bd9Sstevel@tonic-gate 	    errno = E2BIG;
85*7c478bd9Sstevel@tonic-gate 	    return(-1);
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 	dat.buf = msgbuf;
88*7c478bd9Sstevel@tonic-gate 	dat.maxlen = size;
89*7c478bd9Sstevel@tonic-gate 	dat.len = 0;
90*7c478bd9Sstevel@tonic-gate 	ctl.buf = buff;
91*7c478bd9Sstevel@tonic-gate 	ctl.maxlen = sizeof (buff);
92*7c478bd9Sstevel@tonic-gate 	ctl.len = 0;
93*7c478bd9Sstevel@tonic-gate 	flag = Lp_prio_msg;
94*7c478bd9Sstevel@tonic-gate 	Lp_prio_msg = 0;	/* clean this up so there are no surprises */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (Getmsg(md, &ctl, &dat, &flag) < 0)
97*7c478bd9Sstevel@tonic-gate 	{
98*7c478bd9Sstevel@tonic-gate 	    if (errno == EBADF)
99*7c478bd9Sstevel@tonic-gate 		errno = EPIPE;
100*7c478bd9Sstevel@tonic-gate 	    return(-1);
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if (dat.len == 0)
104*7c478bd9Sstevel@tonic-gate 	{
105*7c478bd9Sstevel@tonic-gate 	    (void) Close(md->readfd);
106*7c478bd9Sstevel@tonic-gate 	    return(0);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	break;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate       case MD_USR_FIFO:
111*7c478bd9Sstevel@tonic-gate       case MD_SYS_FIFO:
112*7c478bd9Sstevel@tonic-gate 	if (size < CONTROL_LEN)
113*7c478bd9Sstevel@tonic-gate 	{
114*7c478bd9Sstevel@tonic-gate 	    errno = E2BIG;
115*7c478bd9Sstevel@tonic-gate 	    return(-1);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (read3_2(md, msgbuf, size) < 0)
119*7c478bd9Sstevel@tonic-gate 	    return(-1);
120*7c478bd9Sstevel@tonic-gate 	break;
121*7c478bd9Sstevel@tonic-gate     }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate     return((int)msize(msgbuf));
124*7c478bd9Sstevel@tonic-gate }
125