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