From aa0f70905d7663ad28d8f954f64f9abaebfe1041 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 4 Nov 2014 17:50:24 +0000 Subject: 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 --- games/bcd/bcd.c | 53 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 23 deletions(-) (limited to 'games/bcd') 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]); -- cgit v1.2.3