1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*****************************************
xjump version 2
record.c Ͽ
(C) July 16, 1997 ROYALPANDA
*****************************************/
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/file.h>
#include<errno.h>
#include<ctype.h>
#include<pwd.h>
#include"record.h"
/* ڡɤФ */
static int skipspace( FILE *fp )
{
int c;
for(;;){
c = fgetc(fp);
if( c == EOF )
return -1;
if( c == '\n' )
return -1;
if( !isspace(c) ){
ungetc(c,fp);
return 0;
}
}
}
/* ɤ߹ */
static int read_num( FILE *fp )
{
int c,i;
int err;
if( skipspace( fp ) )
return -1;
i = 0;
err = 1;
for(;;){
c = fgetc( fp );
if( isdigit( c ) ){
i = i * 10 + c - '0';
err = 0;
}else
break;
}
ungetc( c,fp );
if( err )
return -1;
else
return i;
}
/* ̾ɤ߹ */
static void read_name( char *buf, FILE *fp )
{
int l,c;
if( skipspace( fp ) ){
*buf = '\0';
return;
}
for( l = 0 ; l < 31 ; l++ ){
c = fgetc( fp );
if( c < 0 || iscntrl( c ) ){
ungetc( c,fp );
break;
}
*buf++ = c;
}
*buf = '\0';
}
/* ޤɤФ */
static void void_line( FILE *fp )
{
int c;
while( ( c = fgetc(fp) ) != '\n' && c != EOF )
;
}
/* UIDꥢ͡Ф */
static char *get_name( int uid, char *name )
{
struct passwd *pw;
int i;
if( ( pw = getpwuid( uid ) ) == NULL )
name[0] = '\0';
else{
i = 0;
while( i < 31 && pw->pw_gecos[i] != '\0' && pw->pw_gecos[i] != ',' ){
name[i] = pw->pw_gecos[i];
i++;
}
if( i > 0 )
name[i] = '\0';
else
strcpy( name,pw->pw_name );
}
return name;
}
/*쥳ɤΣȥɤ߹ߡ*/
static int read_entry( FILE *fp, record_t *rec )
{
if( ( rec->score = read_num( fp ) ) < 0 )
return -1;
if( ( rec->uid = read_num( fp ) ) < 0 )
return -1;
read_name( rec->name,fp );
if( rec->name[0] == '\0' )
get_name( rec->uid,rec->name );
return 0;
}
/* 쥳ɤߤ */
static void read_record( FILE *fp )
{
int i;
i = 0;
while( !feof( fp ) && i < RECORD_ENTRY ){
if( read_entry( fp,&Record[i] ) == 0 )
i++;
void_line( fp );
}
Record_entry = i;
}
/* 顼 */
static void error( void )
{
perror( Myname );
fprintf( stderr,"%s: do not record high-score.\n",Myname);
Record_entry = -1;
}
/* 쥳ɴطν */
void init_record( void )
{
FILE *fp;
Record_entry = -1;
if( ( fp = fopen( RECORD_FILE,"r+" ) ) == NULL ){
error();
return;
}
flock( fileno(fp),LOCK_EX );
read_record( fp );
fclose( fp );
}
/* Ӵؿ */
static int sort_cmp( record_t *r1, record_t *r2 )
{
if( r1->score > r2->score )
return -1;
else if( r1->score < r2->score )
return 1;
else if( r1->rank > r2->rank )
return 1;
else
return -1;
}
/* */
static void sort_record( void )
{
int i;
for( i = 0 ; i < Record_entry ; i++ )
Record[i].rank = i;
qsort( Record,Record_entry,sizeof(record_t),sort_cmp );
}
/* 쥳ɵϿ */
void save_record( int sc )
{
FILE *fp;
int i,r;
int uid;
if( Record_entry == -1 )
return ;
if( (fp = fopen(RECORD_FILE,"r+")) == NULL ){
error();
return;
}
flock( fileno(fp),LOCK_EX );
read_record( fp );
r = -1;
uid = (int)getuid();
for( i = 0 ; i < Record_entry ; i++ )
if( Record[i].uid == uid ){
r = i;
break;
}
if( r == -1 ){
if( Record_entry < RECORD_ENTRY ){
r = Record_entry++;
Record[r].score = 0;
}else
r = RECORD_ENTRY - 1;
}
if( sc > Record[r].score ){
Record[r].score = sc;
Record[r].uid = uid;
get_name( uid,Record[r].name );
sort_record();
rewind( fp );
ftruncate( fileno(fp) ,0 );
for( i = 0 ; i < Record_entry ; i++ )
fprintf( fp,"%d %d %s\n",
Record[i].score,Record[i].uid,Record[i].name );
fclose( fp );
return;
}
fclose( fp );
}
|