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