8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lp / lib / msgs / mwrite.c
blobb19a4bbb682477e0ab57cde772a949a6a813539b
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.8 */
32 /* LINTLIBRARY */
34 # include <errno.h>
35 # include <string.h>
36 # include <stropts.h>
38 # include "lp.h"
39 # include "msgs.h"
41 int Lp_prio_msg = 0;
43 static int _mwrite ( MESG * md , char * msgbuf , int );
46 * mflush()
47 * return 0
48 * if it successfully writes all the queued message(s), or
49 * if some of them (errno is EAGAIN in this case).
50 * return -1 (with errno) when it failed.
53 int
54 mflush(MESG *md)
56 MQUE *p;
58 errno = 0;
59 if (md == NULL || md->mque == NULL) {
60 errno = ENXIO;
61 return (-1);
64 while ((p = md->mque) != NULL) {
65 if (_mwrite(md, p->dat->buf, p->dat->len) != 0)
66 return (errno == EAGAIN ? 0 : -1);
68 /* mwrite successful, get the next and free this entry */
69 md->mque = p->next;
70 Free(p->dat->buf);
71 Free(p->dat);
72 Free(p);
75 return (0);
79 * mwrite()
80 * return 0
81 * if it successfully writes the messages, or
82 * if it has been queued (errno is EAGAIN in this case)
83 * and md->mque is updated.
84 * return -1 (with errno) when it failed.
87 int mwrite ( MESG * md, char * msgbuf )
89 short size;
90 MQUE * p;
91 MQUE * q;
93 errno = 0;
94 if (md == NULL)
96 errno = ENXIO;
97 return(-1);
99 if (msgbuf == NULL)
101 errno = EINVAL;
102 return(-1);
105 size = stoh(msgbuf);
107 if (LAST_MESSAGE < stoh(msgbuf + MESG_TYPE))
109 errno = EINVAL;
110 return (-1);
112 if (md->mque)
113 goto queue; /* if there is a queue already, try to write all */
115 if (_mwrite(md, msgbuf, size) == 0)
116 return(0);
118 if (errno != EAGAIN)
119 return(-1);
122 * fall through to queue the messages that cannot be sent now.
125 queue:
126 if ((p = (MQUE *)Malloc(sizeof(MQUE))) == NULL
127 || (p->dat = (struct strbuf *)Malloc(sizeof(struct strbuf))) == NULL
128 || (p->dat->buf = (char *)Malloc(size)) == NULL)
130 errno = ENOMEM;
131 return(-1);
133 (void) memcpy(p->dat->buf, msgbuf, size);
134 p->dat->len = size;
135 p->next = 0;
137 if ((q = md->mque) != NULL)
139 /* insert the new one to tail */
140 while (q->next)
141 q = q->next;
142 q->next = p;
144 while ((p = md->mque) != NULL)
146 if (_mwrite(md, p->dat->buf, p->dat->len) != 0) {
147 return (errno == EAGAIN ? 0 : -1);
150 /* mwrite successful, get the next and free this entry */
151 md->mque = p->next;
152 Free(p->dat->buf);
153 Free(p->dat);
154 Free(p);
157 else
158 md->mque = p;
160 return(0);
163 int _mwrite ( MESG * md, char * msgbuf , int size )
165 int flag = 0;
166 struct strbuf ctl;
167 struct strbuf dat;
169 switch (md->type)
171 case MD_CHILD:
172 case MD_STREAM:
173 case MD_BOUND:
174 if (size <= 0 || size > MSGMAX)
176 errno = EINVAL;
177 return(-1);
180 ctl.buf = "xyzzy";
181 ctl.maxlen = ctl.len = strlen(ctl.buf)+1;
182 dat.buf = msgbuf;
183 dat.maxlen = dat.len = size;
184 flag = Lp_prio_msg;
185 Lp_prio_msg = 0; /* clean this up so there are no surprises */
187 if (Putmsg(md, &ctl, &dat, flag) == 0)
188 return(0);
189 return(-1);
191 case MD_SYS_FIFO:
192 case MD_USR_FIFO:
193 switch (write3_2(md, msgbuf, size))
195 case -1:
196 return(-1);
197 case 0:
198 break;
199 default:
200 return(0);
202 break;
204 default:
205 errno = EINVAL;
206 return(-1);
208 return 0;