1 /* Copyright (c) 1983 Regents of the University of California */
2 
3 #ifndef lint
4 static char sccsid[] = "@(#)cfscores.c	4.2	(Berkeley)	09/10/84";
5 #endif not lint
6 
7 #include <pwd.h>
8 
9 struct betinfo {
10 	long	hand;		/* cost of dealing hand */
11 	long	inspection;	/* cost of inspecting hand */
12 	long	game;		/* cost of buying game */
13 	long	runs;		/* cost of running through hands */
14 	long	information;	/* cost of information */
15 	long	thinktime;	/* cost of thinking time */
16 	long	wins;		/* total winnings */
17 	long	worth;		/* net worth after costs */
18 };
19 
20 char *scorefile = "/usr/games/lib/cfscores";
21 int dbfd;
22 
23 main(argc, argv)
24 	int argc;
25 	char *argv[];
26 {
27 	register struct passwd *pw;
28 	int uid;
29 
30 	if (argc > 2) {
31 		printf("Usage: cfscores [user]\n");
32 		exit(1);
33 	}
34 	dbfd = open(scorefile, 0);
35 	if (dbfd < 0) {
36 		perror(scorefile);
37 		exit(2);
38 	}
39 	setpwent();
40 	if (argc == 1) {
41 		uid = getuid();
42 		pw = getpwuid(uid);
43 		if (pw == 0) {
44 			printf("You are not listed in the password file?!?\n");
45 			exit(2);
46 		}
47 		printuser(pw, 1);
48 		exit(0);
49 	}
50 	if (strcmp(argv[1], "-a") == 0) {
51 		while ((pw = getpwent()) != 0)
52 			printuser(pw, 0);
53 		exit(0);
54 	}
55 	pw = getpwnam(argv[1]);
56 	if (pw == 0) {
57 		printf("User %s unknown\n", argv[1]);
58 		exit(3);
59 	}
60 	printuser(pw, 1);
61 	exit(0);
62 }
63 
64 /*
65  * print out info for specified password entry
66  */
67 printuser(pw, printfail)
68 	register struct passwd *pw;
69 	int printfail;
70 {
71 	struct betinfo total;
72 	int i;
73 
74 	if (pw->pw_uid < 0) {
75 		printf("Bad uid %d\n", pw->pw_uid);
76 		return;
77 	}
78 	i = lseek(dbfd, pw->pw_uid * sizeof(struct betinfo), 0);
79 	if (i < 0) {
80 		perror("lseek");
81 		return;
82 	}
83 	i = read(dbfd, (char *)&total, sizeof(total));
84 	if (i < 0) {
85 		perror("read");
86 		return;
87 	}
88 	if (i == 0 || total.hand == 0) {
89 		if (printfail)
90 			printf("%s has never played canfield.\n", pw->pw_name);
91 		return;
92 	}
93 	printf("*----------------------*\n");
94 	if (total.worth >= 0)
95 		printf("* Winnings for %-8s*\n", pw->pw_name);
96 	else
97 		printf("* Losses for %-10s*\n", pw->pw_name);
98 	printf("*======================*\n");
99 	printf("|Costs           Total |\n");
100 	printf("| Hands       %8d |\n", total.hand);
101 	printf("| Inspections %8d |\n", total.inspection);
102 	printf("| Games       %8d |\n", total.game);
103 	printf("| Runs        %8d |\n", total.runs);
104 	printf("| Information %8d |\n", total.information);
105 	printf("| Think time  %8d |\n", total.thinktime);
106 	printf("|Total Costs  %8d |\n", total.wins - total.worth);
107 	printf("|Winnings     %8d |\n", total.wins);
108 	printf("|Net Worth    %8d |\n", total.worth);
109 	printf("*----------------------*\n\n");
110 }
111