mirror of
https://github.com/vattam/BSDGames.git
synced 2025-12-21 03:14:50 +00:00
add missing files
git-svn-id: file:///srv/svn/joey/trunk/src/packages/bsdgames@9776 a4a2c43b-8ac3-0310-8836-e0e880c912e2
This commit is contained in:
638
hunt/hunt/playit.c
Normal file
638
hunt/hunt/playit.c
Normal file
@@ -0,0 +1,638 @@
|
||||
/* $NetBSD: playit.c,v 1.6 2003/02/26 07:14:44 jdc Exp $ */
|
||||
/*
|
||||
* Hunt
|
||||
* Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
|
||||
* San Francisco, California
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifndef lint
|
||||
__RCSID("$NetBSD: playit.c,v 1.6 2003/02/26 07:14:44 jdc Exp $");
|
||||
#endif /* not lint */
|
||||
|
||||
# include <sys/file.h>
|
||||
# include <sys/poll.h>
|
||||
# include <err.h>
|
||||
# include <errno.h>
|
||||
# include <curses.h>
|
||||
# include <ctype.h>
|
||||
# include <signal.h>
|
||||
# include <sys/time.h>
|
||||
# if defined(HPUX) || (defined(BSD_RELEASE) && BSD_RELEASE >= 44)
|
||||
# include <termios.h>
|
||||
# include <unistd.h>
|
||||
# endif
|
||||
# include "hunt.h"
|
||||
|
||||
# ifndef FREAD
|
||||
# define FREAD 1
|
||||
# endif
|
||||
|
||||
# if !defined(USE_CURSES) || !defined(TERMINFO)
|
||||
# define beep() (void) putchar(CTRL('G'))
|
||||
# endif
|
||||
# if !defined(USE_CURSES)
|
||||
# undef refresh
|
||||
# define refresh() (void) fflush(stdout);
|
||||
# endif
|
||||
# ifdef USE_CURSES
|
||||
# define clear_eol() clrtoeol()
|
||||
# define put_ch addch
|
||||
# define put_str addstr
|
||||
# endif
|
||||
|
||||
static int nchar_send;
|
||||
# ifndef USE_CURSES
|
||||
char screen[SCREEN_HEIGHT][SCREEN_WIDTH2], blanks[SCREEN_WIDTH];
|
||||
int cur_row, cur_col;
|
||||
# endif
|
||||
# ifdef OTTO
|
||||
int Otto_count;
|
||||
int Otto_mode;
|
||||
static int otto_y, otto_x;
|
||||
static char otto_face;
|
||||
# endif
|
||||
|
||||
# define MAX_SEND 5
|
||||
# define STDIN 0
|
||||
|
||||
/*
|
||||
* ibuf is the input buffer used for the stream from the driver.
|
||||
* It is small because we do not check for user input when there
|
||||
* are characters in the input buffer.
|
||||
*/
|
||||
static int icnt = 0;
|
||||
static unsigned char ibuf[256], *iptr = ibuf;
|
||||
|
||||
#define GETCHR() (--icnt < 0 ? getchr() : *iptr++)
|
||||
|
||||
#if !defined(BSD_RELEASE) || BSD_RELEASE < 44
|
||||
extern int _putchar();
|
||||
#endif
|
||||
|
||||
static unsigned char getchr __P((void));
|
||||
static void send_stuff __P((void));
|
||||
|
||||
/*
|
||||
* playit:
|
||||
* Play a given game, handling all the curses commands from
|
||||
* the driver.
|
||||
*/
|
||||
void
|
||||
playit()
|
||||
{
|
||||
int ch;
|
||||
int y, x;
|
||||
u_int32_t version;
|
||||
|
||||
if (read(Socket, (char *) &version, LONGLEN) != LONGLEN) {
|
||||
bad_con();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
if (ntohl(version) != (unsigned long)HUNT_VERSION) {
|
||||
bad_ver();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
errno = 0;
|
||||
# ifdef OTTO
|
||||
Otto_count = 0;
|
||||
# endif
|
||||
nchar_send = MAX_SEND;
|
||||
while ((ch = GETCHR()) != EOF) {
|
||||
# ifdef DEBUG
|
||||
fputc(ch, stderr);
|
||||
# endif
|
||||
switch (ch & 0377) {
|
||||
case MOVE:
|
||||
y = GETCHR();
|
||||
x = GETCHR();
|
||||
# ifdef USE_CURSES
|
||||
move(y, x);
|
||||
# else
|
||||
mvcur(cur_row, cur_col, y, x);
|
||||
cur_row = y;
|
||||
cur_col = x;
|
||||
# endif
|
||||
break;
|
||||
case ADDCH:
|
||||
ch = GETCHR();
|
||||
# ifdef OTTO
|
||||
switch (ch) {
|
||||
|
||||
case '<':
|
||||
case '>':
|
||||
case '^':
|
||||
case 'v':
|
||||
otto_face = ch;
|
||||
# ifdef USE_CURSES
|
||||
getyx(stdscr, otto_y, otto_x);
|
||||
# else
|
||||
otto_y = cur_row;
|
||||
otto_x = cur_col;
|
||||
# endif
|
||||
break;
|
||||
}
|
||||
# endif
|
||||
put_ch(ch);
|
||||
break;
|
||||
case CLRTOEOL:
|
||||
clear_eol();
|
||||
break;
|
||||
case CLEAR:
|
||||
clear_the_screen();
|
||||
break;
|
||||
case REFRESH:
|
||||
refresh();
|
||||
break;
|
||||
case REDRAW:
|
||||
redraw_screen();
|
||||
refresh();
|
||||
break;
|
||||
case ENDWIN:
|
||||
refresh();
|
||||
if ((ch = GETCHR()) == LAST_PLAYER)
|
||||
Last_player = TRUE;
|
||||
ch = EOF;
|
||||
goto out;
|
||||
case BELL:
|
||||
beep();
|
||||
break;
|
||||
case READY:
|
||||
refresh();
|
||||
if (nchar_send < 0)
|
||||
# if defined(HPUX) || (defined(BSD_RELEASE) && BSD_RELEASE >= 44)
|
||||
tcflush(STDIN, TCIFLUSH);
|
||||
# else
|
||||
# ifndef TCFLSH
|
||||
(void) ioctl(STDIN, TIOCFLUSH, &in);
|
||||
# else
|
||||
(void) ioctl(STDIN, TCFLSH, 0);
|
||||
# endif
|
||||
# endif
|
||||
nchar_send = MAX_SEND;
|
||||
# ifndef OTTO
|
||||
(void) GETCHR();
|
||||
# else
|
||||
Otto_count -= (GETCHR() & 0xff);
|
||||
if (!Am_monitor) {
|
||||
# ifdef DEBUG
|
||||
fputc('0' + Otto_count, stderr);
|
||||
# endif
|
||||
if (Otto_count == 0 && Otto_mode)
|
||||
otto(otto_y, otto_x, otto_face);
|
||||
}
|
||||
# endif
|
||||
break;
|
||||
default:
|
||||
# ifdef OTTO
|
||||
switch (ch) {
|
||||
|
||||
case '<':
|
||||
case '>':
|
||||
case '^':
|
||||
case 'v':
|
||||
otto_face = ch;
|
||||
# ifdef USE_CURSES
|
||||
getyx(stdscr, otto_y, otto_x);
|
||||
# else
|
||||
otto_y = cur_row;
|
||||
otto_x = cur_col;
|
||||
# endif
|
||||
break;
|
||||
}
|
||||
# endif
|
||||
put_ch(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
out:
|
||||
(void) close(Socket);
|
||||
}
|
||||
|
||||
/*
|
||||
* getchr:
|
||||
* Grab input and pass it along to the driver
|
||||
* Return any characters from the driver
|
||||
* When this routine is called by GETCHR, we already know there are
|
||||
* no characters in the input buffer.
|
||||
*/
|
||||
static unsigned char
|
||||
getchr()
|
||||
{
|
||||
struct pollfd set[2];
|
||||
int nfds;
|
||||
|
||||
set[0].fd = Socket;
|
||||
set[0].events = POLLIN;
|
||||
set[1].fd = STDIN;
|
||||
set[1].events = POLLIN;
|
||||
|
||||
one_more_time:
|
||||
do {
|
||||
errno = 0;
|
||||
nfds = poll(set, 2, INFTIM);
|
||||
} while (nfds <= 0 && errno == EINTR);
|
||||
|
||||
if (set[1].revents && POLLIN)
|
||||
send_stuff();
|
||||
if (! (set[0].revents & POLLIN))
|
||||
goto one_more_time;
|
||||
icnt = read(Socket, ibuf, sizeof ibuf);
|
||||
if (icnt < 0) {
|
||||
bad_con();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
if (icnt == 0)
|
||||
goto one_more_time;
|
||||
iptr = ibuf;
|
||||
icnt--;
|
||||
return *iptr++;
|
||||
}
|
||||
|
||||
/*
|
||||
* send_stuff:
|
||||
* Send standard input characters to the driver
|
||||
*/
|
||||
static void
|
||||
send_stuff()
|
||||
{
|
||||
int count;
|
||||
char *sp, *nsp;
|
||||
static char inp[sizeof Buf];
|
||||
|
||||
count = read(STDIN, Buf, sizeof Buf);
|
||||
if (count <= 0)
|
||||
return;
|
||||
if (nchar_send <= 0 && !no_beep) {
|
||||
(void) write(1, "\7", 1); /* CTRL('G') */
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* look for 'q'uit commands; if we find one,
|
||||
* confirm it. If it is not confirmed, strip
|
||||
* it out of the input
|
||||
*/
|
||||
Buf[count] = '\0';
|
||||
nsp = inp;
|
||||
for (sp = Buf; *sp != '\0'; sp++)
|
||||
if ((*nsp = map_key[(int)*sp]) == 'q')
|
||||
intr(0);
|
||||
else
|
||||
nsp++;
|
||||
count = nsp - inp;
|
||||
if (count) {
|
||||
# ifdef OTTO
|
||||
Otto_count += count;
|
||||
# endif
|
||||
nchar_send -= count;
|
||||
if (nchar_send < 0)
|
||||
count += nchar_send;
|
||||
(void) write(Socket, inp, count);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* quit:
|
||||
* Handle the end of the game when the player dies
|
||||
*/
|
||||
int
|
||||
quit(old_status)
|
||||
int old_status;
|
||||
{
|
||||
int explain, ch;
|
||||
|
||||
if (Last_player)
|
||||
return Q_QUIT;
|
||||
# ifdef OTTO
|
||||
if (Otto_mode)
|
||||
return Q_CLOAK;
|
||||
# endif
|
||||
# ifdef USE_CURSES
|
||||
move(HEIGHT, 0);
|
||||
# else
|
||||
mvcur(cur_row, cur_col, HEIGHT, 0);
|
||||
cur_row = HEIGHT;
|
||||
cur_col = 0;
|
||||
# endif
|
||||
put_str("Re-enter game [ynwo]? ");
|
||||
clear_eol();
|
||||
explain = FALSE;
|
||||
for (;;) {
|
||||
refresh();
|
||||
if (isupper(ch = getchar()))
|
||||
ch = tolower(ch);
|
||||
if (ch == 'y')
|
||||
return old_status;
|
||||
else if (ch == 'o')
|
||||
break;
|
||||
else if (ch == 'n') {
|
||||
# ifndef INTERNET
|
||||
return Q_QUIT;
|
||||
# else
|
||||
# ifdef USE_CURSES
|
||||
move(HEIGHT, 0);
|
||||
# else
|
||||
mvcur(cur_row, cur_col, HEIGHT, 0);
|
||||
cur_row = HEIGHT;
|
||||
cur_col = 0;
|
||||
# endif
|
||||
put_str("Write a parting message [yn]? ");
|
||||
clear_eol();
|
||||
refresh();
|
||||
for (;;) {
|
||||
if (isupper(ch = getchar()))
|
||||
ch = tolower(ch);
|
||||
if (ch == 'y')
|
||||
goto get_message;
|
||||
if (ch == 'n')
|
||||
return Q_QUIT;
|
||||
}
|
||||
# endif
|
||||
}
|
||||
# ifdef INTERNET
|
||||
else if (ch == 'w') {
|
||||
static char buf[WIDTH + WIDTH % 2];
|
||||
char *cp, c;
|
||||
|
||||
get_message:
|
||||
c = ch; /* save how we got here */
|
||||
# ifdef USE_CURSES
|
||||
move(HEIGHT, 0);
|
||||
# else
|
||||
mvcur(cur_row, cur_col, HEIGHT, 0);
|
||||
cur_row = HEIGHT;
|
||||
cur_col = 0;
|
||||
# endif
|
||||
put_str("Message: ");
|
||||
clear_eol();
|
||||
refresh();
|
||||
cp = buf;
|
||||
for (;;) {
|
||||
refresh();
|
||||
if ((ch = getchar()) == '\n' || ch == '\r')
|
||||
break;
|
||||
# if defined(TERMINFO) || BSD_RELEASE >= 44
|
||||
if (ch == erasechar())
|
||||
# else
|
||||
if (ch == _tty.sg_erase)
|
||||
# endif
|
||||
{
|
||||
if (cp > buf) {
|
||||
# ifdef USE_CURSES
|
||||
int y, x;
|
||||
getyx(stdscr, y, x);
|
||||
move(y, x - 1);
|
||||
# else
|
||||
mvcur(cur_row, cur_col, cur_row,
|
||||
cur_col - 1);
|
||||
cur_col -= 1;
|
||||
# endif
|
||||
cp -= 1;
|
||||
clear_eol();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
# if defined(TERMINFO) || BSD_RELEASE >= 44
|
||||
else if (ch == killchar())
|
||||
# else
|
||||
else if (ch == _tty.sg_kill)
|
||||
# endif
|
||||
{
|
||||
# ifdef USE_CURSES
|
||||
int y, x;
|
||||
getyx(stdscr, y, x);
|
||||
move(y, x - (cp - buf));
|
||||
# else
|
||||
mvcur(cur_row, cur_col, cur_row,
|
||||
cur_col - (cp - buf));
|
||||
cur_col -= cp - buf;
|
||||
# endif
|
||||
cp = buf;
|
||||
clear_eol();
|
||||
continue;
|
||||
} else if (!isprint(ch)) {
|
||||
beep();
|
||||
continue;
|
||||
}
|
||||
put_ch(ch);
|
||||
*cp++ = ch;
|
||||
if (cp + 1 >= buf + sizeof buf)
|
||||
break;
|
||||
}
|
||||
*cp = '\0';
|
||||
Send_message = buf;
|
||||
return (c == 'w') ? old_status : Q_MESSAGE;
|
||||
}
|
||||
# endif
|
||||
beep();
|
||||
if (!explain) {
|
||||
put_str("(Yes, No, Write message, or Options) ");
|
||||
explain = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
# ifdef USE_CURSES
|
||||
move(HEIGHT, 0);
|
||||
# else
|
||||
mvcur(cur_row, cur_col, HEIGHT, 0);
|
||||
cur_row = HEIGHT;
|
||||
cur_col = 0;
|
||||
# endif
|
||||
# ifdef FLY
|
||||
put_str("Scan, Cloak, Flying, or Quit? ");
|
||||
# else
|
||||
put_str("Scan, Cloak, or Quit? ");
|
||||
# endif
|
||||
clear_eol();
|
||||
refresh();
|
||||
explain = FALSE;
|
||||
for (;;) {
|
||||
if (isupper(ch = getchar()))
|
||||
ch = tolower(ch);
|
||||
if (ch == 's')
|
||||
return Q_SCAN;
|
||||
else if (ch == 'c')
|
||||
return Q_CLOAK;
|
||||
# ifdef FLY
|
||||
else if (ch == 'f')
|
||||
return Q_FLY;
|
||||
# endif
|
||||
else if (ch == 'q')
|
||||
return Q_QUIT;
|
||||
beep();
|
||||
if (!explain) {
|
||||
# ifdef FLY
|
||||
put_str("[SCFQ] ");
|
||||
# else
|
||||
put_str("[SCQ] ");
|
||||
# endif
|
||||
explain = TRUE;
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
||||
# ifndef USE_CURSES
|
||||
void
|
||||
put_ch(ch)
|
||||
char ch;
|
||||
{
|
||||
if (!isprint(ch)) {
|
||||
fprintf(stderr, "r,c,ch: %d,%d,%d", cur_row, cur_col, ch);
|
||||
return;
|
||||
}
|
||||
screen[cur_row][cur_col] = ch;
|
||||
putchar(ch);
|
||||
if (++cur_col >= COLS) {
|
||||
if (!AM || XN)
|
||||
putchar('\n');
|
||||
cur_col = 0;
|
||||
if (++cur_row >= LINES)
|
||||
cur_row = LINES;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
put_str(s)
|
||||
const char *s;
|
||||
{
|
||||
while (*s)
|
||||
put_ch(*s++);
|
||||
}
|
||||
# endif
|
||||
|
||||
void
|
||||
clear_the_screen()
|
||||
{
|
||||
# ifdef USE_CURSES
|
||||
clear();
|
||||
move(0, 0);
|
||||
refresh();
|
||||
# else
|
||||
int i;
|
||||
|
||||
if (blanks[0] == '\0')
|
||||
for (i = 0; i < SCREEN_WIDTH; i++)
|
||||
blanks[i] = ' ';
|
||||
|
||||
if (CL != NULL) {
|
||||
#if !defined(BSD_RELEASE) || BSD_RELEASE < 44
|
||||
tputs(CL, LINES, _putchar);
|
||||
#else
|
||||
tputs(CL, LINES, __cputchar);
|
||||
#endif
|
||||
for (i = 0; i < SCREEN_HEIGHT; i++)
|
||||
memcpy(screen[i], blanks, SCREEN_WIDTH);
|
||||
} else {
|
||||
for (i = 0; i < SCREEN_HEIGHT; i++) {
|
||||
mvcur(cur_row, cur_col, i, 0);
|
||||
cur_row = i;
|
||||
cur_col = 0;
|
||||
clear_eol();
|
||||
}
|
||||
mvcur(cur_row, cur_col, 0, 0);
|
||||
}
|
||||
cur_row = cur_col = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef USE_CURSES
|
||||
void
|
||||
clear_eol()
|
||||
{
|
||||
if (CE != NULL)
|
||||
#if !defined(BSD_RELEASE) || BSD_RELEASE < 44
|
||||
tputs(CE, 1, _putchar);
|
||||
#else
|
||||
tputs(CE, 1, __cputchar);
|
||||
#endif
|
||||
else {
|
||||
fwrite(blanks, sizeof (char), SCREEN_WIDTH - cur_col, stdout);
|
||||
if (COLS != SCREEN_WIDTH)
|
||||
mvcur(cur_row, SCREEN_WIDTH, cur_row, cur_col);
|
||||
else if (AM)
|
||||
mvcur(cur_row + 1, 0, cur_row, cur_col);
|
||||
else
|
||||
mvcur(cur_row, SCREEN_WIDTH - 1, cur_row, cur_col);
|
||||
}
|
||||
memcpy(&screen[cur_row][cur_col], blanks, SCREEN_WIDTH - cur_col);
|
||||
}
|
||||
# endif
|
||||
|
||||
void
|
||||
redraw_screen()
|
||||
{
|
||||
# ifdef USE_CURSES
|
||||
clearok(stdscr, TRUE);
|
||||
touchwin(stdscr);
|
||||
# else
|
||||
int i;
|
||||
# ifndef NOCURSES
|
||||
static int first = 1;
|
||||
|
||||
if (first) {
|
||||
curscr = newwin(SCREEN_HEIGHT, SCREEN_WIDTH, 0, 0);
|
||||
if (curscr == NULL)
|
||||
errx(1, "Can't create curscr");
|
||||
# if !defined(BSD_RELEASE) || BSD_RELEASE < 44
|
||||
for (i = 0; i < SCREEN_HEIGHT; i++)
|
||||
curscr->_y[i] = screen[i];
|
||||
# endif
|
||||
first = 0;
|
||||
}
|
||||
# if defined(BSD_RELEASE) && BSD_RELEASE >= 44
|
||||
for (i = 0; i < SCREEN_HEIGHT; i++) {
|
||||
int j;
|
||||
|
||||
for (j = 0; j < SCREEN_WIDTH; j++)
|
||||
curscr->lines[i]->line[j].ch = screen[i][j];
|
||||
}
|
||||
curscr->cury = cur_row;
|
||||
curscr->curx = cur_col;
|
||||
# else
|
||||
curscr->_cury = cur_row;
|
||||
curscr->_curx = cur_col;
|
||||
# endif
|
||||
clearok(curscr, TRUE);
|
||||
touchwin(curscr);
|
||||
wrefresh(curscr);
|
||||
#else
|
||||
mvcur(cur_row, cur_col, 0, 0);
|
||||
for (i = 0; i < SCREEN_HEIGHT - 1; i++) {
|
||||
fwrite(screen[i], sizeof (char), SCREEN_WIDTH, stdout);
|
||||
if (COLS > SCREEN_WIDTH || (COLS == SCREEN_WIDTH && !AM))
|
||||
putchar('\n');
|
||||
}
|
||||
fwrite(screen[SCREEN_HEIGHT - 1], sizeof (char), SCREEN_WIDTH - 1,
|
||||
stdout);
|
||||
mvcur(SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, cur_row, cur_col);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* do_message:
|
||||
* Send a message to the driver and return
|
||||
*/
|
||||
void
|
||||
do_message()
|
||||
{
|
||||
u_int32_t version;
|
||||
|
||||
if (read(Socket, (char *) &version, LONGLEN) != LONGLEN) {
|
||||
bad_con();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
if (ntohl(version) != (unsigned long)HUNT_VERSION) {
|
||||
bad_ver();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
# ifdef INTERNET
|
||||
if (write(Socket, Send_message, strlen(Send_message)) < 0) {
|
||||
bad_con();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
# endif
|
||||
(void) close(Socket);
|
||||
}
|
||||
7
hunt/huntd/.cvsignore
Normal file
7
hunt/huntd/.cvsignore
Normal file
@@ -0,0 +1,7 @@
|
||||
a.out
|
||||
*.d
|
||||
*.i
|
||||
*.s
|
||||
*.d.tmp
|
||||
huntd
|
||||
huntd.6
|
||||
141
hunt/huntd/get_names.c
Normal file
141
hunt/huntd/get_names.c
Normal file
@@ -0,0 +1,141 @@
|
||||
/* $NetBSD: get_names.c,v 1.6 2001/02/05 00:42:15 christos Exp $ */
|
||||
/*
|
||||
* Copyright (c) 1983 Regents of the University of California.
|
||||
* All rights reserved. The Berkeley software License Agreement
|
||||
* specifies the terms and conditions for redistribution.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifndef lint
|
||||
__RCSID("$NetBSD: get_names.c,v 1.6 2001/02/05 00:42:15 christos Exp $");
|
||||
#endif /* not lint */
|
||||
|
||||
#include "bsd.h"
|
||||
|
||||
#if defined(TALK_43) || defined(TALK_42)
|
||||
|
||||
# include <sys/param.h>
|
||||
# include <netdb.h>
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
# include "hunt.h"
|
||||
# include "talk_ctl.h"
|
||||
|
||||
#ifndef MAXHOSTNAMELEN
|
||||
#define MAXHOSTNAMELEN 256
|
||||
#endif
|
||||
|
||||
static char hostname[MAXHOSTNAMELEN + 1];
|
||||
char *my_machine_name;
|
||||
|
||||
/*
|
||||
* Determine the local user and machine
|
||||
*/
|
||||
void
|
||||
get_local_name(my_name)
|
||||
const char *my_name;
|
||||
{
|
||||
struct hostent *hp;
|
||||
struct servent *sp;
|
||||
|
||||
/* Load these useful values into the standard message header */
|
||||
msg.id_num = 0;
|
||||
(void) strncpy(msg.l_name, my_name, NAME_SIZE);
|
||||
msg.l_name[NAME_SIZE - 1] = '\0';
|
||||
msg.r_tty[0] = '\0';
|
||||
msg.pid = getpid();
|
||||
# ifdef TALK_43
|
||||
msg.vers = TALK_VERSION;
|
||||
msg.addr.sa_family = htons(AF_INET);
|
||||
msg.ctl_addr.sa_family = htons(AF_INET);
|
||||
# else
|
||||
msg.addr.sin_family = htons(AF_INET);
|
||||
msg.ctl_addr.sin_family = htons(AF_INET);
|
||||
# endif
|
||||
|
||||
(void)gethostname(hostname, sizeof (hostname));
|
||||
hostname[sizeof(hostname) - 1] = '\0';
|
||||
my_machine_name = hostname;
|
||||
/* look up the address of the local host */
|
||||
hp = gethostbyname(my_machine_name);
|
||||
if (hp == (struct hostent *) 0) {
|
||||
# ifdef LOG
|
||||
syslog(LOG_ERR,
|
||||
"This machine doesn't exist. Boy, am I confused!");
|
||||
# else
|
||||
perror("This machine doesn't exist. Boy, am I confused!");
|
||||
# endif
|
||||
exit(1);
|
||||
}
|
||||
memcpy(&my_machine_addr, hp->h_addr, hp->h_length);
|
||||
/* find the daemon portal */
|
||||
# ifdef TALK_43
|
||||
sp = getservbyname("ntalk", "udp");
|
||||
# else
|
||||
sp = getservbyname("talk", "udp");
|
||||
# endif
|
||||
if (sp == 0) {
|
||||
# ifdef LOG
|
||||
syslog(LOG_ERR, "This machine doesn't support talk");
|
||||
# else
|
||||
perror("This machine doesn't support talk");
|
||||
# endif
|
||||
exit(1);
|
||||
}
|
||||
daemon_port = sp->s_port;
|
||||
}
|
||||
|
||||
/*
|
||||
* Determine the remote user and machine
|
||||
*/
|
||||
int
|
||||
get_remote_name(his_address)
|
||||
char *his_address;
|
||||
{
|
||||
char *his_name;
|
||||
char *his_machine_name;
|
||||
char *ptr;
|
||||
struct hostent *hp;
|
||||
|
||||
|
||||
/* check for, and strip out, the machine name of the target */
|
||||
for (ptr = his_address; *ptr != '\0' && *ptr != '@' && *ptr != ':'
|
||||
&& *ptr != '!' && *ptr != '.'; ptr++)
|
||||
continue;
|
||||
if (*ptr == '\0') {
|
||||
/* this is a local to local talk */
|
||||
his_name = his_address;
|
||||
his_machine_name = my_machine_name;
|
||||
} else {
|
||||
if (*ptr == '@') {
|
||||
/* user@host */
|
||||
his_name = his_address;
|
||||
his_machine_name = ptr + 1;
|
||||
} else {
|
||||
/* host.user or host!user or host:user */
|
||||
his_name = ptr + 1;
|
||||
his_machine_name = his_address;
|
||||
}
|
||||
*ptr = '\0';
|
||||
}
|
||||
/* Load these useful values into the standard message header */
|
||||
(void) strncpy(msg.r_name, his_name, NAME_SIZE);
|
||||
msg.r_name[NAME_SIZE - 1] = '\0';
|
||||
|
||||
/* if he is on the same machine, then simply copy */
|
||||
if (memcmp((char *) &his_machine_name, (char *) &my_machine_name,
|
||||
sizeof(his_machine_name)) == 0)
|
||||
memcpy(&his_machine_addr, &my_machine_addr,
|
||||
sizeof(his_machine_name));
|
||||
else {
|
||||
/* look up the address of the recipient's machine */
|
||||
hp = gethostbyname(his_machine_name);
|
||||
if (hp == (struct hostent *) 0)
|
||||
return 0; /* unknown host */
|
||||
memcpy(&his_machine_addr, hp->h_addr, hp->h_length);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user