+/* $NetBSD: update.c,v 1.28 2021/05/02 12:50:43 rillig Exp $ */
+
/*-
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ed James.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* For more info on this and all of my stuff, mail edjames@berkeley.edu.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char sccsid[] = "@(#)update.c 5.5 (Berkeley) 10/30/90";
-#endif not lint
+#if 0
+static char sccsid[] = "@(#)update.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: update.c,v 1.28 2021/05/02 12:50:43 rillig Exp $");
+#endif
+#endif /* not lint */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "def.h"
+#include "struct.h"
+#include "extern.h"
+#include "tunable.h"
-#include "include.h"
+static int next_plane(void);
+static int too_close(const PLANE *p1, const PLANE *p2, int);
+static int dir_deg(int);
-update()
+/* ARGSUSED */
+void
+update(int dummy __unused)
{
- int i, dir_diff, mask, unclean;
- PLANE *pp, *p1, *p2, *p;
+ int dir_diff, unclean;
+ unsigned i;
+ PLANE *pp, *p1, *p2;
-#ifdef BSD
- mask = sigblock(sigmask(SIGINT));
-#endif
#ifdef SYSV
alarm(0);
signal(SIGALRM, update);
if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
pp->ypos == sp->beacon[pp->delayd_no].y) {
- pp->delayd = 0;
+ pp->delayd = false;
if (pp->status == S_UNMARKED)
pp->status = S_MARKED;
}
}
if (pp->altitude > 9)
/* "this is impossible" */
- loser(pp, "exceded flight ceiling.");
+ loser(pp, "exceeded flight ceiling.");
if (pp->altitude <= 0) {
for (i = 0; i < sp->num_airports; i++)
if (pp->xpos == sp->airport[i].x &&
pp->ypos == sp->airport[i].y) {
if (pp->dest_type == T_AIRPORT)
- loser(pp,
+ loser(pp,
"landed at the wrong airport.");
else
- loser(pp,
+ loser(pp,
"landed instead of exited.");
}
loser(pp, "crashed on the ground.");
if (pp->xpos == sp->exit[i].x &&
pp->ypos == sp->exit[i].y) {
if (pp->dest_type == T_EXIT)
- loser(pp,
+ loser(pp,
"exited via the wrong exit.");
else
- loser(pp,
+ loser(pp,
"exited instead of landed.");
}
loser(pp, "illegally left the flight arena.");
if (too_close(p1, p2, 1)) {
static char buf[80];
- (void)sprintf(buf, "collided with plane '%c'.",
+ (void)snprintf(buf, sizeof(buf),
+ "collided with plane '%c'.",
name(p2));
loser(p1, buf);
}
if ((rand() % sp->newplane_time) == 0)
addplane();
+#ifdef SYSV
+ alarm(sp->update_secs);
+#endif
+}
+
+void
+loser(const PLANE *p, const char *s)
+{
+ int c;
#ifdef BSD
- sigsetmask(mask);
+ struct itimerval itv;
+#endif
+
+ /* disable timer */
+#ifdef BSD
+ itv.it_value.tv_sec = 0;
+ itv.it_value.tv_usec = 0;
+ (void)setitimer(ITIMER_REAL, &itv, NULL);
#endif
#ifdef SYSV
- alarm(sp->update_secs);
+ alarm(0);
#endif
+
+ losermsg(p, s);
+ while ((c = getAChar()) != EOF && c != ' ')
+ ;
+ shutdown_gr();
+ (void)log_score(0);
+ exit(0);
}
-char *
-command(pp)
- PLANE *pp;
+const char *
+command(const PLANE *pp)
{
static char buf[50], *bp, *comm_start;
- char *index();
+ size_t bpsize;
buf[0] = '\0';
bp = buf;
- (void)sprintf(bp, "%c%d%c%c%d: ", name(pp), pp->altitude,
+ bpsize = sizeof(buf);
+ (void)snprintf(bp, bpsize, "%c%d%c%c%u: ", name(pp), pp->altitude,
(pp->fuel < LOWFUEL) ? '*' : ' ',
(pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
- comm_start = bp = index(buf, '\0');
+ comm_start = bp = strchr(buf, '\0');
+ bpsize = buf + sizeof(buf) - bp;
if (pp->altitude == 0)
- (void)sprintf(bp, "Holding @ A%d", pp->orig_no);
+ (void)snprintf(bp, bpsize, "Holding @ A%u", pp->orig_no);
else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
- strcpy(bp, "Circle");
+ (void)snprintf(bp, bpsize, "Circle");
else if (pp->new_dir != pp->dir)
- (void)sprintf(bp, "%d", dir_deg(pp->new_dir));
+ (void)snprintf(bp, bpsize, "%d", dir_deg(pp->new_dir));
- bp = index(buf, '\0');
+ bp = strchr(buf, '\0');
+ bpsize = buf + sizeof(buf) - bp;
if (pp->delayd)
- (void)sprintf(bp, " @ B%d", pp->delayd_no);
+ (void)snprintf(bp, bpsize, " @ B%u", pp->delayd_no);
- bp = index(buf, '\0');
- if (*comm_start == '\0' &&
+ bp = strchr(buf, '\0');
+ bpsize = buf + sizeof(buf) - bp;
+ if (*comm_start == '\0' &&
(pp->status == S_UNMARKED || pp->status == S_IGNORED))
- strcpy(bp, "---------");
+ (void)snprintf(bp, bpsize, "---------");
return (buf);
}
-/* char */
-name(p)
- PLANE *p;
+char
+name(const PLANE *p)
{
if (p->plane_type == 0)
return ('A' + p->plane_no);
return ('a' + p->plane_no);
}
-number(l)
+int
+number(int l)
{
- if (l < 'a' && l > 'z' && l < 'A' && l > 'Z')
- return (-1);
- else if (l >= 'a' && l <= 'z')
+ if (islower((unsigned char)l))
return (l - 'a');
- else
+ else if (isupper((unsigned char)l))
return (l - 'A');
+ else
+ return (-1);
}
-next_plane()
+static int
+next_plane(void)
{
static int last_plane = -1;
PLANE *pp;
break;
}
} while (found && last_plane != start_plane);
- if (last_plane == start_plane)
+ if (found)
return (-1);
return (last_plane);
}
-addplane()
+void
+addplane(void)
{
PLANE p, *pp, *p1;
- int i, num_starts, close, rnd, rnd2, pnum;
+ int isclose, pnum;
+ unsigned num_starts, rnd, rnd2, i;
- bzero(&p, sizeof (p));
+ (void)memset(&p, 0, sizeof (p));
p.status = S_MARKED;
p.plane_type = random() % 2;
p.ypos = sp->exit[rnd2].y;
p.new_dir = p.dir = sp->exit[rnd2].dir;
p.altitude = p.new_altitude = 7;
- close = 0;
+ isclose = 0;
for (p1 = air.head; p1 != NULL; p1 = p1->next)
if (too_close(p1, &p, 4)) {
- close++;
+ isclose++;
break;
}
- if (close)
+ if (isclose)
continue;
} else {
p.orig_type = T_AIRPORT;
break;
}
if (i >= num_starts)
- return (-1);
+ return;
pnum = next_plane();
if (pnum < 0)
- return (-1);
+ return;
p.plane_no = pnum;
pp = newplane();
- bcopy(&p, pp, sizeof (p));
+ if (pp == NULL)
+ loser(NULL, "Out of memory!");
+ (void)memcpy(pp, &p, sizeof (p));
if (pp->orig_type == T_AIRPORT)
append(&ground, pp);
else
append(&air, pp);
-
- return (pp->dest_type);
}
-PLANE *
-findplane(n)
+PLANE *
+findplane(int n)
{
PLANE *pp;
return (NULL);
}
-too_close(p1, p2, dist)
- PLANE *p1, *p2;
+static int
+too_close(const PLANE *p1, const PLANE *p2, int dist)
{
if (ABS(p1->altitude - p2->altitude) <= dist &&
- ABS(p1->xpos - p2->xpos) <= dist && ABS(p1->ypos - p2->ypos) <= dist)
+ ABS(p1->xpos - p2->xpos) <= dist &&
+ ABS(p1->ypos - p2->ypos) <= dist)
return (1);
else
return (0);
}
-dir_deg(d)
+static int
+dir_deg(int d)
{
switch (d) {
case 0: return (0);