date and time created 83/07/04 12:48:31 by sam
authorSam Leffler <sam@ucbvax.Berkeley.EDU>
Tue, 5 Jul 1983 03:48:31 +0000 (19:48 -0800)
committerSam Leffler <sam@ucbvax.Berkeley.EDU>
Tue, 5 Jul 1983 03:48:31 +0000 (19:48 -0800)
SCCS-vsn: games/snake/snscore/snscore.c 4.1

usr/src/games/snake/snscore/snscore.c [new file with mode: 0644]

diff --git a/usr/src/games/snake/snscore/snscore.c b/usr/src/games/snake/snscore/snscore.c
new file mode 100644 (file)
index 0000000..3907acf
--- /dev/null
@@ -0,0 +1,94 @@
+#ifndef lint
+static char sccsid[] = "@(#)snscore.c  4.1 (Berkeley) %G%";
+#endif
+
+#include <stdio.h>
+#include <pwd.h>
+char *recfile = "/usr/games/lib/snakerawscores";
+#define MAXPLAYERS 256
+
+struct passwd  *getpwuid();
+char   *malloc();
+
+struct player  {
+       short   uids;
+       short   scores;
+       char    *name;
+} players[MAXPLAYERS], temp;
+
+main()
+{
+       char    buf[80], cp;
+       short   uid, score;
+       FILE    *fd;
+       int     noplayers;
+       int     i, j, notsorted;
+       short   whoallbest, allbest;
+       char    *q;
+       struct  passwd  *p;
+
+       fd = fopen(recfile, "r");
+       if (fd == NULL) {
+               perror(recfile);
+               exit(1);
+       }
+       printf("Snake players scores to date\n");
+       fread(&whoallbest, sizeof(short), 1, fd);
+       fread(&allbest, sizeof(short), 1, fd);
+       for (uid=2;;uid++) {
+               if(fread(&score, sizeof(short), 1, fd) == 0)
+                       break;
+               if (score > 0) {
+                       if (noplayers > MAXPLAYERS) {
+                               printf("too many players\n");
+                               exit(2);
+                       }
+                       players[noplayers].uids = uid;
+                       players[noplayers].scores = score;
+                       /* This is faster if passwd is sorted by uid. */
+                       p = getpwuid(uid);
+                       if (p == NULL)
+                               continue;
+                       q = p -> pw_name;
+                       players[noplayers].name = malloc(strlen(q)+1);
+                       strcpy(players[noplayers].name, q);
+                       noplayers++;
+               }
+       }
+
+       /* bubble sort scores */
+       for (notsorted=1; notsorted; ) {
+               notsorted = 0;
+               for (i=0; i<noplayers-1; i++)
+                       if (players[i].scores < players[i+1].scores) {
+                               temp = players[i];
+                               players[i] = players[i+1];
+                               players[i+1] = temp;
+                               notsorted++;
+                       }
+       }
+
+       j = 1;
+       for (i=0; i<noplayers; i++) {
+               printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
+               if (players[i].scores > players[i+1].scores)
+                       j = i+2;
+       }
+       exit(0);
+}
+
+struct passwd *
+getpwuid(uid)
+register uid;
+{
+       register struct passwd *p;
+       struct passwd *getpwent();
+
+       while( (p = getpwent()) && p->pw_uid != uid );
+       if (p->pw_uid == uid)
+               return(p);
+       setpwent();
+       while( (p = getpwent()) && p->pw_uid != uid );
+       endpwent();
+       return(p);
+}