1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)cfscores.c	5.4 (Berkeley) 03/05/89";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <pwd.h>
30 
31 struct betinfo {
32 	long	hand;		/* cost of dealing hand */
33 	long	inspection;	/* cost of inspecting hand */
34 	long	game;		/* cost of buying game */
35 	long	runs;		/* cost of running through hands */
36 	long	information;	/* cost of information */
37 	long	thinktime;	/* cost of thinking time */
38 	long	wins;		/* total winnings */
39 	long	worth;		/* net worth after costs */
40 };
41 
42 char *scorefile = "/usr/games/lib/cfscores";
43 int dbfd;
44 
45 main(argc, argv)
46 	int argc;
47 	char *argv[];
48 {
49 	register struct passwd *pw;
50 	int uid;
51 
52 	if (argc > 2) {
53 		printf("Usage: cfscores [user]\n");
54 		exit(1);
55 	}
56 	dbfd = open(scorefile, 0);
57 	if (dbfd < 0) {
58 		perror(scorefile);
59 		exit(2);
60 	}
61 	setpwent();
62 	if (argc == 1) {
63 		uid = getuid();
64 		pw = getpwuid(uid);
65 		if (pw == 0) {
66 			printf("You are not listed in the password file?!?\n");
67 			exit(2);
68 		}
69 		printuser(pw, 1);
70 		exit(0);
71 	}
72 	if (strcmp(argv[1], "-a") == 0) {
73 		while ((pw = getpwent()) != 0)
74 			printuser(pw, 0);
75 		exit(0);
76 	}
77 	pw = getpwnam(argv[1]);
78 	if (pw == 0) {
79 		printf("User %s unknown\n", argv[1]);
80 		exit(3);
81 	}
82 	printuser(pw, 1);
83 	exit(0);
84 }
85 
86 /*
87  * print out info for specified password entry
88  */
89 printuser(pw, printfail)
90 	register struct passwd *pw;
91 	int printfail;
92 {
93 	struct betinfo total;
94 	int i;
95 
96 	if (pw->pw_uid < 0) {
97 		printf("Bad uid %d\n", pw->pw_uid);
98 		return;
99 	}
100 	i = lseek(dbfd, pw->pw_uid * sizeof(struct betinfo), 0);
101 	if (i < 0) {
102 		perror("lseek");
103 		return;
104 	}
105 	i = read(dbfd, (char *)&total, sizeof(total));
106 	if (i < 0) {
107 		perror("read");
108 		return;
109 	}
110 	if (i == 0 || total.hand == 0) {
111 		if (printfail)
112 			printf("%s has never played canfield.\n", pw->pw_name);
113 		return;
114 	}
115 	printf("*----------------------*\n");
116 	if (total.worth >= 0)
117 		printf("* Winnings for %-8s*\n", pw->pw_name);
118 	else
119 		printf("* Losses for %-10s*\n", pw->pw_name);
120 	printf("*======================*\n");
121 	printf("|Costs           Total |\n");
122 	printf("| Hands       %8d |\n", total.hand);
123 	printf("| Inspections %8d |\n", total.inspection);
124 	printf("| Games       %8d |\n", total.game);
125 	printf("| Runs        %8d |\n", total.runs);
126 	printf("| Information %8d |\n", total.information);
127 	printf("| Think time  %8d |\n", total.thinktime);
128 	printf("|Total Costs  %8d |\n", total.wins - total.worth);
129 	printf("|Winnings     %8d |\n", total.wins);
130 	printf("|Net Worth    %8d |\n", total.worth);
131 	printf("*----------------------*\n\n");
132 }
133