11590Srgrimes/*
21590Srgrimes * Copyright (c) 1983, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
3087710Smarkm#include <sys/cdefs.h>
3187710Smarkm
3287710Smarkm__FBSDID("$FreeBSD$");
3387710Smarkm
341590Srgrimes#ifndef lint
3587710Smarkmstatic const char sccsid[] = "@(#)get_names.c	8.1 (Berkeley) 6/6/93";
3632503Scharnier#endif
371590Srgrimes
3887710Smarkm#include <sys/param.h>
3987710Smarkm
4032503Scharnier#include <err.h>
4132503Scharnier#include <pwd.h>
4278733Sdd#include <stdlib.h>
4314443Sjoerg#include <string.h>
44200418Sdelphij#include <unistd.h>
4587710Smarkm
461590Srgrimes#include "talk.h"
471590Srgrimes
481590Srgrimesextern	CTL_MSG msg;
491590Srgrimes
5032503Scharnierstatic void
5132503Scharnierusage(void)
5232503Scharnier{
5332503Scharnier	fprintf(stderr, "usage: talk person [ttyname]\n");
5432503Scharnier	exit(1);
5532503Scharnier}
5632503Scharnier
571590Srgrimes/*
581590Srgrimes * Determine the local and remote user, tty, and machines
591590Srgrimes */
6014443Sjoergvoid
61178642Sdelphijget_names(int argc, char *argv[])
621590Srgrimes{
631590Srgrimes	char hostname[MAXHOSTNAMELEN];
641590Srgrimes	char *his_name, *my_name;
65178642Sdelphij	const char *my_machine_name, *his_machine_name;
6687710Smarkm	const char *his_tty;
6787710Smarkm	char *cp;
681590Srgrimes
6932503Scharnier	if (argc < 2 )
7032503Scharnier		usage();
7132503Scharnier	if (!isatty(0))
7232503Scharnier		errx(1, "standard input must be a tty, not a pipe or a file");
731590Srgrimes	if ((my_name = getlogin()) == NULL) {
741590Srgrimes		struct passwd *pw;
751590Srgrimes
7632503Scharnier		if ((pw = getpwuid(getuid())) == NULL)
7732503Scharnier			errx(1, "you don't exist. Go away");
781590Srgrimes		my_name = pw->pw_name;
791590Srgrimes	}
801590Srgrimes	gethostname(hostname, sizeof (hostname));
811590Srgrimes	my_machine_name = hostname;
821590Srgrimes	/* check for, and strip out, the machine name of the target */
83229386Sed	cp = argv[1] + strcspn(argv[1], "@:!");
841590Srgrimes	if (*cp == '\0') {
851590Srgrimes		/* this is a local to local talk */
861590Srgrimes		his_name = argv[1];
87130494Sbms		my_machine_name = his_machine_name = "localhost";
881590Srgrimes	} else {
893829Sache		if (*cp++ == '@') {
901590Srgrimes			/* user@host */
911590Srgrimes			his_name = argv[1];
923829Sache			his_machine_name = cp;
933829Sache		} else {
943829Sache			/* host!user or host:user */
953829Sache			his_name = cp;
963829Sache			his_machine_name = argv[1];
973829Sache		}
983829Sache		*--cp = '\0';
991590Srgrimes	}
1001590Srgrimes	if (argc > 2)
1011590Srgrimes		his_tty = argv[2];	/* tty name is arg 2 */
1021590Srgrimes	else
1031590Srgrimes		his_tty = "";
1041590Srgrimes	get_addrs(my_machine_name, his_machine_name);
1051590Srgrimes	/*
1061590Srgrimes	 * Initialize the message template.
1071590Srgrimes	 */
1081590Srgrimes	msg.vers = TALK_VERSION;
1091590Srgrimes	msg.addr.sa_family = htons(AF_INET);
1101590Srgrimes	msg.ctl_addr.sa_family = htons(AF_INET);
1111590Srgrimes	msg.id_num = htonl(0);
112188886Sdelphij	strlcpy(msg.l_name, my_name, NAME_SIZE);
113188886Sdelphij	strlcpy(msg.r_name, his_name, NAME_SIZE);
114188886Sdelphij	strlcpy(msg.r_tty, his_tty, TTY_SIZE);
1151590Srgrimes}
116