checkpoint of hacking for mail.cs.berkeley.edu
[unix-history] / usr / src / usr.bin / window / ttoutput.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
3dd3a9e5
KB
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
7 *
87f529ec 8 * %sccs.include.redist.c%
60de5df9
EW
9 */
10
46e9ea25 11#ifndef lint
3dd3a9e5 12static char sccsid[] = "@(#)ttoutput.c 3.9 (Berkeley) %G%";
46e9ea25
KB
13#endif /* not lint */
14
1261567e
EW
15#include "ww.h"
16#include "tt.h"
17#include <sys/errno.h>
18
19/*
20 * Buffered output package.
21 * We need this because stdio fails on non-blocking writes.
22 */
23
24ttflush()
25{
26 register char *p;
27 register n;
28 extern errno;
29
30 wwnflush++;
31 for (p = tt_ob; p < tt_obp;) {
32 wwnwr++;
33 n = write(1, p, tt_obp - p);
34 if (n < 0) {
35 wwnwre++;
36 if (errno != EWOULDBLOCK) {
37 /* can't deal with this */
38 p = tt_obp;
39 }
40 } else if (n == 0) {
41 /* what to do? */
42 wwnwrz++;
43 } else {
44 wwnwrc += n;
45 p += n;
46 }
47 }
48 tt_obp = tt_ob;
49}
50
51ttputs(s)
52register char *s;
53{
54 while (*s)
55 ttputc(*s++);
56}
7ffd5866
EW
57
58ttwrite(s, n)
59 register char *s;
60 register n;
61{
62 switch (n) {
63 case 0:
64 break;
65 case 1:
66 ttputc(*s);
67 break;
68 case 2:
69 if (tt_obe - tt_obp < 2)
17f96a0f 70 (*tt.tt_flush)();
7ffd5866
EW
71 *tt_obp++ = *s++;
72 *tt_obp++ = *s;
73 break;
74 case 3:
75 if (tt_obe - tt_obp < 3)
17f96a0f 76 (*tt.tt_flush)();
7ffd5866
EW
77 *tt_obp++ = *s++;
78 *tt_obp++ = *s++;
79 *tt_obp++ = *s;
80 break;
81 case 4:
82 if (tt_obe - tt_obp < 4)
17f96a0f 83 (*tt.tt_flush)();
7ffd5866
EW
84 *tt_obp++ = *s++;
85 *tt_obp++ = *s++;
86 *tt_obp++ = *s++;
87 *tt_obp++ = *s;
88 break;
89 case 5:
90 if (tt_obe - tt_obp < 5)
17f96a0f 91 (*tt.tt_flush)();
7ffd5866
EW
92 *tt_obp++ = *s++;
93 *tt_obp++ = *s++;
94 *tt_obp++ = *s++;
95 *tt_obp++ = *s++;
96 *tt_obp++ = *s;
97 break;
98 default:
99 while (n > 0) {
100 register m;
101
102 while ((m = tt_obe - tt_obp) == 0)
17f96a0f 103 (*tt.tt_flush)();
7ffd5866
EW
104 if ((m = tt_obe - tt_obp) > n)
105 m = n;
106 bcopy(s, tt_obp, m);
107 tt_obp += m;
108 s += m;
109 n -= m;
110 }
111 }
112}