-/* $NetBSD: hack.end.c,v 1.11 2009/06/07 18:30:39 dholland Exp $ */
+/* $NetBSD: hack.end.c,v 1.17 2011/08/06 20:42:43 dholland Exp $ */
/*
* Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: hack.end.c,v 1.11 2009/06/07 18:30:39 dholland Exp $");
+__RCSID("$NetBSD: hack.end.c,v 1.17 2011/08/06 20:42:43 dholland Exp $");
#endif /* not lint */
#include <signal.h>
#include <stdlib.h>
#include "hack.h"
#include "extern.h"
-#define Sprintf (void) sprintf
+#define Snprintf (void) snprintf
xchar maxdlevel = 1;
+struct toptenentry;
+
+static void topten(void);
+static void outheader(void);
+static int outentry(int, struct toptenentry *, int);
+static char *itoa(int);
+static const char *ordin(int);
+
int
dodone(void)
{
/* NOTREACHED */
}
-int done_stopprint;
-int done_hup;
+static int done_stopprint;
+static int done_hup;
/*ARGSUSED*/
-void
+static void
done_intr(int n __unused)
{
done_stopprint++;
(void) signal(SIGQUIT, SIG_IGN);
}
-void
+static void
done_hangup(int n)
{
done_hup++;
static char buf[BUFSZ];
pline("You die ...");
if (mtmp->data->mlet == ' ') {
- Sprintf(buf, "the ghost of %s", (char *) mtmp->mextra);
+ Snprintf(buf, sizeof(buf),
+ "the ghost of %s", (char *) mtmp->mextra);
killer = buf;
} else if (mtmp->mnamelth) {
- Sprintf(buf, "%s called %s",
+ Snprintf(buf, sizeof(buf), "%s called %s",
mtmp->data->mname, NAME(mtmp));
killer = buf;
} else if (mtmp->minvis) {
- Sprintf(buf, "invisible %s", mtmp->data->mname);
+ Snprintf(buf, sizeof(buf), "invisible %s", mtmp->data->mname);
killer = buf;
} else
killer = mtmp->data->mname;
}
if (*st1 == 'c')
killer = st1; /* after outrip() */
- settty((char *) 0); /* does a clear_screen() */
+ settty(NULL); /* does a clear_screen() */
if (!done_stopprint)
printf("Goodbye %s %s...\n\n", pl_character, plname);
{
exit(0);
}
-#define newttentry() (struct toptenentry *) alloc(sizeof(struct toptenentry))
+#define newttentry() ((struct toptenentry *) alloc(sizeof(struct toptenentry)))
#define NAMSZ 8
#define DTHSZ 40
#define PERSMAX 1
char name[NAMSZ + 1];
char death[DTHSZ + 1];
char date[7];/* yymmdd */
-} *tt_head;
+};
-void
+static struct toptenentry *tt_head;
+
+static void
topten(void)
{
int uid = getuid();
(void) unlink(reclock);
}
-void
+static void
outheader(void)
{
char linebuf[BUFSZ];
puts(linebuf);
}
-/* so>0: standout line; so=0: ordinary line; so<0: no output, return lth */
-int
+/* so>0: standout line; so=0: ordinary line; so<0: no output, return length */
+static int
outentry(int rank, struct toptenentry *t1, int so)
{
boolean quit = FALSE, gotkilled = FALSE, starv = FALSE;
char linebuf[BUFSZ];
+ size_t pos;
+
+ linebuf[0] = '\0';
+ pos = 0;
- linebuf[0] = 0;
if (rank)
- Sprintf(eos(linebuf), "%3d", rank);
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, "%3d", rank);
else
- Sprintf(eos(linebuf), " ");
- Sprintf(eos(linebuf), " %6ld %8s", t1->points, t1->name);
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, " ");
+ pos = strlen(linebuf);
+
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, " %6ld %8s",
+ t1->points, t1->name);
+ pos = strlen(linebuf);
+
if (t1->plchar == 'X')
- Sprintf(eos(linebuf), " ");
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, " ");
else
- Sprintf(eos(linebuf), "-%c ", t1->plchar);
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, "-%c ", t1->plchar);
+ pos = strlen(linebuf);
+
if (!strncmp("escaped", t1->death, 7)) {
if (!strcmp(" (with amulet)", t1->death + 7))
- Sprintf(eos(linebuf), "escaped the dungeon with amulet");
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "escaped the dungeon with amulet");
else
- Sprintf(eos(linebuf), "escaped the dungeon [max level %d]",
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "escaped the dungeon [max level %d]",
t1->maxlvl);
+ pos = strlen(linebuf);
} else {
if (!strncmp(t1->death, "quit", 4)) {
quit = TRUE;
if (t1->maxhp < 3 * t1->hp && t1->maxlvl < 4)
- Sprintf(eos(linebuf), "cravenly gave up");
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "cravenly gave up");
else
- Sprintf(eos(linebuf), "quit");
- } else if (!strcmp(t1->death, "choked"))
- Sprintf(eos(linebuf), "choked on %s food",
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "quit");
+ } else if (!strcmp(t1->death, "choked")) {
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "choked on %s food",
(t1->sex == 'F') ? "her" : "his");
- else if (!strncmp(t1->death, "starv", 5))
- Sprintf(eos(linebuf), "starved to death"), starv = TRUE;
- else
- Sprintf(eos(linebuf), "was killed"), gotkilled = TRUE;
- Sprintf(eos(linebuf), " on%s level %d",
+ } else if (!strncmp(t1->death, "starv", 5)) {
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "starved to death");
+ starv = TRUE;
+ } else {
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "was killed");
+ gotkilled = TRUE;
+ }
+ pos = strlen(linebuf);
+
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, " on%s level %d",
(gotkilled || starv) ? "" : " dungeon", t1->level);
+ pos = strlen(linebuf);
+
if (t1->maxlvl != t1->level)
- Sprintf(eos(linebuf), " [max %d]", t1->maxlvl);
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ " [max %d]", t1->maxlvl);
+ pos = strlen(linebuf);
+
if (quit && t1->death[4])
- Sprintf(eos(linebuf), t1->death + 4);
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ "%s", t1->death + 4);
+ pos = strlen(linebuf);
}
- if (gotkilled)
- Sprintf(eos(linebuf), " by %s%s",
+ if (gotkilled) {
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos, " by %s%s",
(!strncmp(t1->death, "trick", 5) || !strncmp(t1->death, "the ", 4))
? "" :
strchr(vowels, *t1->death) ? "an " : "a ",
t1->death);
- Sprintf(eos(linebuf), ".");
+ pos = strlen(linebuf);
+ }
+ strlcat(linebuf, ".", sizeof(linebuf));
+ pos = strlen(linebuf);
if (t1->maxhp) {
- char *bp = eos(linebuf);
char hpbuf[10];
- int hppos;
- Sprintf(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-");
+ unsigned hppos;
+
+ strlcpy(hpbuf, (t1->hp > 0) ? itoa(t1->hp) : "-", sizeof(hpbuf));
hppos = COLNO - 7 - strlen(hpbuf);
- if (bp <= linebuf + hppos) {
- while (bp < linebuf + hppos)
- *bp++ = ' ';
- (void) strcpy(bp, hpbuf);
- Sprintf(eos(bp), " [%d]", t1->maxhp);
+ if (pos <= hppos) {
+ while (pos < hppos)
+ linebuf[pos++] = ' ';
+ (void) strlcpy(linebuf+pos, hpbuf, sizeof(linebuf)-pos);
+ pos = strlen(linebuf);
+ Snprintf(linebuf+pos, sizeof(linebuf)-pos,
+ " [%d]", t1->maxhp);
+ pos = strlen(linebuf);
}
}
if (so == 0)
puts(linebuf);
else if (so > 0) {
- char *bp = eos(linebuf);
if (so >= COLNO)
so = COLNO - 1;
- while (bp < linebuf + so)
- *bp++ = ' ';
- *bp = 0;
+ while (pos < (unsigned)so)
+ linebuf[pos++] = ' ';
+ linebuf[pos] = '\0';
standoutbeg();
fputs(linebuf, stdout);
standoutend();
(void) putchar('\n');
}
- return (strlen(linebuf));
+ return /*(strlen(linebuf))*/ pos;
}
-char *
+static char *
itoa(int a)
{
static char buf[12];
- Sprintf(buf, "%d", a);
+ Snprintf(buf, sizeof(buf), "%d", a);
return (buf);
}
-const char *
+static const char *
ordin(int n)
{
int dg = n % 10;
#ifdef NOSAVEONHANGUP
/*ARGSUSED*/
void
-hangup(int n __unused)
+hang_up(int n __unused)
{
(void) signal(SIGINT, SIG_IGN);
clearlocks();
break;
}
}
- free((char *) t1);
+ free(t1);
}
#ifdef nonsense
totchars[totcharct] = 0;