121473Smckusick /* 221473Smckusick * Copyright (c) 1980 Regents of the University of California. 333690Sbostic * All rights reserved. 433690Sbostic * 533690Sbostic * Redistribution and use in source and binary forms are permitted 634797Sbostic * provided that the above copyright notice and this paragraph are 734797Sbostic * duplicated in all such forms and that any documentation, 834797Sbostic * advertising materials, and other materials related to such 934797Sbostic * distribution and use acknowledge that the software was developed 1034797Sbostic * by the University of California, Berkeley. The name of the 1134797Sbostic * University may not be used to endorse or promote products derived 1234797Sbostic * from this software without specific prior written permission. 1334797Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434797Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534797Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621473Smckusick */ 1721473Smckusick 1821473Smckusick #ifndef lint 19*37017Sbostic static char sccsid[] = "@(#)score.c 5.4 (Berkeley) 03/05/89"; 2033690Sbostic #endif /* not lint */ 2121473Smckusick 2221473Smckusick # include "robots.h" 23*37017Sbostic # include <sys/types.h> 2421473Smckusick # include <pwd.h> 2521473Smckusick 2621473Smckusick typedef struct { 2721473Smckusick int s_uid; 2821473Smckusick int s_score; 2921473Smckusick char s_name[MAXNAME]; 3021473Smckusick } SCORE; 3121473Smckusick 3221473Smckusick typedef struct passwd PASSWD; 3321473Smckusick 3421473Smckusick char *Scorefile = SCOREFILE; 3521473Smckusick 3621473Smckusick int Max_per_uid = MAX_PER_UID; 3721473Smckusick 3821473Smckusick static SCORE Top[MAXSCORES]; 3921473Smckusick 4021473Smckusick /* 4121473Smckusick * score: 4221473Smckusick * Post the player's score, if reasonable, and then print out the 4321473Smckusick * top list. 4421473Smckusick */ 4521473Smckusick score() 4621473Smckusick { 4721473Smckusick register int inf; 4821473Smckusick register SCORE *scp; 4921473Smckusick register int uid; 5021473Smckusick register bool done_show = FALSE; 5121473Smckusick static int numscores, max_uid; 5221473Smckusick 5321473Smckusick Newscore = FALSE; 5421473Smckusick if ((inf = open(Scorefile, 2)) < 0) { 5521473Smckusick perror(Scorefile); 5621473Smckusick return; 5721473Smckusick } 5821473Smckusick 5921473Smckusick if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid) 6021473Smckusick read(inf, Top, sizeof Top); 6121473Smckusick else { 6221473Smckusick for (scp = Top; scp < &Top[MAXSCORES]; scp++) 6321473Smckusick scp->s_score = -1; 6421473Smckusick max_uid = Max_per_uid; 6521473Smckusick } 6621473Smckusick 6721473Smckusick uid = getuid(); 6821473Smckusick if (Top[MAXSCORES-1].s_score <= Score) { 6921473Smckusick numscores = 0; 7021473Smckusick for (scp = Top; scp < &Top[MAXSCORES]; scp++) 7121473Smckusick if (scp->s_score < 0 || 7221473Smckusick (scp->s_uid == uid && ++numscores == max_uid)) { 7321473Smckusick if (scp->s_score > Score) 7421473Smckusick break; 7521473Smckusick scp->s_score = Score; 7621473Smckusick scp->s_uid = uid; 7721473Smckusick set_name(scp); 7821473Smckusick Newscore = TRUE; 7921473Smckusick break; 8021473Smckusick } 8121473Smckusick if (scp == &Top[MAXSCORES]) { 8221473Smckusick Top[MAXSCORES-1].s_score = Score; 8321473Smckusick Top[MAXSCORES-1].s_uid = uid; 8421473Smckusick set_name(&Top[MAXSCORES-1]); 8521473Smckusick Newscore = TRUE; 8621473Smckusick } 8721473Smckusick if (Newscore) 8821473Smckusick qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc); 8921473Smckusick } 9021473Smckusick 9121473Smckusick if (!Newscore) { 9221473Smckusick Full_clear = FALSE; 9321473Smckusick close(inf); 9421473Smckusick return; 9521473Smckusick } 9621473Smckusick else 9721473Smckusick Full_clear = TRUE; 9821473Smckusick 9921473Smckusick for (scp = Top; scp < &Top[MAXSCORES]; scp++) { 10021473Smckusick if (scp->s_score < 0) 10121473Smckusick break; 10221473Smckusick move((scp - Top) + 1, 15); 10321473Smckusick if (!done_show && scp->s_uid == uid && scp->s_score == Score) 10421473Smckusick standout(); 10521473Smckusick printw(" %d\t%d\t%-8.8s ", (scp - Top) + 1, scp->s_score, scp->s_name); 10621473Smckusick if (!done_show && scp->s_uid == uid && scp->s_score == Score) { 10721473Smckusick standend(); 10821473Smckusick done_show = TRUE; 10921473Smckusick } 11021473Smckusick } 11121473Smckusick Num_scores = scp - Top; 11221473Smckusick refresh(); 11321473Smckusick 11421473Smckusick if (Newscore) { 11521473Smckusick lseek(inf, 0L, 0); 11621473Smckusick write(inf, &max_uid, sizeof max_uid); 11721473Smckusick write(inf, Top, sizeof Top); 11821473Smckusick } 11921473Smckusick close(inf); 12021473Smckusick } 12121473Smckusick 12221473Smckusick set_name(scp) 12321473Smckusick register SCORE *scp; 12421473Smckusick { 12521473Smckusick register PASSWD *pp; 12621473Smckusick 12721473Smckusick if ((pp = getpwuid(scp->s_uid)) == NULL) 12821473Smckusick pp->pw_name = "???"; 12921473Smckusick strncpy(scp->s_name, pp->pw_name, MAXNAME); 13021473Smckusick } 13121473Smckusick 13221473Smckusick /* 13321473Smckusick * cmp_sc: 13421473Smckusick * Compare two scores. 13521473Smckusick */ 13621473Smckusick cmp_sc(s1, s2) 13721473Smckusick register SCORE *s1, *s2; 13821473Smckusick { 13921473Smckusick return s2->s_score - s1->s_score; 14021473Smckusick } 14121473Smckusick 14221473Smckusick /* 14321473Smckusick * show_score: 14421473Smckusick * Show the score list for the '-s' option. 14521473Smckusick */ 14621473Smckusick show_score() 14721473Smckusick { 14821473Smckusick register SCORE *scp; 14921473Smckusick register int inf; 15021473Smckusick static int max_score; 15121473Smckusick 15221473Smckusick if ((inf = open(Scorefile, 0)) < 0) { 15321473Smckusick perror(Scorefile); 15421473Smckusick return; 15521473Smckusick } 15621473Smckusick 15721473Smckusick for (scp = Top; scp < &Top[MAXSCORES]; scp++) 15821473Smckusick scp->s_score = -1; 15921473Smckusick 16021473Smckusick read(inf, &max_score, sizeof max_score); 16121473Smckusick read(inf, Top, sizeof Top); 16221473Smckusick close(inf); 16321473Smckusick inf = 1; 16421473Smckusick for (scp = Top; scp < &Top[MAXSCORES]; scp++) 16521473Smckusick if (scp->s_score >= 0) 16621473Smckusick printf("%d\t%d\t%.*s\n", inf++, scp->s_score, sizeof scp->s_name, scp->s_name); 16721473Smckusick } 168