1 /* $OpenBSD: cfscores.c,v 1.24 2018/08/24 11:31:17 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
main(int argc,char * argv[])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 void
printuser(void)85 printuser(void)
86 {
87 struct betinfo total;
88 const char *name;
89 int i;
90
91 name = getlogin();
92 if (name == NULL || *name == '\0')
93 name = " ??? ";
94
95 i = read(dbfd, (char *)&total, sizeof(total));
96 if (i < 0) {
97 warn("lseek %s", scorepath);
98 return;
99 }
100 if (i == 0 || total.hand == 0) {
101 printf("%s has never played canfield.\n", name);
102 return;
103 }
104 i = strlen(name);
105 printf("*----------------------*\n");
106 if (total.worth >= 0) {
107 if (i <= 8)
108 printf("* Winnings for %-8s*\n", name);
109 else {
110 printf("* Winnings for *\n");
111 if (i <= 20)
112 printf("* %20s *\n", name);
113 else
114 printf("%s\n", name);
115 }
116 } else {
117 if (i <= 10)
118 printf("* Losses for %-10s*\n", name);
119 else {
120 printf("* Losses for *\n");
121 if (i <= 20)
122 printf("* %20s *\n", name);
123 else
124 printf("%s\n", name);
125 }
126 }
127 printf("*======================*\n");
128 printf("|Costs Total |\n");
129 printf("| Hands %8ld |\n", total.hand);
130 printf("| Inspections %8ld |\n", total.inspection);
131 printf("| Games %8ld |\n", total.game);
132 printf("| Runs %8ld |\n", total.runs);
133 printf("| Information %8ld |\n", total.information);
134 printf("| Think time %8ld |\n", total.thinktime);
135 printf("|Total Costs %8ld |\n", total.wins - total.worth);
136 printf("|Winnings %8ld |\n", total.wins);
137 printf("|Net Worth %8ld |\n", total.worth);
138 printf("*----------------------*\n\n");
139 }
140