Home | History | Annotate | Line # | Download | only in hack
hack.end.c revision 1.12
      1  1.12  dholland /*	$NetBSD: hack.end.c,v 1.12 2009/06/07 20:13:18 dholland Exp $	*/
      2   1.4  christos 
      3   1.2   mycroft /*
      4   1.6       jsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5   1.6       jsm  * Amsterdam
      6   1.6       jsm  * All rights reserved.
      7   1.6       jsm  *
      8   1.6       jsm  * Redistribution and use in source and binary forms, with or without
      9   1.6       jsm  * modification, are permitted provided that the following conditions are
     10   1.6       jsm  * met:
     11   1.6       jsm  *
     12   1.6       jsm  * - Redistributions of source code must retain the above copyright notice,
     13   1.6       jsm  * this list of conditions and the following disclaimer.
     14   1.6       jsm  *
     15   1.6       jsm  * - Redistributions in binary form must reproduce the above copyright
     16   1.6       jsm  * notice, this list of conditions and the following disclaimer in the
     17   1.6       jsm  * documentation and/or other materials provided with the distribution.
     18   1.6       jsm  *
     19   1.6       jsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20   1.6       jsm  * Informatica, nor the names of its contributors may be used to endorse or
     21   1.6       jsm  * promote products derived from this software without specific prior
     22   1.6       jsm  * written permission.
     23   1.6       jsm  *
     24   1.6       jsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25   1.6       jsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26   1.6       jsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27   1.6       jsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28   1.6       jsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29   1.6       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30   1.6       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31   1.6       jsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32   1.6       jsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33   1.6       jsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34   1.6       jsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.6       jsm  */
     36   1.6       jsm 
     37   1.6       jsm /*
     38   1.6       jsm  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39   1.6       jsm  * All rights reserved.
     40   1.6       jsm  *
     41   1.6       jsm  * Redistribution and use in source and binary forms, with or without
     42   1.6       jsm  * modification, are permitted provided that the following conditions
     43   1.6       jsm  * are met:
     44   1.6       jsm  * 1. Redistributions of source code must retain the above copyright
     45   1.6       jsm  *    notice, this list of conditions and the following disclaimer.
     46   1.6       jsm  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.6       jsm  *    notice, this list of conditions and the following disclaimer in the
     48   1.6       jsm  *    documentation and/or other materials provided with the distribution.
     49   1.6       jsm  * 3. The name of the author may not be used to endorse or promote products
     50   1.6       jsm  *    derived from this software without specific prior written permission.
     51   1.6       jsm  *
     52   1.6       jsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53   1.6       jsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54   1.6       jsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55   1.6       jsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56   1.6       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57   1.6       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58   1.6       jsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59   1.6       jsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60   1.6       jsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61   1.6       jsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.2   mycroft  */
     63   1.2   mycroft 
     64   1.4  christos #include <sys/cdefs.h>
     65   1.2   mycroft #ifndef lint
     66  1.12  dholland __RCSID("$NetBSD: hack.end.c,v 1.12 2009/06/07 20:13:18 dholland Exp $");
     67   1.4  christos #endif				/* not lint */
     68   1.1       cgd 
     69   1.4  christos #include <signal.h>
     70   1.4  christos #include <unistd.h>
     71   1.4  christos #include <stdlib.h>
     72   1.1       cgd #include "hack.h"
     73   1.4  christos #include "extern.h"
     74  1.12  dholland #define	Snprintf	(void) snprintf
     75   1.1       cgd 
     76   1.4  christos xchar           maxdlevel = 1;
     77   1.4  christos 
     78   1.4  christos int
     79  1.11  dholland dodone(void)
     80   1.4  christos {
     81   1.4  christos 	done1(0);
     82   1.4  christos 	return 0;
     83   1.4  christos }
     84   1.4  christos 
     85   1.1       cgd 
     86   1.4  christos /*ARGSUSED*/
     87   1.1       cgd void
     88  1.11  dholland done1(int n __unused)
     89   1.1       cgd {
     90   1.4  christos 	(void) signal(SIGINT, SIG_IGN);
     91   1.1       cgd 	pline("Really quit?");
     92   1.4  christos 	if (readchar() != 'y') {
     93   1.4  christos 		(void) signal(SIGINT, done1);
     94   1.1       cgd 		clrlin();
     95   1.1       cgd 		(void) fflush(stdout);
     96   1.4  christos 		if (multi > 0)
     97   1.4  christos 			nomul(0);
     98   1.1       cgd 		return;
     99   1.1       cgd 	}
    100   1.1       cgd 	done("quit");
    101   1.1       cgd 	/* NOTREACHED */
    102   1.1       cgd }
    103   1.1       cgd 
    104   1.4  christos int             done_stopprint;
    105   1.4  christos int             done_hup;
    106   1.1       cgd 
    107   1.4  christos /*ARGSUSED*/
    108   1.1       cgd void
    109  1.11  dholland done_intr(int n __unused)
    110   1.4  christos {
    111   1.1       cgd 	done_stopprint++;
    112   1.1       cgd 	(void) signal(SIGINT, SIG_IGN);
    113   1.1       cgd 	(void) signal(SIGQUIT, SIG_IGN);
    114   1.1       cgd }
    115   1.1       cgd 
    116   1.1       cgd void
    117  1.11  dholland done_hangup(int n)
    118   1.4  christos {
    119   1.1       cgd 	done_hup++;
    120   1.1       cgd 	(void) signal(SIGHUP, SIG_IGN);
    121   1.4  christos 	done_intr(n);
    122   1.1       cgd }
    123   1.1       cgd 
    124   1.4  christos void
    125  1.11  dholland done_in_by(struct monst *mtmp)
    126   1.4  christos {
    127   1.4  christos 	static char     buf[BUFSZ];
    128   1.1       cgd 	pline("You die ...");
    129   1.4  christos 	if (mtmp->data->mlet == ' ') {
    130  1.12  dholland 		Snprintf(buf, sizeof(buf),
    131  1.12  dholland 			"the ghost of %s", (char *) mtmp->mextra);
    132   1.1       cgd 		killer = buf;
    133   1.4  christos 	} else if (mtmp->mnamelth) {
    134  1.12  dholland 		Snprintf(buf, sizeof(buf), "%s called %s",
    135   1.1       cgd 			mtmp->data->mname, NAME(mtmp));
    136   1.1       cgd 		killer = buf;
    137   1.4  christos 	} else if (mtmp->minvis) {
    138  1.12  dholland 		Snprintf(buf, sizeof(buf), "invisible %s", mtmp->data->mname);
    139   1.1       cgd 		killer = buf;
    140   1.4  christos 	} else
    141   1.4  christos 		killer = mtmp->data->mname;
    142   1.1       cgd 	done("died");
    143   1.1       cgd }
    144   1.1       cgd 
    145   1.4  christos /*
    146   1.4  christos  * called with arg "died", "drowned", "escaped", "quit", "choked",
    147   1.4  christos  * "panicked", "burned", "starved" or "tricked"
    148   1.4  christos  */
    149   1.1       cgd /* Be careful not to call panic from here! */
    150   1.4  christos void
    151  1.11  dholland done(const char *st1)
    152   1.1       cgd {
    153   1.1       cgd 
    154   1.1       cgd #ifdef WIZARD
    155   1.4  christos 	if (wizard && *st1 == 'd') {
    156   1.1       cgd 		u.uswldtim = 0;
    157   1.4  christos 		if (u.uhpmax < 0)
    158   1.4  christos 			u.uhpmax = 100;	/* arbitrary */
    159   1.1       cgd 		u.uhp = u.uhpmax;
    160   1.1       cgd 		pline("For some reason you are still alive.");
    161   1.1       cgd 		flags.move = 0;
    162   1.4  christos 		if (multi > 0)
    163   1.4  christos 			multi = 0;
    164   1.4  christos 		else
    165   1.4  christos 			multi = -1;
    166   1.1       cgd 		flags.botl = 1;
    167   1.1       cgd 		return;
    168   1.1       cgd 	}
    169   1.4  christos #endif	/* WIZARD */
    170   1.1       cgd 	(void) signal(SIGINT, done_intr);
    171   1.1       cgd 	(void) signal(SIGQUIT, done_intr);
    172   1.1       cgd 	(void) signal(SIGHUP, done_hangup);
    173   1.4  christos 	if (*st1 == 'q' && u.uhp < 1) {
    174   1.1       cgd 		st1 = "died";
    175   1.1       cgd 		killer = "quit while already on Charon's boat";
    176   1.1       cgd 	}
    177   1.4  christos 	if (*st1 == 's')
    178   1.4  christos 		killer = "starvation";
    179   1.4  christos 	else if (*st1 == 'd' && st1[1] == 'r')
    180   1.4  christos 		killer = "drowning";
    181   1.4  christos 	else if (*st1 == 'p')
    182   1.4  christos 		killer = "panic";
    183   1.4  christos 	else if (*st1 == 't')
    184   1.4  christos 		killer = "trickery";
    185   1.4  christos 	else if (!strchr("bcd", *st1))
    186   1.4  christos 		killer = st1;
    187   1.1       cgd 	paybill();
    188   1.1       cgd 	clearlocks();
    189   1.4  christos 	if (flags.toplin == 1)
    190   1.4  christos 		more();
    191   1.4  christos 	if (strchr("bcds", *st1)) {
    192   1.1       cgd #ifdef WIZARD
    193   1.4  christos 		if (!wizard)
    194   1.4  christos #endif	/* WIZARD */
    195   1.4  christos 			savebones();
    196   1.4  christos 		if (!flags.notombstone)
    197   1.1       cgd 			outrip();
    198   1.1       cgd 	}
    199   1.4  christos 	if (*st1 == 'c')
    200   1.4  christos 		killer = st1;	/* after outrip() */
    201   1.1       cgd 	settty((char *) 0);	/* does a clear_screen() */
    202   1.4  christos 	if (!done_stopprint)
    203   1.1       cgd 		printf("Goodbye %s %s...\n\n", pl_character, plname);
    204   1.4  christos 	{
    205   1.4  christos 		long int        tmp;
    206   1.4  christos 		tmp = u.ugold - u.ugold0;
    207   1.4  christos 		if (tmp < 0)
    208   1.4  christos 			tmp = 0;
    209   1.4  christos 		if (*st1 == 'd' || *st1 == 'b')
    210   1.4  christos 			tmp -= tmp / 10;
    211   1.4  christos 		u.urexp += tmp;
    212   1.4  christos 		u.urexp += 50 * maxdlevel;
    213   1.4  christos 		if (maxdlevel > 20)
    214   1.4  christos 			u.urexp += 1000 * ((maxdlevel > 30) ? 10 : maxdlevel - 20);
    215   1.4  christos 	}
    216   1.4  christos 	if (*st1 == 'e') {
    217   1.4  christos 		struct monst   *mtmp;
    218   1.4  christos 		struct obj     *otmp;
    219   1.4  christos 		int             i;
    220   1.4  christos 		unsigned        worthlessct = 0;
    221   1.4  christos 		boolean         has_amulet = FALSE;
    222   1.1       cgd 
    223   1.1       cgd 		killer = st1;
    224   1.1       cgd 		keepdogs();
    225   1.1       cgd 		mtmp = mydogs;
    226   1.4  christos 		if (mtmp) {
    227   1.4  christos 			if (!done_stopprint)
    228   1.4  christos 				printf("You");
    229   1.4  christos 			while (mtmp) {
    230   1.4  christos 				if (!done_stopprint)
    231   1.1       cgd 					printf(" and %s", monnam(mtmp));
    232   1.4  christos 				if (mtmp->mtame)
    233   1.1       cgd 					u.urexp += mtmp->mhp;
    234   1.1       cgd 				mtmp = mtmp->nmon;
    235   1.1       cgd 			}
    236   1.4  christos 			if (!done_stopprint)
    237   1.4  christos 				printf("\nescaped from the dungeon with %ld points,\n",
    238   1.4  christos 				       u.urexp);
    239   1.4  christos 		} else if (!done_stopprint)
    240   1.4  christos 			printf("You escaped from the dungeon with %ld points,\n",
    241   1.4  christos 			       u.urexp);
    242   1.4  christos 		for (otmp = invent; otmp; otmp = otmp->nobj) {
    243   1.4  christos 			if (otmp->olet == GEM_SYM) {
    244   1.1       cgd 				objects[otmp->otyp].oc_name_known = 1;
    245   1.4  christos 				i = otmp->quan * objects[otmp->otyp].g_val;
    246   1.4  christos 				if (i == 0) {
    247   1.1       cgd 					worthlessct += otmp->quan;
    248   1.1       cgd 					continue;
    249   1.1       cgd 				}
    250   1.1       cgd 				u.urexp += i;
    251   1.4  christos 				if (!done_stopprint)
    252   1.4  christos 					printf("\t%s (worth %d Zorkmids),\n",
    253   1.4  christos 					       doname(otmp), i);
    254   1.4  christos 			} else if (otmp->olet == AMULET_SYM) {
    255   1.1       cgd 				otmp->known = 1;
    256   1.1       cgd 				i = (otmp->spe < 0) ? 2 : 5000;
    257   1.1       cgd 				u.urexp += i;
    258   1.4  christos 				if (!done_stopprint)
    259   1.4  christos 					printf("\t%s (worth %d Zorkmids),\n",
    260   1.4  christos 					       doname(otmp), i);
    261   1.4  christos 				if (otmp->spe >= 0) {
    262   1.1       cgd 					has_amulet = TRUE;
    263   1.1       cgd 					killer = "escaped (with amulet)";
    264   1.1       cgd 				}
    265   1.1       cgd 			}
    266   1.1       cgd 		}
    267   1.4  christos 		if (worthlessct)
    268   1.4  christos 			if (!done_stopprint)
    269   1.4  christos 				printf("\t%u worthless piece%s of coloured glass,\n",
    270   1.4  christos 				       worthlessct, plur(worthlessct));
    271   1.4  christos 		if (has_amulet)
    272   1.4  christos 			u.urexp *= 2;
    273   1.4  christos 	} else if (!done_stopprint)
    274   1.4  christos 		printf("You %s on dungeon level %d with %ld points,\n",
    275   1.4  christos 		       st1, dlevel, u.urexp);
    276   1.4  christos 	if (!done_stopprint)
    277   1.4  christos 		printf("and %ld piece%s of gold, after %ld move%s.\n",
    278   1.4  christos 		       u.ugold, plur(u.ugold), moves, plur(moves));
    279   1.4  christos 	if (!done_stopprint)
    280   1.4  christos 		printf("You were level %u with a maximum of %d hit points when you %s.\n",
    281   1.4  christos 		       u.ulevel, u.uhpmax, st1);
    282   1.4  christos 	if (*st1 == 'e' && !done_stopprint) {
    283   1.1       cgd 		getret();	/* all those pieces of coloured glass ... */
    284   1.1       cgd 		cls();
    285   1.1       cgd 	}
    286   1.1       cgd #ifdef WIZARD
    287   1.4  christos 	if (!wizard)
    288   1.4  christos #endif	/* WIZARD */
    289   1.1       cgd 		topten();
    290   1.4  christos 	if (done_stopprint)
    291   1.4  christos 		printf("\n\n");
    292   1.1       cgd 	exit(0);
    293   1.1       cgd }
    294   1.1       cgd 
    295   1.1       cgd #define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
    296   1.1       cgd #define	NAMSZ	8
    297   1.1       cgd #define	DTHSZ	40
    298   1.1       cgd #define	PERSMAX	1
    299   1.1       cgd #define	POINTSMIN	1	/* must be > 0 */
    300   1.1       cgd #define	ENTRYMAX	100	/* must be >= 10 */
    301   1.1       cgd #define	PERS_IS_UID		/* delete for PERSMAX per name; now per uid */
    302   1.1       cgd struct toptenentry {
    303   1.1       cgd 	struct toptenentry *tt_next;
    304   1.4  christos 	long int        points;
    305   1.4  christos 	int             level, maxlvl, hp, maxhp;
    306   1.4  christos 	int             uid;
    307   1.4  christos 	char            plchar;
    308   1.4  christos 	char            sex;
    309   1.4  christos 	char            name[NAMSZ + 1];
    310   1.4  christos 	char            death[DTHSZ + 1];
    311   1.4  christos 	char            date[7];/* yymmdd */
    312   1.4  christos }              *tt_head;
    313   1.4  christos 
    314   1.4  christos void
    315  1.11  dholland topten(void)
    316   1.4  christos {
    317   1.4  christos 	int             uid = getuid();
    318   1.4  christos 	int             rank, rank0 = -1, rank1 = 0;
    319   1.4  christos 	int             occ_cnt = PERSMAX;
    320   1.4  christos 	struct toptenentry *t0, *t1, *tprev;
    321   1.5       jsm 	const char     *recfile = RECORD;
    322   1.5       jsm 	const char     *reclock = "record_lock";
    323   1.4  christos 	int             sleepct = 300;
    324   1.4  christos 	FILE           *rfile;
    325   1.4  christos 	int 		flg = 0;
    326   1.1       cgd #define	HUP	if(!done_hup)
    327   1.4  christos 	while (link(recfile, reclock) == -1) {
    328   1.4  christos 		HUP             perror(reclock);
    329   1.4  christos 		if (!sleepct--) {
    330   1.4  christos 			HUP             puts("I give up. Sorry.");
    331   1.4  christos 			HUP             puts("Perhaps there is an old record_lock around?");
    332   1.1       cgd 			return;
    333   1.1       cgd 		}
    334   1.4  christos 		HUP             printf("Waiting for access to record file. (%d)\n",
    335   1.4  christos 				                       sleepct);
    336   1.4  christos 		HUP(void) fflush(stdout);
    337   1.1       cgd 		sleep(1);
    338   1.1       cgd 	}
    339   1.4  christos 	if (!(rfile = fopen(recfile, "r"))) {
    340   1.4  christos 		HUP             puts("Cannot open record file!");
    341   1.1       cgd 		goto unlock;
    342   1.1       cgd 	}
    343   1.4  christos 	HUP(void) putchar('\n');
    344   1.1       cgd 
    345   1.1       cgd 	/* create a new 'topten' entry */
    346   1.1       cgd 	t0 = newttentry();
    347   1.1       cgd 	t0->level = dlevel;
    348   1.1       cgd 	t0->maxlvl = maxdlevel;
    349   1.1       cgd 	t0->hp = u.uhp;
    350   1.1       cgd 	t0->maxhp = u.uhpmax;
    351   1.1       cgd 	t0->points = u.urexp;
    352   1.1       cgd 	t0->plchar = pl_character[0];
    353   1.1       cgd 	t0->sex = (flags.female ? 'F' : 'M');
    354   1.1       cgd 	t0->uid = uid;
    355   1.1       cgd 	(void) strncpy(t0->name, plname, NAMSZ);
    356   1.1       cgd 	(t0->name)[NAMSZ] = 0;
    357   1.1       cgd 	(void) strncpy(t0->death, killer, DTHSZ);
    358   1.1       cgd 	(t0->death)[DTHSZ] = 0;
    359  1.10  ginsbach 	(void) strcpy(t0->date, getdatestr());
    360   1.1       cgd 
    361   1.1       cgd 	/* assure minimum number of points */
    362   1.4  christos 	if (t0->points < POINTSMIN)
    363   1.1       cgd 		t0->points = 0;
    364   1.1       cgd 
    365   1.1       cgd 	t1 = tt_head = newttentry();
    366   1.1       cgd 	tprev = 0;
    367   1.1       cgd 	/* rank0: -1 undefined, 0 not_on_list, n n_th on list */
    368   1.4  christos 	for (rank = 1;;) {
    369   1.4  christos 		if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
    370   1.4  christos 			   t1->date, &t1->uid,
    371   1.4  christos 			   &t1->level, &t1->maxlvl,
    372   1.4  christos 			   &t1->hp, &t1->maxhp, &t1->points,
    373   1.4  christos 			   &t1->plchar, &t1->sex, t1->name, t1->death) != 11
    374   1.4  christos 		    || t1->points < POINTSMIN)
    375   1.1       cgd 			t1->points = 0;
    376   1.4  christos 		if (rank0 < 0 && t1->points < t0->points) {
    377   1.4  christos 			rank0 = rank++;
    378   1.4  christos 			if (tprev == 0)
    379   1.4  christos 				tt_head = t0;
    380   1.4  christos 			else
    381   1.4  christos 				tprev->tt_next = t0;
    382   1.4  christos 			t0->tt_next = t1;
    383   1.4  christos 			occ_cnt--;
    384   1.4  christos 			flg++;	/* ask for a rewrite */
    385   1.4  christos 		} else
    386   1.4  christos 			tprev = t1;
    387   1.4  christos 		if (t1->points == 0)
    388   1.4  christos 			break;
    389   1.4  christos 		if (
    390   1.1       cgd #ifdef PERS_IS_UID
    391   1.4  christos 		    t1->uid == t0->uid &&
    392   1.1       cgd #else
    393   1.4  christos 		    strncmp(t1->name, t0->name, NAMSZ) == 0 &&
    394   1.4  christos #endif	/* PERS_IS_UID */
    395   1.4  christos 		    t1->plchar == t0->plchar && --occ_cnt <= 0) {
    396   1.4  christos 			if (rank0 < 0) {
    397   1.4  christos 				rank0 = 0;
    398   1.4  christos 				rank1 = rank;
    399   1.4  christos 				HUP             printf("You didn't beat your previous score of %ld points.\n\n",
    400   1.4  christos 						                t1->points);
    401   1.4  christos 			}
    402   1.4  christos 			if (occ_cnt < 0) {
    403   1.4  christos 				flg++;
    404   1.4  christos 				continue;
    405   1.4  christos 			}
    406   1.1       cgd 		}
    407   1.4  christos 		if (rank <= ENTRYMAX) {
    408   1.4  christos 			t1 = t1->tt_next = newttentry();
    409   1.4  christos 			rank++;
    410   1.4  christos 		}
    411   1.4  christos 		if (rank > ENTRYMAX) {
    412   1.4  christos 			t1->points = 0;
    413   1.4  christos 			break;
    414   1.1       cgd 		}
    415   1.1       cgd 	}
    416   1.4  christos 	if (flg) {		/* rewrite record file */
    417   1.1       cgd 		(void) fclose(rfile);
    418   1.4  christos 		if (!(rfile = fopen(recfile, "w"))) {
    419   1.4  christos 			HUP             puts("Cannot write record file\n");
    420   1.1       cgd 			goto unlock;
    421   1.1       cgd 		}
    422   1.4  christos 		if (!done_stopprint)
    423   1.4  christos 			if (rank0 > 0) {
    424   1.4  christos 				if (rank0 <= 10)
    425   1.4  christos 					puts("You made the top ten list!\n");
    426   1.4  christos 				else
    427   1.4  christos 					printf("You reached the %d%s place on the top %d list.\n\n",
    428   1.4  christos 					     rank0, ordin(rank0), ENTRYMAX);
    429   1.4  christos 			}
    430   1.1       cgd 	}
    431   1.4  christos 	if (rank0 == 0)
    432   1.4  christos 		rank0 = rank1;
    433   1.4  christos 	if (rank0 <= 0)
    434   1.4  christos 		rank0 = rank;
    435   1.4  christos 	if (!done_stopprint)
    436   1.4  christos 		outheader();
    437   1.1       cgd 	t1 = tt_head;
    438   1.4  christos 	for (rank = 1; t1->points != 0; rank++, t1 = t1->tt_next) {
    439   1.4  christos 		if (flg)
    440   1.4  christos 			fprintf(rfile, "%6s %d %d %d %d %d %ld %c%c %s,%s\n",
    441   1.4  christos 				t1->date, t1->uid,
    442   1.4  christos 				t1->level, t1->maxlvl,
    443   1.4  christos 				t1->hp, t1->maxhp, t1->points,
    444   1.4  christos 				t1->plchar, t1->sex, t1->name, t1->death);
    445   1.4  christos 		if (done_stopprint)
    446   1.4  christos 			continue;
    447   1.9  dholland 		if (rank > (int)flags.end_top &&
    448   1.9  dholland 		    (rank < rank0 - (int)flags.end_around || rank > rank0 + (int)flags.end_around)
    449   1.4  christos 		    && (!flags.end_own ||
    450   1.1       cgd #ifdef PERS_IS_UID
    451   1.4  christos 			t1->uid != t0->uid))
    452   1.1       cgd #else
    453   1.4  christos 			strncmp(t1->name, t0->name, NAMSZ)))
    454   1.4  christos #endif	/* PERS_IS_UID */
    455   1.4  christos 			continue;
    456   1.9  dholland 		if (rank == rank0 - (int)flags.end_around &&
    457   1.9  dholland 		    rank0 > (int)flags.end_top + (int)flags.end_around + 1 &&
    458   1.4  christos 		    !flags.end_own)
    459   1.4  christos 			(void) putchar('\n');
    460   1.4  christos 		if (rank != rank0)
    461   1.4  christos 			(void) outentry(rank, t1, 0);
    462   1.4  christos 		else if (!rank1)
    463   1.4  christos 			(void) outentry(rank, t1, 1);
    464   1.4  christos 		else {
    465   1.4  christos 			int             t0lth = outentry(0, t0, -1);
    466   1.4  christos 			int             t1lth = outentry(rank, t1, t0lth);
    467   1.4  christos 			if (t1lth > t0lth)
    468   1.4  christos 				t0lth = t1lth;
    469   1.4  christos 			(void) outentry(0, t0, t0lth);
    470   1.4  christos 		}
    471   1.1       cgd 	}
    472   1.4  christos 	if (rank0 >= rank)
    473   1.4  christos 		if (!done_stopprint)
    474   1.4  christos 			(void) outentry(0, t0, 1);
    475   1.1       cgd 	(void) fclose(rfile);
    476   1.7  christos 	free(t0);
    477   1.1       cgd unlock:
    478   1.1       cgd 	(void) unlink(reclock);
    479   1.1       cgd }
    480   1.1       cgd 
    481   1.4  christos void
    482  1.11  dholland outheader(void)
    483   1.4  christos {
    484   1.4  christos 	char            linebuf[BUFSZ];
    485   1.4  christos 	char           *bp;
    486   1.1       cgd 	(void) strcpy(linebuf, "Number Points  Name");
    487   1.1       cgd 	bp = eos(linebuf);
    488   1.4  christos 	while (bp < linebuf + COLNO - 9)
    489   1.4  christos 		*bp++ = ' ';
    490   1.1       cgd 	(void) strcpy(bp, "Hp [max]");
    491   1.1       cgd 	puts(linebuf);
    492   1.1       cgd }
    493   1.1       cgd 
    494  1.12  dholland /* so>0: standout line; so=0: ordinary line; so<0: no output, return length */
    495   1.1       cgd int
    496   1.5       jsm outentry(int rank, struct toptenentry *t1, int so)
    497   1.4  christos {
    498   1.9  dholland 	boolean         quit = FALSE, gotkilled = FALSE, starv = FALSE;
    499   1.4  christos 	char            linebuf[BUFSZ];
    500  1.12  dholland 	size_t pos;
    501  1.12  dholland 
    502  1.12  dholland 	linebuf[0] = '\0';
    503  1.12  dholland 	pos = 0;
    504   1.9  dholland 
    505   1.4  christos 	if (rank)
    506  1.12  dholland 		Snprintf(linebuf+pos, sizeof(linebuf)-pos, "%3d", rank);
    507   1.4  christos 	else
    508  1.12  dholland 		Snprintf(linebuf+pos, sizeof(linebuf)-pos, "   ");
    509  1.12  dholland 	pos = strlen(linebuf);
    510  1.12  dholland 
    511  1.12  dholland 	Snprintf(linebuf+pos, sizeof(linebuf)-pos, " %6ld %8s",
    512  1.12  dholland 		t1->points, t1->name);
    513  1.12  dholland 	pos = strlen(linebuf);
    514  1.12  dholland 
    515   1.4  christos 	if (t1->plchar == 'X')
    516  1.12  dholland 		Snprintf(linebuf+pos, sizeof(linebuf)-pos, " ");
    517   1.4  christos 	else
    518  1.12  dholland 		Snprintf(linebuf+pos, sizeof(linebuf)-pos, "-%c ", t1->plchar);
    519  1.12  dholland 	pos = strlen(linebuf);
    520  1.12  dholland 
    521   1.4  christos 	if (!strncmp("escaped", t1->death, 7)) {
    522   1.4  christos 		if (!strcmp(" (with amulet)", t1->death + 7))
    523  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    524  1.12  dholland 				"escaped the dungeon with amulet");
    525   1.4  christos 		else
    526  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    527  1.12  dholland 				"escaped the dungeon [max level %d]",
    528   1.4  christos 				t1->maxlvl);
    529  1.12  dholland 		pos = strlen(linebuf);
    530   1.1       cgd 	} else {
    531   1.4  christos 		if (!strncmp(t1->death, "quit", 4)) {
    532   1.4  christos 			quit = TRUE;
    533   1.4  christos 			if (t1->maxhp < 3 * t1->hp && t1->maxlvl < 4)
    534  1.12  dholland 				Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    535  1.12  dholland 					"cravenly gave up");
    536   1.4  christos 			else
    537  1.12  dholland 				Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    538  1.12  dholland 					"quit");
    539  1.12  dholland 		} else if (!strcmp(t1->death, "choked")) {
    540  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    541  1.12  dholland 				"choked on %s food",
    542   1.4  christos 				(t1->sex == 'F') ? "her" : "his");
    543  1.12  dholland 		} else if (!strncmp(t1->death, "starv", 5)) {
    544  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    545  1.12  dholland 				"starved to death");
    546  1.12  dholland 			starv = TRUE;
    547  1.12  dholland 		} else {
    548  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    549  1.12  dholland 				"was killed");
    550  1.12  dholland 			gotkilled = TRUE;
    551  1.12  dholland 		}
    552  1.12  dholland 		pos = strlen(linebuf);
    553  1.12  dholland 
    554  1.12  dholland 		Snprintf(linebuf+pos, sizeof(linebuf)-pos, " on%s level %d",
    555   1.9  dholland 			(gotkilled || starv) ? "" : " dungeon", t1->level);
    556  1.12  dholland 		pos = strlen(linebuf);
    557  1.12  dholland 
    558   1.4  christos 		if (t1->maxlvl != t1->level)
    559  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    560  1.12  dholland 				" [max %d]", t1->maxlvl);
    561  1.12  dholland 		pos = strlen(linebuf);
    562  1.12  dholland 
    563   1.4  christos 		if (quit && t1->death[4])
    564  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    565  1.12  dholland 				 "%s", t1->death + 4);
    566  1.12  dholland 		pos = strlen(linebuf);
    567   1.4  christos 	}
    568  1.12  dholland 	if (gotkilled) {
    569  1.12  dholland 		Snprintf(linebuf+pos, sizeof(linebuf)-pos, " by %s%s",
    570   1.4  christos 			(!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
    571   1.4  christos 			? "" :
    572   1.4  christos 			strchr(vowels, *t1->death) ? "an " : "a ",
    573   1.4  christos 			t1->death);
    574  1.12  dholland 		pos = strlen(linebuf);
    575  1.12  dholland 	}
    576  1.12  dholland 	strlcat(linebuf, ".", sizeof(linebuf));
    577  1.12  dholland 	pos = strlen(linebuf);
    578   1.4  christos 	if (t1->maxhp) {
    579   1.4  christos 		char            hpbuf[10];
    580  1.12  dholland 		unsigned        hppos;
    581  1.12  dholland 
    582  1.12  dholland 		strlcpy(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-", sizeof(hpbuf));
    583   1.4  christos 		hppos = COLNO - 7 - strlen(hpbuf);
    584  1.12  dholland 		if (pos <= hppos) {
    585  1.12  dholland 			while (pos < hppos)
    586  1.12  dholland 				linebuf[pos++] = ' ';
    587  1.12  dholland 			(void) strlcpy(linebuf+pos, hpbuf, sizeof(linebuf)-pos);
    588  1.12  dholland 			pos = strlen(linebuf);
    589  1.12  dholland 			Snprintf(linebuf+pos, sizeof(linebuf)-pos,
    590  1.12  dholland 				" [%d]", t1->maxhp);
    591  1.12  dholland 			pos = strlen(linebuf);
    592   1.4  christos 		}
    593   1.4  christos 	}
    594   1.4  christos 	if (so == 0)
    595   1.4  christos 		puts(linebuf);
    596   1.4  christos 	else if (so > 0) {
    597   1.4  christos 		if (so >= COLNO)
    598   1.4  christos 			so = COLNO - 1;
    599  1.12  dholland 		while (pos < (unsigned)so)
    600  1.12  dholland 			linebuf[pos++] = ' ';
    601  1.12  dholland 		linebuf[pos] = '\0';
    602   1.4  christos 		standoutbeg();
    603   1.4  christos 		fputs(linebuf, stdout);
    604   1.4  christos 		standoutend();
    605   1.4  christos 		(void) putchar('\n');
    606   1.4  christos 	}
    607  1.12  dholland 	return /*(strlen(linebuf))*/ pos;
    608   1.4  christos }
    609   1.4  christos 
    610   1.4  christos char           *
    611  1.11  dholland itoa(int a)
    612   1.4  christos {
    613   1.4  christos 	static char     buf[12];
    614  1.12  dholland 	Snprintf(buf, sizeof(buf), "%d", a);
    615   1.4  christos 	return (buf);
    616   1.4  christos }
    617   1.4  christos 
    618   1.5       jsm const char           *
    619  1.11  dholland ordin(int n)
    620   1.4  christos {
    621   1.9  dholland 	int             dg = n % 10;
    622   1.9  dholland 
    623   1.9  dholland 	return ((dg == 0 || dg > 3 || n / 10 == 1) ? "th" : (dg == 1) ? "st" :
    624   1.9  dholland 		(dg == 2) ? "nd" : "rd");
    625   1.4  christos }
    626   1.4  christos 
    627   1.4  christos void
    628  1.11  dholland clearlocks(void)
    629   1.4  christos {
    630   1.4  christos 	int x;
    631   1.4  christos 	(void) signal(SIGHUP, SIG_IGN);
    632   1.4  christos 	for (x = maxdlevel; x >= 0; x--) {
    633   1.1       cgd 		glo(x);
    634   1.1       cgd 		(void) unlink(lock);	/* not all levels need be present */
    635   1.1       cgd 	}
    636   1.1       cgd }
    637   1.1       cgd 
    638   1.1       cgd #ifdef NOSAVEONHANGUP
    639   1.4  christos /*ARGSUSED*/
    640   1.4  christos void
    641  1.11  dholland hangup(int n __unused)
    642   1.1       cgd {
    643   1.1       cgd 	(void) signal(SIGINT, SIG_IGN);
    644   1.1       cgd 	clearlocks();
    645   1.1       cgd 	exit(1);
    646   1.1       cgd }
    647   1.4  christos #endif	/* NOSAVEONHANGUP */
    648   1.1       cgd 
    649   1.4  christos char           *
    650  1.11  dholland eos(char *s)
    651   1.1       cgd {
    652   1.4  christos 	while (*s)
    653   1.4  christos 		s++;
    654   1.4  christos 	return (s);
    655   1.1       cgd }
    656   1.1       cgd 
    657   1.1       cgd /* it is the callers responsibility to check that there is room for c */
    658   1.4  christos void
    659  1.11  dholland charcat(char *s, int c)
    660   1.4  christos {
    661   1.4  christos 	while (*s)
    662   1.4  christos 		s++;
    663   1.1       cgd 	*s++ = c;
    664   1.1       cgd 	*s = 0;
    665   1.1       cgd }
    666   1.1       cgd 
    667   1.1       cgd /*
    668   1.1       cgd  * Called with args from main if argc >= 0. In this case, list scores as
    669   1.1       cgd  * requested. Otherwise, find scores for the current player (and list them
    670   1.1       cgd  * if argc == -1).
    671   1.1       cgd  */
    672   1.4  christos void
    673  1.11  dholland prscore(int argc, char **argv)
    674   1.4  christos {
    675   1.4  christos 	char          **players = NULL;
    676   1.4  christos 	int             playerct;
    677   1.4  christos 	int             rank;
    678   1.4  christos 	struct toptenentry *t1, *t2;
    679   1.5       jsm 	const char           *recfile = RECORD;
    680   1.4  christos 	FILE           *rfile;
    681   1.4  christos 	int		flg = 0;
    682   1.4  christos 	int             i;
    683   1.1       cgd #ifdef nonsense
    684   1.4  christos 	long            total_score = 0L;
    685   1.4  christos 	char            totchars[10];
    686   1.4  christos 	int             totcharct = 0;
    687   1.4  christos #endif	/* nonsense */
    688   1.4  christos 	int             outflg = (argc >= -1);
    689   1.1       cgd #ifdef PERS_IS_UID
    690   1.4  christos 	int             uid = -1;
    691   1.1       cgd #else
    692   1.4  christos 	char           *player0;
    693   1.4  christos #endif	/* PERS_IS_UID */
    694   1.1       cgd 
    695   1.4  christos 	if (!(rfile = fopen(recfile, "r"))) {
    696   1.1       cgd 		puts("Cannot open record file!");
    697   1.1       cgd 		return;
    698   1.1       cgd 	}
    699   1.4  christos 	if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
    700   1.4  christos 		if (!argv[1][2]) {
    701   1.1       cgd 			argc--;
    702   1.1       cgd 			argv++;
    703   1.4  christos 		} else if (!argv[1][3] && strchr("CFKSTWX", argv[1][2])) {
    704   1.1       cgd 			argv[1]++;
    705   1.1       cgd 			argv[1][0] = '-';
    706   1.4  christos 		} else
    707   1.4  christos 			argv[1] += 2;
    708   1.1       cgd 	}
    709   1.4  christos 	if (argc <= 1) {
    710   1.1       cgd #ifdef PERS_IS_UID
    711   1.1       cgd 		uid = getuid();
    712   1.1       cgd 		playerct = 0;
    713   1.1       cgd #else
    714   1.1       cgd 		player0 = plname;
    715   1.4  christos 		if (!*player0)
    716   1.1       cgd 			player0 = "hackplayer";
    717   1.1       cgd 		playerct = 1;
    718   1.1       cgd 		players = &player0;
    719   1.4  christos #endif	/* PERS_IS_UID */
    720   1.1       cgd 	} else {
    721   1.1       cgd 		playerct = --argc;
    722   1.1       cgd 		players = ++argv;
    723   1.1       cgd 	}
    724   1.4  christos 	if (outflg)
    725   1.4  christos 		putchar('\n');
    726   1.1       cgd 
    727   1.1       cgd 	t1 = tt_head = newttentry();
    728   1.4  christos 	for (rank = 1;; rank++) {
    729   1.4  christos 		if (fscanf(rfile, "%6s %d %d %d %d %d %ld %c%c %[^,],%[^\n]",
    730   1.4  christos 			   t1->date, &t1->uid,
    731   1.4  christos 			   &t1->level, &t1->maxlvl,
    732   1.4  christos 			   &t1->hp, &t1->maxhp, &t1->points,
    733   1.4  christos 			   &t1->plchar, &t1->sex, t1->name, t1->death) != 11)
    734   1.1       cgd 			t1->points = 0;
    735   1.4  christos 		if (t1->points == 0)
    736   1.4  christos 			break;
    737   1.1       cgd #ifdef PERS_IS_UID
    738   1.4  christos 		if (!playerct && t1->uid == uid)
    739   1.1       cgd 			flg++;
    740   1.4  christos 		else
    741   1.4  christos #endif	/* PERS_IS_UID */
    742   1.4  christos 			for (i = 0; i < playerct; i++) {
    743   1.4  christos 				if (strcmp(players[i], "all") == 0 ||
    744   1.4  christos 				strncmp(t1->name, players[i], NAMSZ) == 0 ||
    745   1.4  christos 				    (players[i][0] == '-' &&
    746   1.4  christos 				     players[i][1] == t1->plchar &&
    747   1.4  christos 				     players[i][2] == 0) ||
    748   1.4  christos 				    (digit(players[i][0]) && rank <= atoi(players[i])))
    749   1.4  christos 					flg++;
    750   1.4  christos 			}
    751   1.4  christos 		t1 = t1->tt_next = newttentry();
    752   1.1       cgd 	}
    753   1.1       cgd 	(void) fclose(rfile);
    754   1.4  christos 	if (!flg) {
    755   1.4  christos 		if (outflg) {
    756   1.4  christos 			printf("Cannot find any entries for ");
    757   1.4  christos 			if (playerct < 1)
    758   1.4  christos 				printf("you.\n");
    759   1.4  christos 			else {
    760   1.4  christos 				if (playerct > 1)
    761   1.4  christos 					printf("any of ");
    762   1.4  christos 				for (i = 0; i < playerct; i++)
    763   1.4  christos 					printf("%s%s", players[i], (i < playerct - 1) ? ", " : ".\n");
    764   1.4  christos 				printf("Call is: %s -s [playernames]\n", hname);
    765   1.4  christos 			}
    766   1.1       cgd 		}
    767   1.4  christos 		return;
    768   1.1       cgd 	}
    769   1.4  christos 	if (outflg)
    770   1.4  christos 		outheader();
    771   1.1       cgd 	t1 = tt_head;
    772   1.4  christos 	for (rank = 1; t1->points != 0; rank++, t1 = t2) {
    773   1.1       cgd 		t2 = t1->tt_next;
    774   1.1       cgd #ifdef PERS_IS_UID
    775   1.4  christos 		if (!playerct && t1->uid == uid)
    776   1.1       cgd 			goto outwithit;
    777   1.1       cgd 		else
    778   1.4  christos #endif	/* PERS_IS_UID */
    779   1.4  christos 			for (i = 0; i < playerct; i++) {
    780   1.4  christos 				if (strcmp(players[i], "all") == 0 ||
    781   1.4  christos 				strncmp(t1->name, players[i], NAMSZ) == 0 ||
    782   1.4  christos 				    (players[i][0] == '-' &&
    783   1.4  christos 				     players[i][1] == t1->plchar &&
    784   1.4  christos 				     players[i][2] == 0) ||
    785   1.4  christos 				    (digit(players[i][0]) && rank <= atoi(players[i]))) {
    786   1.1       cgd 			outwithit:
    787   1.4  christos 					if (outflg)
    788   1.4  christos 						(void) outentry(rank, t1, 0);
    789   1.1       cgd #ifdef nonsense
    790   1.4  christos 					total_score += t1->points;
    791   1.4  christos 					if (totcharct < sizeof(totchars) - 1)
    792   1.4  christos 						totchars[totcharct++] = t1->plchar;
    793   1.4  christos #endif	/* nonsense */
    794   1.4  christos 					break;
    795   1.4  christos 				}
    796   1.1       cgd 			}
    797   1.1       cgd 		free((char *) t1);
    798   1.1       cgd 	}
    799   1.1       cgd #ifdef nonsense
    800   1.1       cgd 	totchars[totcharct] = 0;
    801   1.1       cgd 
    802   1.4  christos 	/*
    803   1.4  christos 	 * We would like to determine whether he is experienced. However, the
    804   1.4  christos 	 * information collected here only tells about the scores/roles that
    805   1.4  christos 	 * got into the topten (top 100?). We should maintain a .hacklog or
    806   1.4  christos 	 * something in his home directory.
    807   1.4  christos 	 */
    808   1.1       cgd 	flags.beginner = (total_score < 6000);
    809   1.4  christos 	for (i = 0; i < 6; i++)
    810   1.4  christos 		if (!strchr(totchars, "CFKSTWX"[i])) {
    811   1.4  christos 			flags.beginner = 1;
    812   1.4  christos 			if (!pl_character[0])
    813   1.4  christos 				pl_character[0] = "CFKSTWX"[i];
    814   1.4  christos 			break;
    815   1.4  christos 		}
    816   1.4  christos #endif	/* nonsense */
    817   1.1       cgd }
    818