xref: /netbsd-src/games/sail/lo_main.c (revision 388218390b6006bcf1a684c7a0af2dd8c62aef61)
1*38821839Sdholland /*	$NetBSD: lo_main.c,v 1.19 2010/08/06 09:14:40 dholland Exp $	*/
2982615d7Scgd 
361f28255Scgd /*
4982615d7Scgd  * Copyright (c) 1983, 1993
5982615d7Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32bf72b656Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34982615d7Scgd #if 0
35156daa7aStls static char sccsid[] = "@(#)lo_main.c	8.2 (Berkeley) 4/28/95";
36982615d7Scgd #else
37*38821839Sdholland __RCSID("$NetBSD: lo_main.c,v 1.19 2010/08/06 09:14:40 dholland Exp $");
38982615d7Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd /*
4261f28255Scgd  * Print out the top ten SAILors
4361f28255Scgd  *
4461f28255Scgd  * -l force a long listing (print out real usernames)
4561f28255Scgd  */
46aac077fcSjwise 
4732aeef11Sdholland #include <err.h>
48*38821839Sdholland #include <errno.h>
49aac077fcSjwise #include <stdio.h>
50bcc8ec99Scgd #include <stdlib.h>
51*38821839Sdholland #include <string.h>
5261f28255Scgd #include <pwd.h>
53*38821839Sdholland #include <curses.h>
54*38821839Sdholland 
55156daa7aStls #include "extern.h"
5661f28255Scgd #include "pathnames.h"
5761f28255Scgd 
58*38821839Sdholland 
59da121b33Sdholland static const char *const title[] = {
6061f28255Scgd 	"Admiral", "Commodore", "Captain", "Captain",
6161f28255Scgd 	"Captain", "Captain", "Captain", "Commander",
6261f28255Scgd 	"Commander", "Lieutenant"
6361f28255Scgd };
6461f28255Scgd 
65*38821839Sdholland void
lo_curses(void)66*38821839Sdholland lo_curses(void)
67*38821839Sdholland {
68*38821839Sdholland 	FILE *fp;
69*38821839Sdholland 	char sbuf[32];
70*38821839Sdholland 	int n = 0, npeople;
71*38821839Sdholland 	int line;
72*38821839Sdholland 	struct passwd *pass;
73*38821839Sdholland 	struct logs log;
74*38821839Sdholland 	struct ship *ship;
75*38821839Sdholland 
76*38821839Sdholland 	erase();
77*38821839Sdholland 
78*38821839Sdholland 	fp = fopen(_PATH_LOGFILE, "r");
79*38821839Sdholland 	if (fp == NULL) {
80*38821839Sdholland 		mvprintw(5, 10, "%s: %s", _PATH_LOGFILE, strerror(errno));
81*38821839Sdholland 		mvaddstr(7, 10, "Press any key...");
82*38821839Sdholland 		getch();
83*38821839Sdholland 		return;
84*38821839Sdholland 	}
85*38821839Sdholland 	switch (fread(&npeople, sizeof npeople, 1, fp)) {
86*38821839Sdholland 	    case 0:
87*38821839Sdholland 		mvprintw(5, 10, "Nobody has sailed yet.");
88*38821839Sdholland 		mvaddstr(7, 10, "Press any key...");
89*38821839Sdholland 		getch();
90*38821839Sdholland 		return;
91*38821839Sdholland 	    case 1:
92*38821839Sdholland 		break;
93*38821839Sdholland 	    default:
94*38821839Sdholland 		mvprintw(5, 10, "%s: %s", _PATH_LOGFILE, strerror(errno));
95*38821839Sdholland 		mvaddstr(7, 10, "Press any key...");
96*38821839Sdholland 		getch();
97*38821839Sdholland 		return;
98*38821839Sdholland 	}
99*38821839Sdholland 	line = 0;
100*38821839Sdholland 	while (fread(&log, sizeof log, 1, fp) == 1 &&
101*38821839Sdholland 	       log.l_name[0] != '\0' &&
102*38821839Sdholland 		line < LINES - 2) {
103*38821839Sdholland 		if (longfmt && (pass = getpwuid(log.l_uid)) != NULL)
104*38821839Sdholland 			snprintf(sbuf, sizeof(sbuf),
105*38821839Sdholland 				"%10.10s (%s)", log.l_name, pass->pw_name);
106*38821839Sdholland 		else
107*38821839Sdholland 			snprintf(sbuf, sizeof(sbuf),
108*38821839Sdholland 				"%20.20s", log.l_name);
109*38821839Sdholland 		ship = &scene[log.l_gamenum].ship[log.l_shipnum];
110*38821839Sdholland 		mvprintw(line, 0,
111*38821839Sdholland 			"%-10s %21s of the %15s %3d points, %5.2f equiv",
112*38821839Sdholland 			title[n++], sbuf, ship->shipname, log.l_netpoints,
113*38821839Sdholland 			(float) log.l_netpoints / ship->specs->pts);
114*38821839Sdholland 		line++;
115*38821839Sdholland 	}
116*38821839Sdholland 	fclose(fp);
117*38821839Sdholland 	mvprintw(line+1, 0, "%d people have played. Press any key.", npeople);
118*38821839Sdholland 	getch();
119*38821839Sdholland }
120*38821839Sdholland 
121bf72b656Schristos int
lo_main(void)122dfea9f08Sjwise lo_main(void)
12361f28255Scgd {
12461f28255Scgd 	FILE *fp;
12561f28255Scgd 	char sbuf[32];
126a627b039Sdholland 	int n = 0, npeople;
12761f28255Scgd 	struct passwd *pass;
12861f28255Scgd 	struct logs log;
12961f28255Scgd 	struct ship *ship;
13061f28255Scgd 
13161f28255Scgd 	if ((fp = fopen(_PATH_LOGFILE, "r")) == 0) {
13232aeef11Sdholland 		err(1, "%s", _PATH_LOGFILE);
13361f28255Scgd 	}
1349a834809Sdholland 	switch (fread(&npeople, sizeof npeople, 1, fp)) {
13561f28255Scgd 	case 0:
13661f28255Scgd 		printf("Nobody has sailed yet.\n");
13761f28255Scgd 		exit(0);
13861f28255Scgd 	case 1:
13961f28255Scgd 		break;
14061f28255Scgd 	default:
14132aeef11Sdholland 		err(1, "%s", _PATH_LOGFILE);
14261f28255Scgd 	}
1439a834809Sdholland 	while (fread(&log, sizeof log, 1, fp) == 1 &&
14461f28255Scgd 	       log.l_name[0] != '\0') {
14561f28255Scgd 		if (longfmt && (pass = getpwuid(log.l_uid)) != NULL)
14625e99263Sdholland 			snprintf(sbuf, sizeof(sbuf),
14725e99263Sdholland 				"%10.10s (%s)", log.l_name, pass->pw_name);
14861f28255Scgd 		else
14925e99263Sdholland 			snprintf(sbuf, sizeof(sbuf),
15025e99263Sdholland 				"%20.20s", log.l_name);
15161f28255Scgd 		ship = &scene[log.l_gamenum].ship[log.l_shipnum];
15261f28255Scgd 		printf("%-10s %21s of the %15s %3d points, %5.2f equiv\n",
15361f28255Scgd 			title[n++], sbuf, ship->shipname, log.l_netpoints,
15461f28255Scgd 			(float) log.l_netpoints / ship->specs->pts);
15561f28255Scgd 	}
1564c5d0b18Swiz 	fclose(fp);
157a627b039Sdholland 	printf("\n%d people have played.\n", npeople);
15861f28255Scgd 	return 0;
15961f28255Scgd }
160