diff --git a/rootfs.manifest b/rootfs.manifest index 98240dc..9891d74 100644 --- a/rootfs.manifest +++ b/rootfs.manifest @@ -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 diff --git a/src/games/Makefile b/src/games/Makefile index da2207f..e0c5ceb 100644 --- a/src/games/Makefile +++ b/src/games/Makefile @@ -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. diff --git a/src/games/robots/.gitignore b/src/games/robots/.gitignore new file mode 100644 index 0000000..6a28141 --- /dev/null +++ b/src/games/robots/.gitignore @@ -0,0 +1 @@ +robots diff --git a/src/games/robots/Makefile b/src/games/robots/Makefile index db4e681..1773384 100644 --- a/src/games/robots/Makefile +++ b/src/games/robots/Makefile @@ -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 \ - 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 +TOPSRC = $(shell cd ../../..; pwd) +include $(TOPSRC)/target.mk +#include $(TOPSRC)/cross.mk +#CFLAGS = -O -DCROSS -g -DFANCY -robots: ${OBJS} - ${CC} ${SEPFLAG} ${CFLAGS} -o robots ${OBJS} -lcurses -ltermlib +CFLAGS += -DMAX_PER_UID=5 -Werror -Wall -lint: - lint -hb ${DEFS} ${CFILES} -lcurses 2>1 > lint.out +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 +MAN = robots.0 +LIBS = -lcurses -ltermcap -lc -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 +all: robots $(MAN) + +robots: $(OBJS) + $(CC) $(LDFLAGS) -o $@.elf $(OBJS) $(LIBS) + $(OBJDUMP) -S $@.elf > $@.dis + $(SIZE) $@.elf + $(ELF2AOUT) $@.elf $@ && rm $@.elf + +$(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 diff --git a/src/games/robots/extern.c b/src/games/robots/extern.c index 1a4c103..bfb4042 100644 --- a/src/games/robots/extern.c +++ b/src/games/robots/extern.c @@ -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 */ diff --git a/src/games/robots/flush_in.c b/src/games/robots/flush_in.c index 9d4842d..e9b4c2d 100644 --- a/src/games/robots/flush_in.c +++ b/src/games/robots/flush_in.c @@ -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 +#include /* * 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 } diff --git a/src/games/robots/init_field.c b/src/games/robots/init_field.c index a72c717..78cafcf 100644 --- a/src/games/robots/init_field.c +++ b/src/games/robots/init_field.c @@ -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:", diff --git a/src/games/robots/main.c b/src/games/robots/main.c index df15f7a..0b3e8b8 100644 --- a/src/games/robots/main.c +++ b/src/games/robots/main.c @@ -3,21 +3,71 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ +#include "robots.h" +#include +#include +#include +#include +#include -#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 -# include +/* + * 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; } diff --git a/src/games/robots/make_level.c b/src/games/robots/make_level.c index 56204b4..0f5b9cd 100644 --- a/src/games/robots/make_level.c +++ b/src/games/robots/make_level.c @@ -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 /* * 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++) diff --git a/src/games/robots/move.c b/src/games/robots/move.c index 80b6230..1ad7dd7 100644 --- a/src/games/robots/move.c +++ b/src/games/robots/move.c @@ -3,25 +3,138 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ +#include "robots.h" +#include -#ifndef lint -static char sccsid[] = "@(#)move.c 5.1 (Berkeley) 5/30/85"; -#endif not lint +#define ESC '\033' -# include "robots.h" -# include +/* + * 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)); -} diff --git a/src/games/robots/move_robs.c b/src/games/robots/move_robs.c index 3fd782c..2c7a4f1 100644 --- a/src/games/robots/move_robs.c +++ b/src/games/robots/move_robs.c @@ -3,34 +3,45 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ +#include "robots.h" +#include +#include +#include -#ifndef lint -static char sccsid[] = "@(#)move_robs.c 5.1 (Berkeley) 5/30/85"; -#endif not lint - -# include "robots.h" -# include +/* + * 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; -} diff --git a/src/games/robots/play_level.c b/src/games/robots/play_level.c index 32be7c1..f016c4d 100644 --- a/src/games/robots/play_level.c +++ b/src/games/robots/play_level.c @@ -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 /* * 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) { diff --git a/src/games/robots/query.c b/src/games/robots/query.c index 2624829..973a247 100644 --- a/src/games/robots/query.c +++ b/src/games/robots/query.c @@ -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; { diff --git a/src/games/robots/rnd_pos.c b/src/games/robots/rnd_pos.c index 9706e0d..0c13e59 100644 --- a/src/games/robots/rnd_pos.c +++ b/src/games/robots/rnd_pos.c @@ -3,14 +3,17 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ +#include "robots.h" +#include -#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; -} diff --git a/src/games/robots/robots.h b/src/games/robots/robots.h index de93fd4..94bab24 100644 --- a/src/games/robots/robots.h +++ b/src/games/robots/robots.h @@ -6,44 +6,53 @@ * @(#)robots.h 5.1 (Berkeley) 5/30/85 */ -# include -# include +#include +#include + +#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); diff --git a/src/games/robots/score.c b/src/games/robots/score.c index 31e0ed7..172a3ec 100644 --- a/src/games/robots/score.c +++ b/src/games/robots/score.c @@ -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 +#include "robots.h" +#include +#include +#include +#include +#include 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); }