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