rain.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* $NetBSD: rain.c,v 1.17 2004/05/02 21:31:23 christos Exp $ */
  2. /*
  3. * Copyright (c) 1980, 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) 1980, 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[] = "@(#)rain.c 8.1 (Berkeley) 5/31/93";
  38. #else
  39. __RCSID("$NetBSD: rain.c,v 1.17 2004/05/02 21:31:23 christos Exp $");
  40. #endif
  41. #endif /* not lint */
  42. /*
  43. * rain 11/3/1980 EPS/CITHEP
  44. * cc rain.c -o rain -O -ltermlib
  45. */
  46. #include <sys/types.h>
  47. #include <curses.h>
  48. #include <err.h>
  49. #include <signal.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <termios.h>
  53. #include <unistd.h>
  54. #include <errno.h>
  55. #include <limits.h>
  56. static volatile sig_atomic_t sig_caught = 0;
  57. int main(int, char **);
  58. static void onsig(int);
  59. int
  60. main(int argc, char **argv)
  61. {
  62. int x, y, j;
  63. long cols, lines;
  64. unsigned int delay = 0;
  65. unsigned long val = 0;
  66. int ch;
  67. char *ep;
  68. int xpos[5], ypos[5];
  69. /* Revoke setgid privileges */
  70. setregid(getgid(), getgid());
  71. while ((ch = getopt(argc, argv, "d:")) != -1)
  72. switch (ch) {
  73. case 'd':
  74. val = strtoul(optarg, &ep, 0);
  75. if (ep == optarg || *ep)
  76. errx(1, "Invalid delay `%s'", optarg);
  77. if (errno == ERANGE && val == ULONG_MAX)
  78. err(1, "Invalid delay `%s'", optarg);
  79. if (val >= 1000)
  80. errx(1, "Invalid delay `%s' (1-999)", optarg);
  81. delay = (unsigned int)val * 1000; /* ms -> us */
  82. break;
  83. default:
  84. (void)fprintf(stderr, "Usage: %s [-d delay]\n",
  85. getprogname());
  86. return 1;
  87. }
  88. initscr();
  89. cols = COLS - 4;
  90. lines = LINES - 4;
  91. (void)signal(SIGHUP, onsig);
  92. (void)signal(SIGINT, onsig);
  93. (void)signal(SIGTERM, onsig);
  94. curs_set(0);
  95. for (j = 4; j >= 0; --j) {
  96. xpos[j] = random() % cols + 2;
  97. ypos[j] = random() % lines + 2;
  98. }
  99. for (j = 0;;) {
  100. if (sig_caught) {
  101. endwin();
  102. exit(0);
  103. }
  104. x = random() % cols + 2;
  105. y = random() % lines + 2;
  106. mvaddch(y, x, '.');
  107. mvaddch(ypos[j], xpos[j], 'o');
  108. if (!j--)
  109. j = 4;
  110. mvaddch(ypos[j], xpos[j], 'O');
  111. if (!j--)
  112. j = 4;
  113. mvaddch(ypos[j] - 1, xpos[j], '-');
  114. mvaddstr(ypos[j], xpos[j] - 1, "|.|");
  115. mvaddch(ypos[j] + 1, xpos[j], '-');
  116. if (!j--)
  117. j = 4;
  118. mvaddch(ypos[j] - 2, xpos[j], '-');
  119. mvaddstr(ypos[j] - 1, xpos[j] - 1, "/ \\");
  120. mvaddstr(ypos[j], xpos[j] - 2, "| O |");
  121. mvaddstr(ypos[j] + 1, xpos[j] - 1, "\\ /");
  122. mvaddch(ypos[j] + 2, xpos[j], '-');
  123. if (!j--)
  124. j = 4;
  125. mvaddch(ypos[j] - 2, xpos[j], ' ');
  126. mvaddstr(ypos[j] - 1, xpos[j] - 1, " ");
  127. mvaddstr(ypos[j], xpos[j] - 2, " ");
  128. mvaddstr(ypos[j] + 1, xpos[j] - 1, " ");
  129. mvaddch(ypos[j] + 2, xpos[j], ' ');
  130. xpos[j] = x;
  131. ypos[j] = y;
  132. refresh();
  133. if (delay)
  134. usleep(delay);
  135. else
  136. tcdrain(STDOUT_FILENO);
  137. }
  138. }
  139. static void
  140. onsig(int dummy __attribute__((__unused__)))
  141. {
  142. sig_caught = 1;
  143. }