Ported robots game.

This commit is contained in:
Serge Vakulenko
2014-09-12 13:32:16 -07:00
parent e7b77dadc3
commit c5ac22d17f
16 changed files with 397 additions and 357 deletions

View File

@@ -427,6 +427,7 @@ file /games/ppt
file /games/primes
file /games/quiz
file /games/rain
file /games/robots
file /games/rogue
file /games/sail
file /games/teachgammon
@@ -447,6 +448,8 @@ file /games/lib/cfscores
mode 0666
file /games/lib/fortunes.dat
file /games/lib/cards.pck
file /games/lib/robots_roll
mode 0666
dir /games/lib/quiz.k
file /games/lib/quiz.k/africa
file /games/lib/quiz.k/america
@@ -772,6 +775,7 @@ file /share/man/cat6/monop.0
file /share/man/cat6/number.0
file /share/man/cat6/quiz.0
file /share/man/cat6/rain.0
file /share/man/cat6/robots.0
file /share/man/cat6/rogue.0
file /share/man/cat6/sail.0
file /share/man/cat6/trek.0

View File

@@ -12,8 +12,9 @@ CFLAGS += -Werror -Wall -Os
# Programs that live in subdirectories, and have makefiles of their own.
#
SUBDIR = adventure atc backgammon battlestar boggle btlgammon \
cribbage fortune hangman mille monop quiz rogue sail trek
# TODO: robots snake
cribbage fortune hangman mille monop quiz robots rogue \
sail trek
# TODO: snake
# C programs that live in the current directory and do not need
# explicit make lines.

1
src/games/robots/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
robots

View File

@@ -5,27 +5,46 @@
#
# @(#)Makefile 5.2 (Berkeley) 5/15/86
#
DESTDIR=
HDRS= robot.h
CFILES= extern.c init_field.c main.c make_level.c move.c \
move_robs.c play_level.c query.c rnd_pos.c score.c \
flush_in.c
OBJS= extern.o init_field.o main.o make_level.o move.o \
TOPSRC = $(shell cd ../../..; pwd)
include $(TOPSRC)/target.mk
#include $(TOPSRC)/cross.mk
#CFLAGS = -O -DCROSS -g -DFANCY
CFLAGS += -DMAX_PER_UID=5 -Werror -Wall
OBJS = extern.o init_field.o main.o make_level.o move.o \
move_robs.o play_level.o query.o rnd_pos.o score.o \
flush_in.o
DEFS= -DMAX_PER_UID=5
CFLAGS= -O ${DEFS}
SEPFLAG= -i
MAN = robots.0
LIBS = -lcurses -ltermcap -lc
robots: ${OBJS}
${CC} ${SEPFLAG} ${CFLAGS} -o robots ${OBJS} -lcurses -ltermlib
all: robots $(MAN)
lint:
lint -hb ${DEFS} ${CFILES} -lcurses 2>1 > lint.out
robots: $(OBJS)
$(CC) $(LDFLAGS) -o $@.elf $(OBJS) $(LIBS)
$(OBJDUMP) -S $@.elf > $@.dis
$(SIZE) $@.elf
$(ELF2AOUT) $@.elf $@ && rm $@.elf
install: robots
install -s -m 4711 -o daemon robots ${DESTDIR}/usr/games
install -c -m 644 -o daemon /dev/null ${DESTDIR}/usr/games/lib/robots_roll
$(MAN): robots.6
nroff -man $< > $@
clean:
rm -f a.out core *.o robots lint.out errs
rm -f *.o core robots *.0 *.dis
install: all
install robots $(DESTDIR)/games/
install -m 644 $(MAN) $(DESTDIR)/share/man/cat6/
install -c -m 644 /dev/null ${DESTDIR}/games/lib/robots_roll
###
extern.o: extern.c robots.h
flush_in.o: flush_in.c
init_field.o: init_field.c robots.h
main.o: main.c robots.h
make_level.o: make_level.c robots.h
move.o: move.c robots.h
move_robs.o: move_robs.c robots.h
play_level.o: play_level.c robots.h
query.o: query.c robots.h
rnd_pos.o: rnd_pos.c robots.h
score.o: score.c robots.h

View File

@@ -3,12 +3,7 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)extern.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include "robots.h"
#include "robots.h"
bool Dead; /* Player is now dead */
bool Full_clear = TRUE; /* Lots of junk for init_field to clear */

View File

@@ -3,22 +3,18 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)flush_in.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include <curses.h>
#include <curses.h>
/*
* flush_in:
* Flush all pending input.
*/
void
flush_in()
{
# ifdef TIOCFLUSH
#ifdef TIOCFLUSH
ioctl(fileno(stdin), TIOCFLUSH, NULL);
# else TIOCFLUSH
#else
crmode();
# endif TIOCFLUSH
#endif
}

View File

@@ -3,23 +3,17 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)init_field.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include "robots.h"
#include "robots.h"
/*
* init_field:
* Lay down the initial pattern whih is constant across all levels,
* and initialize all the global variables.
*/
void
init_field()
{
register int i;
register WINDOW *wp;
register int j;
static bool first = TRUE;
static char *desc[] = {
"Directions:",

View File

@@ -3,21 +3,71 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "robots.h"
#include <signal.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
All rights reserved.\n";
#endif not lint
#ifndef CROSS
static int
fputchar(int c)
{
return putchar(c);
}
#endif
#ifndef lint
static char sccsid[] = "@(#)main.c 5.1 (Berkeley) 5/30/85";
#endif not lint
/*
* quit:
* Leave the program elegantly.
*/
void
quit(int sig)
{
mvcur(0, COLS - 1, LINES - 1, 0);
#ifndef CROSS
if (CE) {
tputs(CE, 1, fputchar);
endwin();
} else
#endif
{
endwin();
putchar('\n');
}
exit(0);
/* NOTREACHED */
}
# include "robots.h"
# include <signal.h>
# include <ctype.h>
/*
* another:
* See if another game is desired
*/
int
another()
{
register int y;
#ifdef FANCY
if ((Stand_still || Pattern_roll) && !Newscore)
return TRUE;
#endif
if (query("Another game?")) {
if (Full_clear) {
for (y = 1; y <= Num_scores; y++) {
move(y, 1);
clrtoeol();
}
refresh();
}
return TRUE;
}
return FALSE;
}
int
main(ac, av)
int ac;
char **av;
@@ -37,10 +87,12 @@ char **av;
if (isdigit(av[0][0]))
Max_per_uid = atoi(av[0]);
else {
setuid(getuid());
setgid(getgid());
if (setuid(getuid()) < 0)
/*ignore*/;
if (setgid(getgid()) < 0)
/*ignore*/;
Scorefile = av[0];
# ifdef FANCY
#ifdef FANCY
sp = rindex(Scorefile, '/');
if (sp == NULL)
sp = Scorefile;
@@ -50,7 +102,7 @@ char **av;
Stand_still = TRUE;
if (Pattern_roll || Stand_still)
Teleport = TRUE;
# endif
#endif
}
else
for (sp = &av[0][1]; *sp; sp++)
@@ -116,52 +168,6 @@ char **av;
refresh();
score();
} while (another());
quit();
}
/*
* quit:
* Leave the program elegantly.
*/
quit()
{
extern int _putchar();
mvcur(0, COLS - 1, LINES - 1, 0);
if (CE) {
tputs(CE, 1, _putchar);
endwin();
}
else {
endwin();
putchar('\n');
}
exit(0);
/* NOTREACHED */
}
/*
* another:
* See if another game is desired
*/
another()
{
register int y;
#ifdef FANCY
if ((Stand_still || Pattern_roll) && !Newscore)
return TRUE;
#endif
if (query("Another game?")) {
if (Full_clear) {
for (y = 1; y <= Num_scores; y++) {
move(y, 1);
clrtoeol();
}
refresh();
}
return TRUE;
}
return FALSE;
quit(0);
return 0;
}

View File

@@ -3,23 +3,19 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)make_level.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include "robots.h"
#include "robots.h"
#include <strings.h>
/*
* make_level:
* Make the current level
*/
void
make_level()
{
register int i;
register COORD *cp;
register WINDOW *wp;
register int x, *endp;
register int x;
reset_count();
for (i = 1; i < Y_FIELDSIZE; i++)

View File

@@ -3,25 +3,138 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "robots.h"
#include <ctype.h>
#ifndef lint
static char sccsid[] = "@(#)move.c 5.1 (Berkeley) 5/30/85";
#endif not lint
#define ESC '\033'
# include "robots.h"
# include <ctype.h>
/*
* eaten:
* Player would get eaten at this place
*/
int
eaten(pos)
register COORD *pos;
{
register int x, y;
# define ESC '\033'
for (y = pos->y - 1; y <= pos->y + 1; y++) {
if (y <= 0 || y >= Y_FIELDSIZE)
continue;
for (x = pos->x - 1; x <= pos->x + 1; x++) {
if (x <= 0 || x >= X_FIELDSIZE)
continue;
if (Field[y][x] == 1)
return TRUE;
}
}
return FALSE;
}
/*
* must_telep:
* Must I teleport; i.e., is there anywhere I can move without
* being eaten?
*/
int
must_telep()
{
register int x, y;
static COORD newpos;
#ifdef FANCY
if (Stand_still && Num_robots > 1 && eaten(&My_pos))
return TRUE;
#endif
for (y = -1; y <= 1; y++) {
newpos.y = My_pos.y + y;
if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE)
continue;
for (x = -1; x <= 1; x++) {
newpos.x = My_pos.x + x;
if (newpos.x <= 0 || newpos.x >= X_FIELDSIZE)
continue;
if (Field[newpos.y][newpos.x] > 0)
continue;
if (!eaten(&newpos))
return FALSE;
}
}
return TRUE;
}
/*
* do_move:
* Execute a move
*/
int
do_move(dy, dx)
int dy, dx;
{
static COORD newpos;
newpos.y = My_pos.y + dy;
newpos.x = My_pos.x + dx;
if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE ||
newpos.x <= 0 || newpos.x >= X_FIELDSIZE ||
Field[newpos.y][newpos.x] > 0 || eaten(&newpos)) {
if (Running) {
Running = FALSE;
leaveok(stdscr, FALSE);
move(My_pos.y, My_pos.x);
refresh();
}
else {
putchar(CTRL('G'));
reset_count();
}
return FALSE;
}
else if (dy == 0 && dx == 0)
return TRUE;
mvaddch(My_pos.y, My_pos.x, ' ');
My_pos = newpos;
mvaddch(My_pos.y, My_pos.x, PLAYER);
if (!jumping())
refresh();
return TRUE;
}
/*
* reset_count:
* Reset the count variables
*/
void
reset_count()
{
Count = 0;
Running = FALSE;
leaveok(stdscr, FALSE);
refresh();
}
/*
* jumping:
* See if we are jumping, i.e., we should not refresh.
*/
int
jumping()
{
return (Jump && (Count || Running || Waiting));
}
/*
* get_move:
* Get and execute a move from the player
*/
void
get_move()
{
register int c;
register int y, x, lastmove;
static COORD newpos;
#ifdef FANCY
register int lastmove = 0;
#endif
if (Waiting)
return;
@@ -122,7 +235,7 @@ over:
case 'q':
case 'Q':
if (query("Really quit?"))
quit();
quit(0);
refresh();
break;
case 'w':
@@ -142,13 +255,13 @@ teleport:
refresh();
flush_in();
goto ret;
case CTRL(L):
case CTRL('L'):
wrefresh(curscr);
break;
case EOF:
break;
default:
putchar(CTRL(G));
putchar(CTRL('G'));
reset_count();
fflush(stdout);
break;
@@ -159,114 +272,3 @@ ret:
if (--Count == 0)
leaveok(stdscr, FALSE);
}
/*
* must_telep:
* Must I teleport; i.e., is there anywhere I can move without
* being eaten?
*/
must_telep()
{
register int x, y;
static COORD newpos;
#ifdef FANCY
if (Stand_still && Num_robots > 1 && eaten(&My_pos))
return TRUE;
#endif
for (y = -1; y <= 1; y++) {
newpos.y = My_pos.y + y;
if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE)
continue;
for (x = -1; x <= 1; x++) {
newpos.x = My_pos.x + x;
if (newpos.x <= 0 || newpos.x >= X_FIELDSIZE)
continue;
if (Field[newpos.y][newpos.x] > 0)
continue;
if (!eaten(&newpos))
return FALSE;
}
}
return TRUE;
}
/*
* do_move:
* Execute a move
*/
do_move(dy, dx)
int dy, dx;
{
static COORD newpos;
newpos.y = My_pos.y + dy;
newpos.x = My_pos.x + dx;
if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE ||
newpos.x <= 0 || newpos.x >= X_FIELDSIZE ||
Field[newpos.y][newpos.x] > 0 || eaten(&newpos)) {
if (Running) {
Running = FALSE;
leaveok(stdscr, FALSE);
move(My_pos.y, My_pos.x);
refresh();
}
else {
putchar(CTRL(G));
reset_count();
}
return FALSE;
}
else if (dy == 0 && dx == 0)
return TRUE;
mvaddch(My_pos.y, My_pos.x, ' ');
My_pos = newpos;
mvaddch(My_pos.y, My_pos.x, PLAYER);
if (!jumping())
refresh();
return TRUE;
}
/*
* eaten:
* Player would get eaten at this place
*/
eaten(pos)
register COORD *pos;
{
register int x, y;
for (y = pos->y - 1; y <= pos->y + 1; y++) {
if (y <= 0 || y >= Y_FIELDSIZE)
continue;
for (x = pos->x - 1; x <= pos->x + 1; x++) {
if (x <= 0 || x >= X_FIELDSIZE)
continue;
if (Field[y][x] == 1)
return TRUE;
}
}
return FALSE;
}
/*
* reset_count:
* Reset the count variables
*/
reset_count()
{
Count = 0;
Running = FALSE;
leaveok(stdscr, FALSE);
refresh();
}
/*
* jumping:
* See if we are jumping, i.e., we should not refresh.
*/
jumping()
{
return (Jump && (Count || Running || Waiting));
}

View File

@@ -3,34 +3,45 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "robots.h"
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#ifndef lint
static char sccsid[] = "@(#)move_robs.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include "robots.h"
# include <signal.h>
/*
* sign:
* Return the sign of the number
*/
static int
sign(n)
int n;
{
if (n < 0)
return -1;
else if (n > 0)
return 1;
else
return 0;
}
/*
* move_robots:
* Move the robots around
*/
void
move_robots(was_sig)
bool was_sig;
int was_sig;
{
register COORD *rp;
register int y, x;
register int mindist, d;
static COORD newpos;
if (Real_time)
signal(SIGALRM, move_robots);
# ifdef DEBUG
#ifdef DEBUG
move(Min.y, Min.x);
addch(inch());
move(Max.y, Max.x);
addch(inch());
# endif DEBUG
#endif
for (rp = Robots; rp < &Robots[MAXROBOTS]; rp++) {
if (rp->y < 0)
continue;
@@ -81,17 +92,17 @@ bool was_sig;
if (was_sig) {
refresh();
if (Dead || Num_robots <= 0)
longjmp(End_move);
longjmp(End_move, 1);
}
# ifdef DEBUG
#ifdef DEBUG
standout();
move(Min.y, Min.x);
addch(inch());
move(Max.y, Max.x);
addch(inch());
standend();
# endif DEBUG
#endif
if (Real_time)
alarm(3);
}
@@ -100,6 +111,7 @@ bool was_sig;
* add_score:
* Add a score to the overall point total
*/
void
add_score(add)
int add;
{
@@ -107,18 +119,3 @@ int add;
move(Y_SCORE, X_SCORE);
printw("%d", Score);
}
/*
* sign:
* Return the sign of the number
*/
sign(n)
int n;
{
if (n < 0)
return -1;
else if (n > 0)
return 1;
else
return 0;
}

View File

@@ -3,21 +3,17 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)play_level.c 5.2 (Berkeley) 9/23/85";
#endif not lint
# include "robots.h"
#include "robots.h"
#include <unistd.h>
/*
* play_level:
* Let the player play the current level
*/
void
play_level()
{
register COORD *cp;
register int y, x, bonus;
move(My_pos.y, My_pos.x);
addch(PLAYER);
@@ -29,14 +25,14 @@ play_level()
addch(ROBOT);
}
refresh();
# ifdef DEBUG
#ifdef DEBUG
standout();
move(Min.y, Min.x);
addch(inch());
move(Max.y, Max.x);
addch(inch());
standend();
# endif DEBUG
#endif
setjmp(End_move);
flush_in();
while (!Dead && Num_robots > 0) {

View File

@@ -3,17 +3,13 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)query.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include "robots.h"
#include "robots.h"
/*
* query:
* Ask a question and get a yes or no answer. Default is "no".
*/
int
query(prompt)
char *prompt;
{

View File

@@ -3,14 +3,17 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "robots.h"
#include <stdlib.h>
#ifndef lint
static char sccsid[] = "@(#)rnd_pos.c 5.1 (Berkeley) 5/30/85";
#endif not lint
#define IS_SAME(p,y,x) ((p).y != -1 && (p).y == y && (p).x == x)
# include "robots.h"
# define IS_SAME(p,y,x) ((p).y != -1 && (p).y == y && (p).x == x)
static int
rnd(range)
int range;
{
return random() % range;
}
/*
* rnd_pos:
@@ -21,7 +24,6 @@ rnd_pos()
{
static COORD pos;
static int call = 0;
register int i = 0;
do {
pos.y = rnd(Y_FIELDSIZE - 1) + 1;
@@ -31,11 +33,3 @@ rnd_pos()
call++;
return &pos;
}
rnd(range)
int range;
{
unsigned int rand();
return rand() % range;
}

View File

@@ -6,44 +6,53 @@
* @(#)robots.h 5.1 (Berkeley) 5/30/85
*/
# include <curses.h>
# include <setjmp.h>
#include <curses.h>
#include <setjmp.h>
#ifndef flushok
# define flushok(win, ok) /* empty */
#endif
/*
* miscellaneous constants
*/
# define Y_FIELDSIZE 23
# define X_FIELDSIZE 60
# define Y_SIZE 24
# define X_SIZE 80
# define MAXLEVELS 4
# define MAXROBOTS (MAXLEVELS * 10)
# define ROB_SCORE 10
# define S_BONUS (60 * ROB_SCORE)
# define Y_SCORE 21
# define X_SCORE (X_FIELDSIZE + 9)
# define Y_PROMPT (Y_FIELDSIZE - 1)
# define X_PROMPT (X_FIELDSIZE + 2)
# define MAXSCORES (Y_SIZE - 2)
# define MAXNAME 16
# define MS_NAME "Ten"
# define SCOREFILE "/usr/games/lib/robots_roll"
#define Y_FIELDSIZE 23
#define X_FIELDSIZE 60
#define Y_SIZE 24
#define X_SIZE 80
#define MAXLEVELS 4
#define MAXROBOTS (MAXLEVELS * 10)
#define ROB_SCORE 10
#define S_BONUS (60 * ROB_SCORE)
#define Y_SCORE 21
#define X_SCORE (X_FIELDSIZE + 9)
#define Y_PROMPT (Y_FIELDSIZE - 1)
#define X_PROMPT (X_FIELDSIZE + 2)
#define MAXSCORES (Y_SIZE - 2)
#define MAXNAME 16
#define MS_NAME "Ten"
#ifdef CROSS
# define SCOREFILE "/usr/local/games/robots_roll"
#else
# define SCOREFILE "/games/lib/robots_roll"
#endif
/*
* characters on screen
*/
# define ROBOT '+'
# define HEAP '*'
# define PLAYER '@'
#define ROBOT '+'
#define HEAP '*'
#define PLAYER '@'
/*
* pseudo functions
*/
# undef CTRL
# define CTRL(X) ('X' - 'A' + 1)
#undef CTRL
#define CTRL(x) (x - 'A' + 1)
/*
* type definitions
@@ -77,7 +86,19 @@ extern jmp_buf End_move;
/*
* functions types
*/
COORD *rnd_pos(void);
int quit(), cmp_sc(), move_robots();
void quit(int sig);
void move_robots(int was_sig);
void show_score(void);
void init_field(void);
void make_level(void);
void play_level(void);
void score(void);
void reset_count(void);
void flush_in(void);
void add_score(int add);
void get_move(void);
COORD *rnd_pos();
int query(char *prompt);
int jumping(void);

View File

@@ -3,13 +3,12 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)score.c 5.1 (Berkeley) 5/30/85";
#endif not lint
# include "robots.h"
# include <pwd.h>
#include "robots.h"
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct {
int s_uid;
@@ -25,11 +24,37 @@ int Max_per_uid = MAX_PER_UID;
static SCORE Top[MAXSCORES];
/*
* cmp_sc:
* Compare two scores.
*/
static int
cmp_sc(a1, a2)
const void *a1, *a2;
{
const SCORE *s1 = a1;
const SCORE *s2 = a2;
return s2->s_score - s1->s_score;
}
static void
set_name(scp)
register SCORE *scp;
{
register PASSWD *pp;
if ((pp = getpwuid(scp->s_uid)) == NULL)
pp->pw_name = "???";
strncpy(scp->s_name, pp->pw_name, MAXNAME);
}
/*
* score:
* Post the player's score, if reasonable, and then print out the
* top list.
*/
void
score()
{
register int inf;
@@ -44,9 +69,12 @@ score()
return;
}
if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid)
read(inf, Top, sizeof Top);
else {
if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid) {
if (read(inf, Top, sizeof Top) != sizeof Top) {
perror("Error reading top scores");
exit(-1);
}
} else {
for (scp = Top; scp < &Top[MAXSCORES]; scp++)
scp->s_score = -1;
max_uid = Max_per_uid;
@@ -101,36 +129,23 @@ score()
if (Newscore) {
lseek(inf, 0L, 0);
write(inf, &max_uid, sizeof max_uid);
write(inf, Top, sizeof Top);
if (write(inf, &max_uid, sizeof max_uid) != sizeof max_uid) {
perror("Error writing max uid");
exit(-1);
}
if (write(inf, Top, sizeof Top) != sizeof Top) {
perror("Error writing top scores");
exit(-1);
}
}
close(inf);
}
set_name(scp)
register SCORE *scp;
{
register PASSWD *pp;
if ((pp = getpwuid(scp->s_uid)) == NULL)
pp->pw_name = "???";
strncpy(scp->s_name, pp->pw_name, MAXNAME);
}
/*
* cmp_sc:
* Compare two scores.
*/
cmp_sc(s1, s2)
register SCORE *s1, *s2;
{
return s2->s_score - s1->s_score;
}
/*
* show_score:
* Show the score list for the '-s' option.
*/
void
show_score()
{
register SCORE *scp;
@@ -145,11 +160,18 @@ show_score()
for (scp = Top; scp < &Top[MAXSCORES]; scp++)
scp->s_score = -1;
read(inf, &max_score, sizeof max_score);
read(inf, Top, sizeof Top);
if (read(inf, &max_score, sizeof max_score) != sizeof max_score) {
perror("Error reading max scores");
exit(-1);
}
if (read(inf, Top, sizeof Top) != sizeof Top) {
perror("Error reading top scores");
exit(-1);
}
close(inf);
inf = 1;
for (scp = Top; scp < &Top[MAXSCORES]; scp++)
if (scp->s_score >= 0)
printf("%d\t%d\t%.*s\n", inf++, scp->s_score, sizeof scp->s_name, scp->s_name);
printf("%d\t%d\t%.*s\n", inf++, scp->s_score,
(int)sizeof scp->s_name, scp->s_name);
}