cfscores.c 4.6 KB

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