]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - tetris/input.c
Coverity CID 868: Fix possible NULL deref (after INFTIM passes :-)
[bsdgames-darwin.git] / tetris / input.c
1 /* $NetBSD: input.c,v 1.10 2006/03/19 00:50:28 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek and Darren F. Provine.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)input.c 8.1 (Berkeley) 5/31/93
35 */
36
37 /*
38 * Tetris input.
39 */
40
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <sys/poll.h>
44
45 #include <errno.h>
46 #include <unistd.h>
47
48 #include "input.h"
49 #include "tetris.h"
50
51 /* return true iff the given timeval is positive */
52 #define TV_POS(tv) \
53 ((tv)->tv_sec > 0 || ((tv)->tv_sec == 0 && (tv)->tv_usec > 0))
54
55 /* subtract timeval `sub' from `res' */
56 #define TV_SUB(res, sub) \
57 (res)->tv_sec -= (sub)->tv_sec; \
58 (res)->tv_usec -= (sub)->tv_usec; \
59 if ((res)->tv_usec < 0) { \
60 (res)->tv_usec += 1000000; \
61 (res)->tv_sec--; \
62 }
63
64 /*
65 * Do a `read wait': poll for reading from stdin, with timeout *tvp.
66 * On return, modify *tvp to reflect the amount of time spent waiting.
67 * It will be positive only if input appeared before the time ran out;
68 * otherwise it will be zero or perhaps negative.
69 *
70 * If tvp is nil, wait forever, but return if poll is interrupted.
71 *
72 * Return 0 => no input, 1 => can read() from stdin
73 */
74 int
75 rwait(tvp)
76 struct timeval *tvp;
77 {
78 struct pollfd set[1];
79 struct timeval starttv, endtv;
80 int timeout;
81 #define NILTZ ((struct timezone *)0)
82
83 if (tvp) {
84 (void) gettimeofday(&starttv, NILTZ);
85 endtv = *tvp;
86 timeout = tvp->tv_sec * 1000 + tvp->tv_usec / 1000;
87 } else
88 timeout = INFTIM;
89 again:
90 set[0].fd = STDIN_FILENO;
91 set[0].events = POLLIN;
92 switch (poll(set, 1, timeout)) {
93
94 case -1:
95 if (tvp == 0)
96 return (-1);
97 if (errno == EINTR)
98 goto again;
99 stop("poll failed, help");
100 /* NOTREACHED */
101
102 case 0: /* timed out */
103 if (tvp) {
104 tvp->tv_sec = 0;
105 tvp->tv_usec = 0;
106 }
107 return (0);
108 }
109 if (tvp) {
110 /* since there is input, we may not have timed out */
111 (void) gettimeofday(&endtv, NILTZ);
112 TV_SUB(&endtv, &starttv);
113 TV_SUB(tvp, &endtv); /* adjust *tvp by elapsed time */
114 }
115 return (1);
116 }
117
118 /*
119 * `sleep' for the current turn time.
120 * Eat any input that might be available.
121 */
122 void
123 tsleep()
124 {
125 struct timeval tv;
126 char c;
127
128 tv.tv_sec = 0;
129 tv.tv_usec = fallrate;
130 while (TV_POS(&tv))
131 if (rwait(&tv) && read(0, &c, 1) != 1)
132 break;
133 }
134
135 /*
136 * getchar with timeout.
137 */
138 int
139 tgetchar()
140 {
141 static struct timeval timeleft;
142 char c;
143
144 /*
145 * Reset timeleft to fallrate whenever it is not positive.
146 * In any case, wait to see if there is any input. If so,
147 * take it, and update timeleft so that the next call to
148 * tgetchar() will not wait as long. If there is no input,
149 * make timeleft zero or negative, and return -1.
150 *
151 * Most of the hard work is done by rwait().
152 */
153 if (!TV_POS(&timeleft)) {
154 faster(); /* go faster */
155 timeleft.tv_sec = 0;
156 timeleft.tv_usec = fallrate;
157 }
158 if (!rwait(&timeleft))
159 return (-1);
160 if (read(0, &c, 1) != 1)
161 stop("end of file, help");
162 return ((int)(unsigned char)c);
163 }