xref: /titanic_50/usr/src/cmd/lp/model/drain.output.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "termio.h"
33 
34 /*
35  * The following macro computes the number of seconds to sleep
36  * AFTER waiting for the system buffers to be drained.
37  *
38  * Various choices:
39  *
40  *	- A percentage (perhaps even >100%) of the time it would
41  *	  take to print the printer's buffer. Use this if it appears
42  *	  the printers are affected if the port is closed before they
43  *	  finish printing.
44  *
45  *	- 0. Use this to avoid any extra sleep after waiting for the
46  *	  system buffers to be flushed.
47  *
48  *	- N > 0. Use this to have a fixed sleep after flushing the
49  *	  system buffers.
50  *
51  * The sleep period can be overridden by a single command line argument.
52  */
53 			/* 25% of the print-full-buffer time, plus 1 */
54 #define LONG_ENOUGH(BUFSZ,CPS)	 (1 + ((250 * BUFSZ) / CPS) / 1000)
55 
56 extern int		tidbit();
57 
58 /**
59  ** main()
60  **/
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	extern char		*getenv();
66 
67 	short			bufsz	= -1,
68 				cps	= -1;
69 
70 	char			*TERM;
71 
72 	int			sleep_time	= 0;
73 
74 
75 	/*
76 	 * Wait for the output to drain.
77 	 */
78 	ioctl (1, TCSBRK, (struct termio *)1);
79 
80 	/*
81 	 * Decide how long to sleep.
82 	 */
83 	if (argc != 2 || (sleep_time = atoi(argv[1])) < 0)
84 		if ((TERM = getenv("TERM"))) {
85 			tidbit (TERM, "bufsz", &bufsz);
86 			tidbit (TERM, "cps", &cps);
87 			if (cps > 0 && bufsz > 0)
88 				sleep_time = LONG_ENOUGH(bufsz, cps);
89 		} else
90 			sleep_time = 2;
91 
92 	/*
93 	 * Wait ``long enough'' for the printer to finish
94 	 * printing what's in its buffer.
95 	 */
96 	if (sleep_time)
97 		sleep (sleep_time);
98 
99 	return (0);
100 }
101