📄 hunt.c
字号:
test_one_host: msg = htons(C_TESTMSG()); (void) sendto(test_socket, (char *) &msg, sizeof msg, 0, (struct sockaddr *) &test, DAEMON_SIZE); goto get_response;}find_driver(do_startup)FLAG do_startup;{ SOCKET *hosts; hosts = list_drivers(); if (hosts[0].sin_port != htons(0)) { int i, c; if (hosts[1].sin_port == htons(0)) { Daemon = hosts[0]; return; } /* go thru list and return host that matches daemon */ clear_the_screen();# ifdef USE_CURSES move(1, 0);# else mvcur(cur_row, cur_col, 1, 0); cur_row = 1; cur_col = 0;# endif put_str("Pick one:"); for (i = 0; i < HEIGHT - 4 && hosts[i].sin_port != htons(0); i += 1) { struct hostent *hp; char buf[80];# ifdef USE_CURSES move(3 + i, 0);# else mvcur(cur_row, cur_col, 3 + i, 0); cur_row = 3 + i; cur_col = 0;# endif hp = gethostbyaddr((char *) &hosts[i].sin_addr, sizeof hosts[i].sin_addr, AF_INET); (void) sprintf(buf, "%8c %.64s", 'a' + i, hp != NULL ? hp->h_name : inet_ntoa(hosts->sin_addr)); put_str(buf); }# ifdef USE_CURSES move(4 + i, 0);# else mvcur(cur_row, cur_col, 4 + i, 0); cur_row = 4 + i; cur_col = 0;# endif put_str("Enter letter: "); refresh(); while (!islower(c = getchar()) || (c -= 'a') >= i) { beep(); refresh(); } Daemon = hosts[c]; clear_the_screen(); return; } if (!do_startup) return; start_driver(); sleep(2); find_driver(FALSE);}dump_scores(host) SOCKET host;{ struct hostent *hp; int s; char buf[BUFSIZ]; int cnt; hp = gethostbyaddr((char *) &host.sin_addr, sizeof host.sin_addr, AF_INET); printf("\n%s:\n", hp != NULL ? hp->h_name : inet_ntoa(host.sin_addr)); fflush(stdout); s = socket(SOCK_FAMILY, SOCK_STREAM, 0); if (s < 0) { perror("socket"); exit(1); } if (connect(s, (struct sockaddr *) &host, sizeof host) < 0) { perror("connect"); exit(1); } while ((cnt = read(s, buf, BUFSIZ)) > 0) write(fileno(stdout), buf, cnt); (void) close(s);}# endifstart_driver(){ register int procid;# ifdef MONITOR if (Am_monitor) { leave(1, "No one playing."); /* NOTREACHED */ }# endif# ifdef INTERNET if (Sock_host != NULL) { sleep(3); return; }# endif# ifdef USE_CURSES move(HEIGHT, 0);# else mvcur(cur_row, cur_col, HEIGHT, 0); cur_row = HEIGHT; cur_col = 0;# endif put_str("Starting..."); refresh(); procid = fork(); if (procid == -1) { perror("fork"); leave(1, "fork failed."); } if (procid == 0) { (void) signal(SIGINT, SIG_IGN);# ifndef INTERNET (void) close(Socket);# else if (use_port == NULL)# endif execl(Driver, "HUNT", (char *) NULL);# ifdef INTERNET else execl(Driver, "HUNT", "-p", use_port, (char *) NULL);# endif /* only get here if exec failed */ (void) kill(getppid(), SIGEMT); /* tell mom */ _exit(1); }# ifdef USE_CURSES move(HEIGHT, 0);# else mvcur(cur_row, cur_col, HEIGHT, 0); cur_row = HEIGHT; cur_col = 0;# endif put_str("Connecting..."); refresh();}/* * bad_con: * We had a bad connection. For the moment we assume that this * means the game is full. */bad_con(){ leave(1, "The game is full. Sorry."); /* NOTREACHED */}/* * bad_ver: * version number mismatch. */bad_ver(){ leave(1, "Version number mismatch. No go."); /* NOTREACHED */}/* * sigterm: * Handle a terminate signal */SIGNAL_TYPEsigterm(){ leave(0, (char *) NULL); /* NOTREACHED */}/* * sigemt: * Handle a emt signal - shouldn't happen on vaxes(?) */SIGNAL_TYPEsigemt(){ leave(1, "Unable to start driver. Try again."); /* NOTREACHED */}# ifdef INTERNET/* * sigalrm: * Handle an alarm signal */SIGNAL_TYPEsigalrm(){ return;}# endif/* * rmnl: * Remove a '\n' at the end of a string if there is one */rmnl(s)char *s;{ register char *cp; cp = strrchr(s, '\n'); if (cp != NULL) *cp = '\0';}/* * intr: * Handle a interrupt signal */SIGNAL_TYPEintr(){ register int ch; register int explained; register int y, x; (void) signal(SIGINT, SIG_IGN);# ifdef USE_CURSES getyx(stdscr, y, x); move(HEIGHT, 0);# else y = cur_row; x = cur_col; mvcur(cur_row, cur_col, HEIGHT, 0); cur_row = HEIGHT; cur_col = 0;# endif put_str("Really quit? "); clear_eol(); refresh(); explained = FALSE; for (;;) { ch = getchar(); if (isupper(ch)) ch = tolower(ch); if (ch == 'y') { if (Socket != 0) { (void) write(Socket, "q", 1); (void) close(Socket); } leave(0, (char *) NULL); } else if (ch == 'n') { (void) signal(SIGINT, intr);# ifdef USE_CURSES move(y, x);# else mvcur(cur_row, cur_col, y, x); cur_row = y; cur_col = x;# endif refresh(); return; } if (!explained) { put_str("(Yes or No) "); refresh(); explained = TRUE; } beep(); refresh(); }}/* * leave: * Leave the game somewhat gracefully, restoring all current * tty stats. */leave(eval, mesg)int eval;char *mesg;{ if (in_visual) {# ifdef USE_CURSES move(HEIGHT, 0); refresh(); endwin();# else /* !USE_CURSES */ mvcur(cur_row, cur_col, HEIGHT, 0); (void) fflush(stdout); /* flush in case VE changes pages */# if defined(BSD_RELEASE) && BSD_RELEASE >= 44 tcsetattr(0, TCSADRAIN, &__orig_termios);# else resetty();# endif _puts(VE); _puts(TE);# endif /* !USE_CURSES */ } if (mesg != NULL) puts(mesg); exit(eval);}#if !defined(USE_CURSES) && defined(SIGTSTP)/* * tstp: * Handle stop and start signals */SIGNAL_TYPEtstp(){# if BSD_RELEASE < 44 static struct sgttyb tty;# endif int y, x; y = cur_row; x = cur_col; mvcur(cur_row, cur_col, HEIGHT, 0); cur_row = HEIGHT; cur_col = 0;# if !defined(BSD_RELEASE) || BSD_RELEASE < 44 tty = _tty;# endif _puts(VE); _puts(TE); (void) fflush(stdout);# if defined(BSD_RELEASE) && BSD_RELEASE >= 44 tcsetattr(0, TCSADRAIN, &__orig_termios);# else resetty();# endif (void) kill(getpid(), SIGSTOP); (void) signal(SIGTSTP, tstp);# if defined(BSD_RELEASE) && BSD_RELEASE >= 44 tcsetattr(0, TCSADRAIN, &saved_tty);# else _tty = tty; ioctl(_tty_ch, TIOCSETP, &_tty);# endif _puts(TI); _puts(VS); cur_row = y; cur_col = x; _puts(tgoto(CM, cur_row, cur_col)); redraw_screen(); (void) fflush(stdout);}#endif /* !defined(USE_CURSES) && defined(SIGTSTP) */# if defined(BSD_RELEASE) && BSD_RELEASE < 43char *strpbrk(s, brk) register char *s, *brk;{ register char *p; register c; while (c = *s) { for (p = brk; *p; p++) if (c == *p) return (s); s++; } return (0);}# endiflongenv_init(enter_status) long enter_status;{ register int i; char *envp, *envname, *s; for (i = 0; i < 256; i++) map_key[i] = (char) i; envname = NULL; if ((envp = getenv("HUNT")) != NULL) { while ((s = strpbrk(envp, "=,")) != NULL) { if (strncmp(envp, "cloak,", s - envp + 1) == 0) { enter_status = Q_CLOAK; envp = s + 1; } else if (strncmp(envp, "scan,", s - envp + 1) == 0) { enter_status = Q_SCAN; envp = s + 1; } else if (strncmp(envp, "fly,", s - envp + 1) == 0) { enter_status = Q_FLY; envp = s + 1; } else if (strncmp(envp, "nobeep,", s - envp + 1) == 0) { no_beep = TRUE; envp = s + 1; } else if (strncmp(envp, "name=", s - envp + 1) == 0) { envname = s + 1; if ((s = strchr(envp, ',')) == NULL) { *envp = '\0'; strncpy(name, envname, NAMELEN); break; } *s = '\0'; strncpy(name, envname, NAMELEN); envp = s + 1; }# ifdef INTERNET else if (strncmp(envp, "port=", s - envp + 1) == 0) { use_port = s + 1; Test_port = atoi(use_port); if ((s = strchr(envp, ',')) == NULL) { *envp = '\0'; break; } *s = '\0'; envp = s + 1; } else if (strncmp(envp, "host=", s - envp + 1) == 0) { Sock_host = s + 1; if ((s = strchr(envp, ',')) == NULL) { *envp = '\0'; break; } *s = '\0'; envp = s + 1; } else if (strncmp(envp, "message=", s - envp + 1) == 0) { Send_message = s + 1; if ((s = strchr(envp, ',')) == NULL) { *envp = '\0'; break; } *s = '\0'; envp = s + 1; }# endif else if (strncmp(envp, "team=", s - envp + 1) == 0) { team = *(s + 1); if (!isdigit(team)) team = ' '; if ((s = strchr(envp, ',')) == NULL) { *envp = '\0'; break; } *s = '\0'; envp = s + 1; } /* must be last option */ else if (strncmp(envp, "mapkey=", s - envp + 1) == 0) { for (s = s + 1; *s != '\0'; s += 2) { map_key[(unsigned int) *s] = *(s + 1); if (*(s + 1) == '\0') { break; } } *envp = '\0'; break; } else { *s = '\0'; printf("unknown option %s\n", envp); if ((s = strchr(envp, ',')) == NULL) { *envp = '\0'; break; } envp = s + 1; } } if (*envp != '\0') if (envname == NULL) strncpy(name, envp, NAMELEN); else printf("unknown option %s\n", envp); } return enter_status;}fill_in_blanks(){ register int i; register char *cp;again: if (name[0] != '\0') { printf("Entering as '%s'", name); if (team != ' ') printf(" on team %c.\n", team); else putchar('\n'); } else { printf("Enter your code name: "); if (fgets(name, NAMELEN, stdin) == NULL) exit(1); } rmnl(name); if (name[0] == '\0') { name[0] = '\0'; printf("You have to have a code name!\n"); goto again; } for (cp = name; *cp != '\0'; cp++) if (!isprint(*cp)) { name[0] = '\0'; printf("Illegal character in your code name.\n"); goto again; } if (team == ' ') { printf("Enter your team (0-9 or nothing): "); i = getchar(); if (isdigit(i)) team = i; while (i != '\n' && i != EOF) i = getchar(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -