|
|
1.1 ! root 1: #ifndef lint ! 2: static char sccsid[] = "@(#)snscore.c 4.1 (Berkeley) 7/4/83"; ! 3: #endif ! 4: ! 5: #include <stdio.h> ! 6: #include <pwd.h> ! 7: char *recfile = "/usr/games/lib/snakerawscores"; ! 8: #define MAXPLAYERS 256 ! 9: ! 10: struct passwd *getpwuid(); ! 11: char *malloc(); ! 12: ! 13: struct player { ! 14: short uids; ! 15: short scores; ! 16: char *name; ! 17: } players[MAXPLAYERS], temp; ! 18: ! 19: main() ! 20: { ! 21: char buf[80], cp; ! 22: short uid, score; ! 23: FILE *fd; ! 24: int noplayers; ! 25: int i, j, notsorted; ! 26: short whoallbest, allbest; ! 27: char *q; ! 28: struct passwd *p; ! 29: ! 30: fd = fopen(recfile, "r"); ! 31: if (fd == NULL) { ! 32: perror(recfile); ! 33: exit(1); ! 34: } ! 35: printf("Snake players scores to date\n"); ! 36: fread(&whoallbest, sizeof(short), 1, fd); ! 37: fread(&allbest, sizeof(short), 1, fd); ! 38: for (uid=2;;uid++) { ! 39: if(fread(&score, sizeof(short), 1, fd) == 0) ! 40: break; ! 41: if (score > 0) { ! 42: if (noplayers > MAXPLAYERS) { ! 43: printf("too many players\n"); ! 44: exit(2); ! 45: } ! 46: players[noplayers].uids = uid; ! 47: players[noplayers].scores = score; ! 48: /* This is faster if passwd is sorted by uid. */ ! 49: p = getpwuid(uid); ! 50: if (p == NULL) ! 51: continue; ! 52: q = p -> pw_name; ! 53: players[noplayers].name = malloc(strlen(q)+1); ! 54: strcpy(players[noplayers].name, q); ! 55: noplayers++; ! 56: } ! 57: } ! 58: ! 59: /* bubble sort scores */ ! 60: for (notsorted=1; notsorted; ) { ! 61: notsorted = 0; ! 62: for (i=0; i<noplayers-1; i++) ! 63: if (players[i].scores < players[i+1].scores) { ! 64: temp = players[i]; ! 65: players[i] = players[i+1]; ! 66: players[i+1] = temp; ! 67: notsorted++; ! 68: } ! 69: } ! 70: ! 71: j = 1; ! 72: for (i=0; i<noplayers; i++) { ! 73: printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name); ! 74: if (players[i].scores > players[i+1].scores) ! 75: j = i+2; ! 76: } ! 77: exit(0); ! 78: } ! 79: ! 80: struct passwd * ! 81: getpwuid(uid) ! 82: register uid; ! 83: { ! 84: register struct passwd *p; ! 85: struct passwd *getpwent(); ! 86: ! 87: while( (p = getpwent()) && p->pw_uid != uid ); ! 88: if (p->pw_uid == uid) ! 89: return(p); ! 90: setpwent(); ! 91: while( (p = getpwent()) && p->pw_uid != uid ); ! 92: endpwent(); ! 93: return(p); ! 94: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.