summaryrefslogtreecommitdiff
path: root/games/bcd
diff options
context:
space:
mode:
authorTed Unangst <tedu@cvs.openbsd.org>2014-11-04 17:50:24 +0000
committerTed Unangst <tedu@cvs.openbsd.org>2014-11-04 17:50:24 +0000
commitaa0f70905d7663ad28d8f954f64f9abaebfe1041 (patch)
treed472b608a23ff976f471a36671e6f8bc3f07a3f4 /games/bcd
parent9bc5438d3f8a85c95a135e88c7b9cf8627b60b49 (diff)
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
Diffstat (limited to 'games/bcd')
-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]);