pl_1.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* $NetBSD: pl_1.c,v 1.18 2004/01/27 20:23:36 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. #if 0
  33. static char sccsid[] = "@(#)pl_1.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: pl_1.c,v 1.18 2004/01/27 20:23:36 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <sys/types.h>
  39. #include <sys/wait.h>
  40. #include <signal.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include "extern.h"
  45. #include "player.h"
  46. /*
  47. * If we get here before a ship is chosen, then ms == 0 and
  48. * we don't want to update the score file, or do any Write's either.
  49. * We can assume the sync file is already created and may need
  50. * to be removed.
  51. * Of course, we don't do any more Sync()'s if we got here
  52. * because of a Sync() failure.
  53. */
  54. void
  55. leave(int conditions)
  56. {
  57. signal(SIGHUP, SIG_IGN);
  58. signal(SIGINT, SIG_IGN);
  59. signal(SIGQUIT, SIG_IGN);
  60. signal(SIGALRM, SIG_IGN);
  61. signal(SIGCHLD, SIG_IGN);
  62. if (done_curses) {
  63. Msg("It looks like you've had it!");
  64. switch (conditions) {
  65. case LEAVE_QUIT:
  66. break;
  67. case LEAVE_CAPTURED:
  68. Msg("Your ship was captured.");
  69. break;
  70. case LEAVE_HURRICAN:
  71. Msg("Hurricane! All ships destroyed.");
  72. break;
  73. case LEAVE_DRIVER:
  74. Msg("The driver died.");
  75. break;
  76. case LEAVE_SYNC:
  77. Msg("Synchronization error.");
  78. break;
  79. default:
  80. Msg("A funny thing happened (%d).", conditions);
  81. }
  82. } else {
  83. switch (conditions) {
  84. case LEAVE_QUIT:
  85. break;
  86. case LEAVE_DRIVER:
  87. printf("The driver died.\n");
  88. break;
  89. case LEAVE_FORK:
  90. perror("fork");
  91. break;
  92. case LEAVE_SYNC:
  93. printf("Synchronization error\n.");
  94. break;
  95. default:
  96. printf("A funny thing happened (%d).\n",
  97. conditions);
  98. }
  99. }
  100. if (ms != 0) {
  101. logger(ms);
  102. if (conditions != LEAVE_SYNC) {
  103. makemsg(ms, "Captain %s relinquishing.",
  104. mf->captain);
  105. Write(W_END, ms, 0, 0, 0, 0);
  106. Sync();
  107. }
  108. }
  109. sync_close(!hasdriver);
  110. sleep(5);
  111. cleanupscreen();
  112. exit(0);
  113. }
  114. /*ARGSUSED*/
  115. void
  116. choke(int n __attribute__((__unused__)))
  117. {
  118. leave(LEAVE_QUIT);
  119. }
  120. /*ARGSUSED*/
  121. void
  122. child(int n __attribute__((__unused__)))
  123. {
  124. int status;
  125. int pid;
  126. signal(SIGCHLD, SIG_IGN);
  127. do {
  128. pid = wait3(&status, WNOHANG, (struct rusage *)0);
  129. if (pid < 0 || (pid > 0 && !WIFSTOPPED(status)))
  130. hasdriver = 0;
  131. } while (pid > 0);
  132. signal(SIGCHLD, child);
  133. }