diff options
author | Miod Vallat <miod@cvs.openbsd.org> | 2015-02-07 01:37:31 +0000 |
---|---|---|
committer | Miod Vallat <miod@cvs.openbsd.org> | 2015-02-07 01:37:31 +0000 |
commit | 6b05e3b41e2fdd25c60be8c8156d579604b2c858 (patch) | |
tree | 8dbe41375189f39c36c72ef8d2c161a7a0f299e5 | |
parent | 47fa25c9b18c2190e627822649ba77cacb73ec71 (diff) |
By popular demand, allow any ELF file to be passed to -d as a word list, in
which case its symbols will be used.
Which means that `hangman -k' is equivalent to `hangman -d /bsd' now.
ok beck@ tedu@
-rw-r--r-- | games/hangman/extern.c | 8 | ||||
-rw-r--r-- | games/hangman/hangman.6 | 8 | ||||
-rw-r--r-- | games/hangman/hangman.h | 12 | ||||
-rw-r--r-- | games/hangman/ksyms.c | 42 | ||||
-rw-r--r-- | games/hangman/main.c | 6 | ||||
-rw-r--r-- | games/hangman/playgame.c | 6 | ||||
-rw-r--r-- | games/hangman/setup.c | 12 |
7 files changed, 50 insertions, 44 deletions
diff --git a/games/hangman/extern.c b/games/hangman/extern.c index 7b66d2cc446..bb6162e9d0a 100644 --- a/games/hangman/extern.c +++ b/games/hangman/extern.c @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.c,v 1.6 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: extern.c,v 1.7 2015/02/07 01:37:30 miod Exp $ */ /* $NetBSD: extern.c,v 1.3 1995/03/23 08:32:41 cgd Exp $ */ /* @@ -68,7 +68,7 @@ FILE *Dict = NULL; off_t Dict_size; -int ksyms; -int ksymfd = -1; -off_t ksymoffs, ksymsize; +int syms; +int symfd = -1; +off_t symoffs, symsize; diff --git a/games/hangman/hangman.6 b/games/hangman/hangman.6 index 51e04330a3b..a59d9694212 100644 --- a/games/hangman/hangman.6 +++ b/games/hangman/hangman.6 @@ -1,4 +1,4 @@ -.\" $OpenBSD: hangman.6,v 1.16 2008/04/05 07:46:05 jmc Exp $ +.\" $OpenBSD: hangman.6,v 1.17 2015/02/07 01:37:30 miod Exp $ .\" .\" Copyright (c) 1983, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)hangman.6 8.1 (Berkeley) 5/31/93 .\" -.Dd $Mdocdate: April 5 2008 $ +.Dd $Mdocdate: February 7 2015 $ .Dt HANGMAN 6 .Os .Sh NAME @@ -53,7 +53,9 @@ The options are as follows: Use the specified .Ar wordlist instead of the default. -A wordlist file must have one word per line. +If the wordlist file is an ELF binary file, its symbols are used as the +word list. +Otherwise, the wordlist file must be a plain text file, with one word per line. Only lowercase words of at least a certain length .Pq typically 6 characters are chosen. diff --git a/games/hangman/hangman.h b/games/hangman/hangman.h index ad528c5435f..e51099c475b 100644 --- a/games/hangman/hangman.h +++ b/games/hangman/hangman.h @@ -1,4 +1,4 @@ -/* $OpenBSD: hangman.h,v 1.7 2008/04/01 21:05:50 miod Exp $ */ +/* $OpenBSD: hangman.h,v 1.8 2015/02/07 01:37:30 miod Exp $ */ /* $NetBSD: hangman.h,v 1.5 1995/04/24 12:23:44 cgd Exp $ */ /* @@ -85,16 +85,16 @@ extern FILE *Dict; extern off_t Dict_size; -extern int ksyms; -extern int ksymfd; -extern off_t ksymoffs, ksymsize; +extern int syms; +extern int symfd; +extern off_t symoffs, symsize; void die(int); void endgame(void); void getguess(void); void getword(void); -void kgetword(void); -int ksetup(void); +void sym_getword(void); +int sym_setup(void); void playgame(void); void prdata(void); void prman(void); diff --git a/games/hangman/ksyms.c b/games/hangman/ksyms.c index 3444cbed89e..7e63a92de8a 100644 --- a/games/hangman/ksyms.c +++ b/games/hangman/ksyms.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ksyms.c,v 1.4 2013/10/15 22:09:29 deraadt Exp $ */ +/* $OpenBSD: ksyms.c,v 1.5 2015/02/07 01:37:30 miod Exp $ */ /* * Copyright (c) 2008 Miodrag Vallat. @@ -30,7 +30,7 @@ int ksyms_elf_parse(); void -kgetword() +sym_getword() { uint tries; off_t pos; @@ -39,10 +39,10 @@ kgetword() size_t symlen; for (tries = 0; tries < MAXBADWORDS; tries++) { - pos = arc4random_uniform(ksymsize); - if (lseek(ksyms, pos + ksymoffs, SEEK_SET) == -1) + pos = arc4random_uniform(symsize); + if (lseek(symfd, pos + symoffs, SEEK_SET) == -1) continue; - buflen = read(ksyms, symbuf, BUFSIZ); + buflen = read(symfd, symbuf, BUFSIZ); if (buflen < 0) continue; @@ -53,8 +53,8 @@ kgetword() * of the string table. We make sure the buffer will be * NUL terminated in all cases. */ - if (buflen + pos >= ksymsize) - buflen = ksymsize - pos; + if (buflen + pos >= symsize) + buflen = symsize - pos; *(end = symbuf + buflen) = '\0'; for (sym = symbuf; *sym != '\0'; sym++) ; @@ -86,15 +86,15 @@ kgetword() } int -ksetup() +sym_setup() { - if ((ksyms = open(Dict_name, O_RDONLY)) < 0) - return ksyms; + if ((symfd = open(Dict_name, O_RDONLY)) < 0) + return -1; if (ksyms_elf_parse() == 0) return 0; - close(ksyms); + close(symfd); errno = ENOEXEC; return -1; } @@ -106,23 +106,23 @@ ksyms_elf_parse() Elf_Shdr sh; uint s; - if (lseek(ksyms, 0, SEEK_SET) == -1) + if (lseek(symfd, 0, SEEK_SET) == -1) return -1; - if (read(ksyms, &eh, sizeof eh) != sizeof eh) + if (read(symfd, &eh, sizeof eh) != sizeof eh) return -1; if (!IS_ELF(eh)) return -1; - if (lseek(ksyms, eh.e_shoff, SEEK_SET) == -1) + if (lseek(symfd, eh.e_shoff, SEEK_SET) == -1) return -1; - ksymoffs = 0; - ksymsize = 0; + symoffs = 0; + symsize = 0; for (s = 0; s < eh.e_shnum; s++) { - if (read(ksyms, &sh, sizeof sh) != sizeof sh) + if (read(symfd, &sh, sizeof sh) != sizeof sh) return -1; /* @@ -131,15 +131,15 @@ ksyms_elf_parse() * names. Just pick the largest one. */ if (sh.sh_type == SHT_STRTAB) { - if (ksymsize > (off_t)sh.sh_size) + if (symsize > (off_t)sh.sh_size) continue; - ksymoffs = (off_t)sh.sh_offset; - ksymsize = (off_t)sh.sh_size; + symoffs = (off_t)sh.sh_offset; + symsize = (off_t)sh.sh_size; } } - if (ksymsize == 0) + if (symsize == 0) return -1; return 0; diff --git a/games/hangman/main.c b/games/hangman/main.c index 206e3cae39f..5e140e736a7 100644 --- a/games/hangman/main.c +++ b/games/hangman/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.11 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.12 2015/02/07 01:37:30 miod Exp $ */ /* $NetBSD: main.c,v 1.3 1995/03/23 08:32:50 cgd Exp $ */ /* @@ -46,13 +46,13 @@ main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "d:hk")) != -1) { switch (ch) { case 'd': - if (ksyms) + if (syms) usage(); else Dict_name = optarg; break; case 'k': - ksyms = 1; + syms = 1; Dict_name = _PATH_KSYMS; break; case 'h': diff --git a/games/hangman/playgame.c b/games/hangman/playgame.c index 853c057062d..05ddf43e570 100644 --- a/games/hangman/playgame.c +++ b/games/hangman/playgame.c @@ -1,4 +1,4 @@ -/* $OpenBSD: playgame.c,v 1.8 2009/10/27 23:59:25 deraadt Exp $ */ +/* $OpenBSD: playgame.c,v 1.9 2015/02/07 01:37:30 miod Exp $ */ /* $NetBSD: playgame.c,v 1.3 1995/03/23 08:32:53 cgd Exp $ */ /*- @@ -41,8 +41,8 @@ playgame(void) { bool *bp; - if (ksyms) - kgetword(); + if (syms) + sym_getword(); else getword(); Errors = 0; diff --git a/games/hangman/setup.c b/games/hangman/setup.c index 8a76cf282be..bb9b1d78001 100644 --- a/games/hangman/setup.c +++ b/games/hangman/setup.c @@ -1,4 +1,4 @@ -/* $OpenBSD: setup.c,v 1.11 2013/08/29 20:22:14 naddy Exp $ */ +/* $OpenBSD: setup.c,v 1.12 2015/02/07 01:37:30 miod Exp $ */ /* $NetBSD: setup.c,v 1.3 1995/03/23 08:32:59 cgd Exp $ */ /*- @@ -58,12 +58,16 @@ setup(void) addstr(*sp); } - if (ksyms) { - if (ksetup() != 0) { + /* always check for an ELF file */ + if (sym_setup() != 0) { + if (syms) { endwin(); err(1, "open %s", Dict_name); } - } else { + } else + syms = 1; + + if (!syms) { if ((Dict = fopen(Dict_name, "r")) == NULL) { endwin(); err(1, "fopen %s", Dict_name); |