Abolish cgetc(). It contained one line of code, which was wrong.

Call getchar() directly, and handle EOF properly instead of looping
(in some cases) or pretending that EOF is 0 (which it isn't).
This commit is contained in:
dholland 2009-05-24 23:20:22 +00:00
parent 387cc577e1
commit db2522cfd8
6 changed files with 35 additions and 77 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.12 2008/01/28 07:03:59 dholland Exp $
# $NetBSD: Makefile,v 1.13 2009/05/24 23:20:22 dholland Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
PROG= trek
@ -9,7 +9,7 @@ SRCS= abandon.c attack.c autover.c capture.c check_out.c checkcond.c \
lose.c lrscan.c main.c move.c nova.c out.c phaser.c play.c ram.c \
ranf.c rest.c schedule.c score.c setup.c setwarp.c \
shield.c snova.c srscan.c systemname.c torped.c \
visual.c warp.c win.c cgetc.c
visual.c warp.c win.c
MAN= trek.6
DPADD= ${LIBM}
LDADD= -lm

View File

@ -1,48 +0,0 @@
/* $NetBSD: cgetc.c,v 1.8 2009/05/24 18:22:27 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 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. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)cgetc.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: cgetc.c,v 1.8 2009/05/24 18:22:27 dholland Exp $");
#endif
#endif /* not lint */
#include <stdio.h>
#include "trek.h"
char
cgetc(int i __unused)
{
return getchar();
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: computer.c,v 1.14 2009/05/24 22:55:03 dholland Exp $ */
/* $NetBSD: computer.c,v 1.15 2009/05/24 23:20:22 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)computer.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: computer.c,v 1.14 2009/05/24 22:55:03 dholland Exp $");
__RCSID("$NetBSD: computer.c,v 1.15 2009/05/24 23:20:22 dholland Exp $");
#endif
#endif /* not lint */
@ -293,8 +293,8 @@ computer(int v __unused)
* means get new computer request; newline means
* exit computer mode.
*/
while ((i = cgetc(0)) != ';') {
if (i == '\0')
while ((i = getchar()) != ';') {
if (i == EOF)
exit(1);
if (i == '\n') {
ungetc(i, stdin);

View File

@ -1,4 +1,4 @@
/* $NetBSD: getpar.c,v 1.14 2009/05/24 21:44:56 dholland Exp $ */
/* $NetBSD: getpar.c,v 1.15 2009/05/24 23:20:22 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)getpar.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: getpar.c,v 1.14 2009/05/24 21:44:56 dholland Exp $");
__RCSID("$NetBSD: getpar.c,v 1.15 2009/05/24 23:20:22 dholland Exp $");
#endif
#endif /* not lint */
@ -133,7 +133,7 @@ getcodpar(const char *s, const struct cvntab tab[])
printf("%s: ", s);
if (f) {
/* throw out the newline */
cgetc(0);
getchar();
}
scanf("%*[ \t;]");
if ((c = scanf("%99[^ \t;\n]", input)) < 0)
@ -202,7 +202,7 @@ getstrpar(const char *s, char *r, int l, const char *t)
if ((f = testnl()) && s)
printf("%s: ", s);
if (f)
cgetc(0);
getchar();
scanf("%*[\t ;]");
i = scanf(format, r);
if (i < 0)
@ -220,15 +220,19 @@ getstrpar(const char *s, char *r, int l, const char *t)
int
testnl(void)
{
char c;
int c;
while ((c = cgetc(0)) != '\n')
while ((c = getchar()) != '\n') {
if (c == EOF) {
exit(1);
}
if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') || c == '-') {
ungetc(c, stdin);
return(0);
}
}
ungetc(c, stdin);
return (1);
}
@ -241,9 +245,12 @@ testnl(void)
void
skiptonl(int c)
{
while (c != '\n')
if (!(c = cgetc(0)))
return;
while (c != '\n') {
c = getchar();
if (c == EOF) {
exit(1);
}
}
ungetc('\n', stdin);
return;
}
@ -256,10 +263,12 @@ skiptonl(int c)
static int
testterm(void)
{
char c;
int c;
if (!(c = cgetc(0)))
return (1);
c = getchar();
if (c == EOF) {
exit(1);
}
if (c == '.')
return (0);
if (c == '\n' || c == ';')
@ -279,15 +288,15 @@ testterm(void)
int
readdelim(int d)
{
char c;
int c;
while ((c = cgetc(0)) != '\0') {
while ((c = getchar()) != EOF) {
if (c == d)
return (1);
if (c == ' ')
continue;
ungetc(c, stdin);
break;
return 0;
}
return (0);
exit(1);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: torped.c,v 1.13 2009/05/24 22:55:03 dholland Exp $ */
/* $NetBSD: torped.c,v 1.14 2009/05/24 23:20:22 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)torped.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: torped.c,v 1.13 2009/05/24 22:55:03 dholland Exp $");
__RCSID("$NetBSD: torped.c,v 1.14 2009/05/24 23:20:22 dholland Exp $");
#endif
#endif /* not lint */
@ -104,7 +104,7 @@ torped(int v __unused)
} else {
/* see if the user wants one */
if (!testnl()) {
k = ungetc(cgetc(0), stdin);
k = ungetc(getchar(), stdin);
if (k >= '0' && k <= '9')
burst = 1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: trek.h,v 1.15 2009/05/24 22:55:03 dholland Exp $ */
/* $NetBSD: trek.h,v 1.16 2009/05/24 23:20:22 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@ -381,9 +381,6 @@ void autover(void);
void capture(int);
struct kling *selectklingon(void);
/* cgetc.c */
char cgetc(int);
/* check_out.c */
int check_out(int);