xref: /titanic_50/usr/src/cmd/lp/lib/msgs/_getmessage.c (revision 2449e17f82f6097fd2c665b64723e31ceecbeca6)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9	*/
27 /* LINTLIBRARY */
28 
29 # include	<stdarg.h>
30 # include	<string.h>
31 # include	<errno.h>
32 
33 # include	"msgs.h"
34 
35 extern char	*_lp_msg_fmts[];
36 extern int	errno;
37 
38 /* VARARGS */
39 #if	defined(__STDC__)
40 int _getmessage ( char * buf, short rtype, va_list arg )
41 #else
42 int _getmessage (buf, rtype, arg)
43     char	*buf;
44     short	rtype;
45     va_list	arg;
46 #endif
47 {
48     char	*endbuf;
49     char	*fmt;
50     char	**t_string;
51     int		temp = 0;
52     long	*t_long;
53     short	*t_short;
54     short	etype;
55 
56     if (buf == (char *)0)
57     {
58 	errno = ENOSPC;
59 	return(-1);
60     }
61 
62     /*
63      * We assume that we're given a buffer big enough to hold
64      * the header.
65      */
66 
67     endbuf = buf + (long)stoh(buf);
68     if ((buf + MESG_DATA) > endbuf)
69     {
70 	errno = ENOMSG;
71 	return(-1);
72     }
73 
74     etype = stoh(buf + MESG_TYPE);
75     if (etype < 0 || etype > LAST_MESSAGE)
76     {
77 	errno = EBADMSG;
78         return(-1);
79     }
80 
81     if (etype != rtype)
82     {
83 	if (rtype > 0 && rtype <= LAST_MESSAGE)
84 	    fmt = _lp_msg_fmts[rtype];
85 	else
86 	{
87 	    errno = EINVAL;
88 	    return(-1);
89 	}
90     }
91     else
92 	fmt = _lp_msg_fmts[etype];
93 
94     buf += MESG_LEN;
95 
96     while (*fmt != '\0')
97 	switch(*fmt++)
98 	{
99 	    case 'H':
100 	        if ((buf + 4) > endbuf)
101 		{
102 		    errno = ENOMSG;
103 		    return(-1);
104 		}
105 
106 		t_short = va_arg(arg, short *);
107 		*t_short = stoh(buf);
108 		buf += 4;
109 		break;
110 
111 	    case 'L':
112 		if ((buf + 8) > endbuf)
113 		{
114 		    errno = ENOMSG;
115 		    return(-1);
116 		}
117 
118 		t_long = va_arg(arg, long *);
119 		*t_long = stol(buf);
120 		buf += 8;
121 		break;
122 
123 	    case 'D':
124 		if ((buf + 4) > endbuf)
125 		{
126 		    errno = ENOMSG;
127 		    return(-1);
128 		}
129 
130 		t_short = va_arg(arg, short *);
131 		*t_short = stoh(buf);
132 		buf += 4;
133 		t_string = va_arg(arg, char **);
134 		if ((buf + *t_short) > endbuf)
135 		{
136 		    errno = ENOMSG;
137 		    return(-1);
138 		}
139 		(*t_short)--;		/* Don't mention the null we added */
140 		*t_string = buf;
141 		buf += *t_short;
142 		break;
143 
144 	    case 'S':
145 		if ((buf + 4) > endbuf)
146 		{
147 		    errno = ENOMSG;
148 		    return(-1);
149 		}
150 
151 		t_string = va_arg(arg, char **);
152 		temp = stoh(buf);
153 		buf += 4;
154 		if ((buf + temp) > endbuf)
155 		{
156 		    errno = ENOMSG;
157 		    return(-1);
158 		}
159 
160 		*t_string = buf;
161 		buf += temp;
162 		break;
163 	}
164     return(etype);
165 }
166