xref: /titanic_41/usr/src/cmd/lp/lib/msgs/write_fifo.c (revision dd49f125507979bb2ab505a8daf2a46d1be27051)
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.10	*/
27 /* LINTLIBRARY */
28 
29 # include	<unistd.h>
30 # include	<errno.h>
31 # include	<string.h>
32 
33 # include	"lp.h"
34 # include	"msgs.h"
35 
36 /*
37 ** Choose at least one byte that won't appear in the body or header
38 ** of a message.
39 */
40 unsigned char	Resync[HEAD_RESYNC_LEN]		= { 0x01, 0xFE };
41 unsigned char	Endsync[HEAD_RESYNC_LEN]	= { 0x02, 0xFD };
42 
43 
44 /*
45 ** write_fifo() - WRITE A BUFFER WITH HEADER AND CHECKSUM
46 */
47 
48 #if	defined(__STDC__)
49 int write_fifo ( int fifo, char * buf, unsigned int size )
50 #else
51 int write_fifo (fifo, buf, size)
52     int			fifo;
53     char		*buf;
54     unsigned int	size;
55 #endif
56 {
57     unsigned short	chksum	= 0;
58     int			wbytes	= 0;
59 
60     (void)memcpy (buf + HEAD_RESYNC, Resync, HEAD_RESYNC_LEN);
61     (void)memcpy (buf + TAIL_ENDSYNC(size), Endsync, TAIL_ENDSYNC_LEN);
62 
63     CALC_CHKSUM (buf, size, chksum);
64     (void)htos (buf + TAIL_CHKSUM(size), chksum);
65 
66 
67     /*
68     ** A message must be written in one call, to avoid interleaving
69     ** messages from several processes.
70     **
71     ** The caller is responsible for trapping SIGPIPE, so
72     ** we just return what the "write()" system call does.
73     **
74     ** Well, almost.  If the pipe was almost full, we may have
75     ** written a partial message.  If this is the case, we lie
76     ** and say the pipe was full, so the caller can try again.
77     **
78     ** read_fifo can deal with a truncated message, so we let it
79     ** do the grunt work associated with partial messages.
80     **
81     ** NOTE:  Writing the remainder of the message is not feasible
82     ** as someone else may have written something to the fifo
83     ** while we were setting up to retry.
84     */
85 
86     if ((wbytes = write(fifo, buf, size)) > 0)
87 	if (wbytes != size)
88 	    return(0);
89 
90     return(wbytes);
91 }
92