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