xref: /illumos-gate/usr/src/cmd/lp/lib/msgs/mread.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6	*/
277c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stropts.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "lp.h"
367c478bd9Sstevel@tonic-gate #include "msgs.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate extern int	Lp_prio_msg;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate **	Function:	int mread( MESG *, char *, int)
427c478bd9Sstevel@tonic-gate **	Args:		message descriptor
437c478bd9Sstevel@tonic-gate **			message buffer (var)
447c478bd9Sstevel@tonic-gate **			buffer size
457c478bd9Sstevel@tonic-gate **	Return:		The size of the message in message buffer.
467c478bd9Sstevel@tonic-gate **			or -1 on error.  Possible errnos are:
477c478bd9Sstevel@tonic-gate **		EINVAL	Bad value for md or msgbuf.
487c478bd9Sstevel@tonic-gate **		E2BIG	Not enough space for message.
497c478bd9Sstevel@tonic-gate **		EPIPE	Far end dropped the connection.
507c478bd9Sstevel@tonic-gate **		ENOMSG	No valid message available on fifo.
517c478bd9Sstevel@tonic-gate **
527c478bd9Sstevel@tonic-gate **	mread examines message descriptor and either calls read3_2
537c478bd9Sstevel@tonic-gate **	to read 3.2 HPI messages or getmsg(2) to read 4.0 HPI messages.
547c478bd9Sstevel@tonic-gate **	If a message is read, it is returned in message buffer.
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
mread(MESG * md,char * msgbuf,int size)587c478bd9Sstevel@tonic-gate int mread ( MESG * md, char * msgbuf, int size )
597c478bd9Sstevel@tonic-gate #else
607c478bd9Sstevel@tonic-gate int mread ( md, msgbuf, size )
617c478bd9Sstevel@tonic-gate MESG	*md;
627c478bd9Sstevel@tonic-gate char	*msgbuf;
637c478bd9Sstevel@tonic-gate int	size;
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate     int			flag = 0;
677c478bd9Sstevel@tonic-gate     char		buff [MSGMAX];
687c478bd9Sstevel@tonic-gate     struct strbuf	dat;
697c478bd9Sstevel@tonic-gate     struct strbuf	ctl;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate     if (md == NULL || msgbuf == NULL)
727c478bd9Sstevel@tonic-gate     {
737c478bd9Sstevel@tonic-gate 	errno = EINVAL;
747c478bd9Sstevel@tonic-gate 	return(-1);
757c478bd9Sstevel@tonic-gate     }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate     switch(md->type)
787c478bd9Sstevel@tonic-gate     {
797c478bd9Sstevel@tonic-gate       case MD_CHILD:
807c478bd9Sstevel@tonic-gate       case MD_STREAM:
817c478bd9Sstevel@tonic-gate       case MD_BOUND:
827c478bd9Sstevel@tonic-gate 	if (size <= 0)
837c478bd9Sstevel@tonic-gate 	{
847c478bd9Sstevel@tonic-gate 	    errno = E2BIG;
857c478bd9Sstevel@tonic-gate 	    return(-1);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	dat.buf = msgbuf;
887c478bd9Sstevel@tonic-gate 	dat.maxlen = size;
897c478bd9Sstevel@tonic-gate 	dat.len = 0;
907c478bd9Sstevel@tonic-gate 	ctl.buf = buff;
917c478bd9Sstevel@tonic-gate 	ctl.maxlen = sizeof (buff);
927c478bd9Sstevel@tonic-gate 	ctl.len = 0;
937c478bd9Sstevel@tonic-gate 	flag = Lp_prio_msg;
947c478bd9Sstevel@tonic-gate 	Lp_prio_msg = 0;	/* clean this up so there are no surprises */
95*55fea89dSDan Cross 
967c478bd9Sstevel@tonic-gate 	if (Getmsg(md, &ctl, &dat, &flag) < 0)
977c478bd9Sstevel@tonic-gate 	{
987c478bd9Sstevel@tonic-gate 	    if (errno == EBADF)
997c478bd9Sstevel@tonic-gate 		errno = EPIPE;
1007c478bd9Sstevel@tonic-gate 	    return(-1);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (dat.len == 0)
1047c478bd9Sstevel@tonic-gate 	{
1057c478bd9Sstevel@tonic-gate 	    (void) Close(md->readfd);
1067c478bd9Sstevel@tonic-gate 	    return(0);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	break;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate       case MD_USR_FIFO:
1117c478bd9Sstevel@tonic-gate       case MD_SYS_FIFO:
1127c478bd9Sstevel@tonic-gate 	if (size < CONTROL_LEN)
1137c478bd9Sstevel@tonic-gate 	{
1147c478bd9Sstevel@tonic-gate 	    errno = E2BIG;
1157c478bd9Sstevel@tonic-gate 	    return(-1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (read3_2(md, msgbuf, size) < 0)
1197c478bd9Sstevel@tonic-gate 	    return(-1);
1207c478bd9Sstevel@tonic-gate 	break;
1217c478bd9Sstevel@tonic-gate     }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate     return((int)msize(msgbuf));
1247c478bd9Sstevel@tonic-gate }
125