121197Sdist /*
2*60761Sbostic * Copyright (c) 1983, 1993
3*60761Sbostic * The Regents of the University of California. All rights reserved.
434207Sbostic *
542571Sbostic * %sccs.include.redist.c%
621197Sdist */
712425Smckusick
812425Smckusick #ifndef lint
9*60761Sbostic static char copyright[] =
10*60761Sbostic "@(#) Copyright (c) 1983, 1993\n\
11*60761Sbostic The Regents of the University of California. All rights reserved.\n";
1234207Sbostic #endif /* not lint */
1312425Smckusick
1421197Sdist #ifndef lint
15*60761Sbostic static char sccsid[] = "@(#)cfscores.c 8.1 (Berkeley) 05/31/93";
1634207Sbostic #endif /* not lint */
1721197Sdist
1837014Sbostic #include <sys/types.h>
1912425Smckusick #include <pwd.h>
2041170Sbostic #include "pathnames.h"
2112425Smckusick
2212425Smckusick struct betinfo {
2312425Smckusick long hand; /* cost of dealing hand */
2412425Smckusick long inspection; /* cost of inspecting hand */
2512425Smckusick long game; /* cost of buying game */
2612425Smckusick long runs; /* cost of running through hands */
2712425Smckusick long information; /* cost of information */
2812425Smckusick long thinktime; /* cost of thinking time */
2912425Smckusick long wins; /* total winnings */
3012425Smckusick long worth; /* net worth after costs */
3112425Smckusick };
3212425Smckusick
3312425Smckusick int dbfd;
3412425Smckusick
main(argc,argv)3512425Smckusick main(argc, argv)
3612425Smckusick int argc;
3712425Smckusick char *argv[];
3812425Smckusick {
3912425Smckusick register struct passwd *pw;
4012425Smckusick int uid;
4112425Smckusick
4212425Smckusick if (argc > 2) {
4312425Smckusick printf("Usage: cfscores [user]\n");
4412425Smckusick exit(1);
4512425Smckusick }
4641170Sbostic dbfd = open(_PATH_SCORE, 0);
4712425Smckusick if (dbfd < 0) {
4841170Sbostic perror(_PATH_SCORE);
4912425Smckusick exit(2);
5012425Smckusick }
5112425Smckusick setpwent();
5212425Smckusick if (argc == 1) {
5312425Smckusick uid = getuid();
5412425Smckusick pw = getpwuid(uid);
5512425Smckusick if (pw == 0) {
5612425Smckusick printf("You are not listed in the password file?!?\n");
5712425Smckusick exit(2);
5812425Smckusick }
5912425Smckusick printuser(pw, 1);
6012425Smckusick exit(0);
6112425Smckusick }
6212425Smckusick if (strcmp(argv[1], "-a") == 0) {
6312425Smckusick while ((pw = getpwent()) != 0)
6412425Smckusick printuser(pw, 0);
6512425Smckusick exit(0);
6612425Smckusick }
6712425Smckusick pw = getpwnam(argv[1]);
6812425Smckusick if (pw == 0) {
6912425Smckusick printf("User %s unknown\n", argv[1]);
7012425Smckusick exit(3);
7112425Smckusick }
7212425Smckusick printuser(pw, 1);
7312425Smckusick exit(0);
7412425Smckusick }
7512425Smckusick
7612425Smckusick /*
7712425Smckusick * print out info for specified password entry
7812425Smckusick */
printuser(pw,printfail)7912425Smckusick printuser(pw, printfail)
8012425Smckusick register struct passwd *pw;
8112425Smckusick int printfail;
8212425Smckusick {
8312425Smckusick struct betinfo total;
8412425Smckusick int i;
8512425Smckusick
8612425Smckusick if (pw->pw_uid < 0) {
8712425Smckusick printf("Bad uid %d\n", pw->pw_uid);
8812425Smckusick return;
8912425Smckusick }
9012425Smckusick i = lseek(dbfd, pw->pw_uid * sizeof(struct betinfo), 0);
9112425Smckusick if (i < 0) {
9212425Smckusick perror("lseek");
9312425Smckusick return;
9412425Smckusick }
9512425Smckusick i = read(dbfd, (char *)&total, sizeof(total));
9612425Smckusick if (i < 0) {
9712425Smckusick perror("read");
9812425Smckusick return;
9912425Smckusick }
10017178Sralph if (i == 0 || total.hand == 0) {
10112425Smckusick if (printfail)
10212425Smckusick printf("%s has never played canfield.\n", pw->pw_name);
10312425Smckusick return;
10412425Smckusick }
10512425Smckusick printf("*----------------------*\n");
10612425Smckusick if (total.worth >= 0)
10712425Smckusick printf("* Winnings for %-8s*\n", pw->pw_name);
10812425Smckusick else
10912425Smckusick printf("* Losses for %-10s*\n", pw->pw_name);
11012425Smckusick printf("*======================*\n");
11112425Smckusick printf("|Costs Total |\n");
11212425Smckusick printf("| Hands %8d |\n", total.hand);
11312425Smckusick printf("| Inspections %8d |\n", total.inspection);
11412425Smckusick printf("| Games %8d |\n", total.game);
11512425Smckusick printf("| Runs %8d |\n", total.runs);
11612425Smckusick printf("| Information %8d |\n", total.information);
11712425Smckusick printf("| Think time %8d |\n", total.thinktime);
11812425Smckusick printf("|Total Costs %8d |\n", total.wins - total.worth);
11912425Smckusick printf("|Winnings %8d |\n", total.wins);
12012425Smckusick printf("|Net Worth %8d |\n", total.worth);
12112425Smckusick printf("*----------------------*\n\n");
12212425Smckusick }
123