summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2014-11-04 17:50:23 +0000
committertedu <tedu@openbsd.org>2014-11-04 17:50:23 +0000
commit50d1b26a76893391431ff2a251be716e3cfea5be (patch)
tree3354c4242a046fc61ee68c98eff0ed069815004f
parentFix memory leak on reallocarray() failure introduced by conversion (diff)
downloadwireguard-openbsd-50d1b26a76893391431ff2a251be716e3cfea5be.tar.xz
wireguard-openbsd-50d1b26a76893391431ff2a251be716e3cfea5be.zip
allow printing longer lines than fit on a card by spilling onto more cards.
don't negatively index into the table for signed chars. ok pjanzen
-rw-r--r--games/bcd/bcd.c53
1 files changed, 30 insertions, 23 deletions
diff --git a/games/bcd/bcd.c b/games/bcd/bcd.c
index ba4a522718f..0e9afa6edb9 100644
--- a/games/bcd/bcd.c
+++ b/games/bcd/bcd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bcd.c,v 1.13 2009/10/27 23:59:24 deraadt Exp $ */
+/* $OpenBSD: bcd.c,v 1.14 2014/11/04 17:50:23 tedu Exp $ */
/* $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ */
/*
@@ -110,44 +110,51 @@ u_short holes[256] = {
*/
#define bit(w,i) ((w)&(1<<(i)))
-void printcard(char *);
+void printcard(char *, size_t);
+
+#define COLUMNS 48
+
int
main(int argc, char *argv[])
{
- char cardline[80];
/*
* The original bcd prompts with a "%" when reading from stdin,
* but this seems kind of silly. So this one doesn't.
*/
-
if (argc > 1) {
- while (--argc)
- printcard(*++argv);
- } else
- while (fgets(cardline, sizeof(cardline), stdin))
- printcard(cardline);
+ while (--argc) {
+ argv++;
+ printcard(*argv, strlen(*argv));
+ }
+ } else {
+ char cardline[1024];
+ while (fgets(cardline, sizeof(cardline), stdin)) {
+ char *p = cardline;
+ size_t len = strlen(p);
+ while (len > 0) {
+ size_t amt = len > COLUMNS ? COLUMNS : len;
+ printcard(p, amt);
+ p += amt;
+ len -= amt;
+ }
+ }
+ }
exit(0);
}
-#define COLUMNS 48
-
void
-printcard(char *str)
+printcard(char *str, size_t len)
{
static const char rowchars[] = " 123456789";
int i, row;
- char *p;
-
- /* ruthlessly remove newlines and truncate at 48 characters. */
- str[strcspn(str, "\n")] = '\0';
+ char *p, *end;
- if (strlen(str) > COLUMNS)
- str[COLUMNS] = '\0';
+ end = str + len;
/* make string upper case. */
- for (p = str; *p; ++p)
+ for (p = str; p < end; ++p)
if (isascii(*p) && islower(*p))
*p = toupper(*p);
@@ -163,8 +170,8 @@ printcard(char *str)
*/
p = str;
putchar('/');
- for (i = 1; *p; i++, p++)
- if (holes[(int)*p])
+ for (i = 1; p < end; i++, p++)
+ if (holes[(unsigned char)*p])
putchar(*p);
else
putchar(' ');
@@ -181,8 +188,8 @@ printcard(char *str)
*/
for (row = 0; row <= 11; ++row) {
putchar('|');
- for (i = 0, p = str; *p; i++, p++) {
- if (bit(holes[(int)*p], 11 - row))
+ for (i = 0, p = str; p < end; i++, p++) {
+ if (bit(holes[(unsigned char)*p], 11 - row))
putchar(']');
else
putchar(rowchars[row]);