diff options
author | dholland <dholland@NetBSD.org> | 2008-02-23 21:35:13 +0000 |
---|---|---|
committer | dholland <dholland@NetBSD.org> | 2008-02-23 21:35:13 +0000 |
commit | 27c1fac74480e16625e433b49acff1de7afe2679 (patch) | |
tree | aea7029301b82e663a1929952322e72f9c730de4 /monop | |
parent | 1cdc3b6df35396e5120ad58e3f0a616abce0186b (diff) | |
download | bsdgames-darwin-27c1fac74480e16625e433b49acff1de7afe2679.tar.gz bsdgames-darwin-27c1fac74480e16625e433b49acff1de7afe2679.tar.zst bsdgames-darwin-27c1fac74480e16625e433b49acff1de7afe2679.zip |
Sanity fixes for input handling. Suggested by OpenBSD, but different code.
Diffstat (limited to 'monop')
-rw-r--r-- | monop/misc.c | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/monop/misc.c b/monop/misc.c index 3de75b53..803c6e84 100644 --- a/monop/misc.c +++ b/monop/misc.c @@ -1,4 +1,4 @@ -/* $NetBSD: misc.c,v 1.17 2008/02/20 04:10:01 dholland Exp $ */ +/* $NetBSD: misc.c,v 1.18 2008/02/23 21:35:13 dholland Exp $ */ /* * Copyright (c) 1980, 1993 @@ -34,12 +34,13 @@ #if 0 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: misc.c,v 1.17 2008/02/20 04:10:01 dholland Exp $"); +__RCSID("$NetBSD: misc.c,v 1.18 2008/02/23 21:35:13 dholland Exp $"); #endif #endif /* not lint */ #include "monop.ext" #include <ctype.h> +#include <limits.h> #include <signal.h> /* @@ -94,29 +95,25 @@ int get_int(prompt) const char *prompt; { - int num; + long num; char *sp; - int c; char buf[257]; for (;;) { -inter: printf("%s", prompt); - num = 0; - for (sp = buf; (c=getchar()) != '\n'; *sp++ = c) - if (c == -1) /* check for interrupted system call */ - goto inter; - *sp = c; - if (sp == buf) - continue; - for (sp = buf; isspace((unsigned char)*sp); sp++) - continue; - for (; isdigit((unsigned char)*sp); sp++) - num = num * 10 + *sp - '0'; - if (*sp == '\n') - return num; - else + fgets(buf, sizeof(buf), stdin); + if (feof(stdin)) + return 0; + sp = strchr(buf, '\n'); + if (sp) + *sp = '\0'; + errno = 0; + num = strtol(buf, &sp, 10); + if (errno || strlen(sp) > 0 || num < 0 || num >= INT_MAX) { printf("I can't understand that\n"); + continue; + } + return num; } } @@ -218,6 +215,7 @@ set_ownlist(pl) op->sqr->desc); } printf("num = %d\n", num); + exit(1); } #ifdef DEBUG printf("orig->num_in = %d\n", orig->num_in); |