/* * score.c -- xrobots * * * Copyright 1989 Brian Warkentine * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the author's name not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. The author makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * The author's current employer has no connection with this software, as it * was written before being employed at Sun. The following address is for * contacting the author only and does not imply any responsibility on the * behalf of Sun Microsystems. * * Contact the author at: * Brian Warkentine (brianw@Sun.COM) * Sun Microsystems * 2550 Garcia Avenue * Mountain View, Ca 94043-1100 * *---------------------------------------------------------------------- * flock() is used to stop a race condition within... if you don't * have a bsd-like flock(), you could probably comment it out. * If SYSV is defined, this will be #ifdef'd out. * The race condition (multiple users writing to the score file) is * probably rare. */ #include #include #include #ifdef R3 #include #include #include #else #include #include #include #endif #include /* brings in */ #include #include #include "xrobots.h" /*----------------------------------------------------------------------*/ typedef struct { char score[6], name[26]; } SCORE; static SCORE scores[MAXSCORES]; static void show_scores(), write_out_scores(); static void new_high_score(), load_scores(); static FILE *scorefile = 0; char *score_filename; /*----------------------------------------------------------------------*/ void check_score(current_score) int current_score; { load_scores(); if(current_score > atoi(scores[MAXSCORES-1].score)) { new_high_score(current_score); write_out_scores(); } if(scorefile) { #ifndef SYSV flock(scorefile->_file, LOCK_UN); #endif fclose(scorefile); show_scores(); } } static void load_scores() { int i = 0; if( !(scorefile = fopen(score_filename,"r+")) ) { scorefile = fopen(score_filename, "w"); return; } #ifndef SYSV flock(scorefile->_file, LOCK_EX); #endif for(i = 0; i < MAXSCORES; i++) { if(!fgets(scores[i].score, 6, scorefile)) /* get score */ break; if(!fgets(scores[i].name, 26, scorefile)) /* get name */ break; if(!fgetc(scorefile)) /* and newline */ break; } } static void new_high_score(current_score) int current_score; { int i; char textscore[6], name[26]; sprintf(textscore,"%5d",current_score); strncpy(name,(const char *)getenv("USER"),25); for(i=MAXSCORES-2;i>=0;i--) if( current_score < atoi(scores[i].score) ) { /* move new score into i+1 slot */ strcpy(scores[i+1].score,textscore); strcpy(scores[i+1].name,name); return; } else { strcpy(scores[i+1].score,scores[i].score); strcpy(scores[i+1].name,scores[i].name); } /* if it got here, there is a new number 1 score */ strcpy(scores[0].score,textscore); strcpy(scores[0].name,name); } void write_out_scores() { int i; if( !scorefile ) return; rewind(scorefile); for(i=0;i_file, LOCK_UN); #endif fclose(scorefile); show_scores(); } }