xref: /openbsd/games/canfield/cfscores/cfscores.c (revision 9b7c3dbb)
1 /*	$OpenBSD: cfscores.c,v 1.23 2016/01/08 20:23:54 mestre Exp $	*/
2 /*	$NetBSD: cfscores.c,v 1.3 1995/03/21 15:08:37 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 struct betinfo {
43 	long	hand;		/* cost of dealing hand */
44 	long	inspection;	/* cost of inspecting hand */
45 	long	game;		/* cost of buying game */
46 	long	runs;		/* cost of running through hands */
47 	long	information;	/* cost of information */
48 	long	thinktime;	/* cost of thinking time */
49 	long	wins;		/* total winnings */
50 	long	worth;		/* net worth after costs */
51 };
52 
53 int dbfd;
54 char scorepath[PATH_MAX];
55 
56 void	printuser(void);
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	const char *home;
62 	int ret;
63 
64 	if (pledge("stdio rpath", NULL) == -1)
65 		err(1, "pledge");
66 
67 	home = getenv("HOME");
68 	if (home == NULL || *home == '\0')
69 		err(1, "getenv");
70 
71 	ret = snprintf(scorepath, sizeof(scorepath), "%s/%s", home,
72 	    ".cfscores");
73 	if (ret < 0 || ret >= PATH_MAX)
74 		errc(1, ENAMETOOLONG, "%s/%s", home, ".cfscores");
75 
76 	dbfd = open(scorepath, O_RDONLY);
77 	if (dbfd < 0)
78 		err(2, "%s", scorepath);
79 
80 	printuser();
81 	return 0;
82 }
83 
84 /*
85  * print out info for specified password entry
86  */
87 void
88 printuser(void)
89 {
90 	struct betinfo total;
91 	const char *name;
92 	int i;
93 
94 	name = getlogin();
95 	if (name == NULL || *name == '\0')
96 		name = " ??? ";
97 
98 	i = read(dbfd, (char *)&total, sizeof(total));
99 	if (i < 0) {
100 		warn("lseek %s", scorepath);
101 		return;
102 	}
103 	if (i == 0 || total.hand == 0) {
104 		printf("%s has never played canfield.\n", name);
105 		return;
106 	}
107 	i = strlen(name);
108 	printf("*----------------------*\n");
109 	if (total.worth >= 0) {
110 		if (i <= 8)
111 			printf("* Winnings for %-8s*\n", name);
112 		else {
113 			printf("*     Winnings for     *\n");
114 			if (i <= 20)
115 				printf("* %20s *\n", name);
116 			else
117 				printf("%s\n", name);
118 		}
119 	} else {
120 		if (i <= 10)
121 			printf("* Losses for %-10s*\n", name);
122 		else {
123 			printf("*      Losses for      *\n");
124 			if (i <= 20)
125 				printf("* %20s *\n", name);
126 			else
127 				printf("%s\n", name);
128 		}
129 	}
130 	printf("*======================*\n");
131 	printf("|Costs           Total |\n");
132 	printf("| Hands       %8ld |\n", total.hand);
133 	printf("| Inspections %8ld |\n", total.inspection);
134 	printf("| Games       %8ld |\n", total.game);
135 	printf("| Runs        %8ld |\n", total.runs);
136 	printf("| Information %8ld |\n", total.information);
137 	printf("| Think time  %8ld |\n", total.thinktime);
138 	printf("|Total Costs  %8ld |\n", total.wins - total.worth);
139 	printf("|Winnings     %8ld |\n", total.wins);
140 	printf("|Net Worth    %8ld |\n", total.worth);
141 	printf("*----------------------*\n\n");
142 }
143