xref: /freebsd/usr.bin/talk/get_names.c (revision 3daadfc83361cf4f31017a7fed5a5910cd8c4ce9)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
34caa64801SMark Murray #include <sys/cdefs.h>
35caa64801SMark Murray 
36caa64801SMark Murray __FBSDID("$FreeBSD$");
37caa64801SMark Murray 
389b50d902SRodney W. Grimes #ifndef lint
39caa64801SMark Murray static const char sccsid[] = "@(#)get_names.c	8.1 (Berkeley) 6/6/93";
4020868061SPhilippe Charnier #endif
41caa64801SMark Murray 
42caa64801SMark Murray #include <sys/param.h>
439b50d902SRodney W. Grimes 
4420868061SPhilippe Charnier #include <err.h>
4520868061SPhilippe Charnier #include <pwd.h>
469582ea7eSDima Dorfman #include <stdlib.h>
4728592604SJoerg Wunsch #include <string.h>
483daadfc8SXin LI #include <unistd.h>
49caa64801SMark Murray 
509b50d902SRodney W. Grimes #include "talk.h"
519b50d902SRodney W. Grimes 
529b50d902SRodney W. Grimes extern	CTL_MSG msg;
539b50d902SRodney W. Grimes 
5420868061SPhilippe Charnier static void
5520868061SPhilippe Charnier usage(void)
5620868061SPhilippe Charnier {
5720868061SPhilippe Charnier 	fprintf(stderr, "usage: talk person [ttyname]\n");
5820868061SPhilippe Charnier 	exit(1);
5920868061SPhilippe Charnier }
6020868061SPhilippe Charnier 
619b50d902SRodney W. Grimes /*
629b50d902SRodney W. Grimes  * Determine the local and remote user, tty, and machines
639b50d902SRodney W. Grimes  */
6428592604SJoerg Wunsch void
65b6d9e1f3SXin LI get_names(int argc, char *argv[])
669b50d902SRodney W. Grimes {
679b50d902SRodney W. Grimes 	char hostname[MAXHOSTNAMELEN];
689b50d902SRodney W. Grimes 	char *his_name, *my_name;
69b6d9e1f3SXin LI 	const char *my_machine_name, *his_machine_name;
70caa64801SMark Murray 	const char *his_tty;
71caa64801SMark Murray 	char *cp;
729b50d902SRodney W. Grimes 
7320868061SPhilippe Charnier 	if (argc < 2 )
7420868061SPhilippe Charnier 		usage();
7520868061SPhilippe Charnier 	if (!isatty(0))
7620868061SPhilippe Charnier 		errx(1, "standard input must be a tty, not a pipe or a file");
779b50d902SRodney W. Grimes 	if ((my_name = getlogin()) == NULL) {
789b50d902SRodney W. Grimes 		struct passwd *pw;
799b50d902SRodney W. Grimes 
8020868061SPhilippe Charnier 		if ((pw = getpwuid(getuid())) == NULL)
8120868061SPhilippe Charnier 			errx(1, "you don't exist. Go away");
829b50d902SRodney W. Grimes 		my_name = pw->pw_name;
839b50d902SRodney W. Grimes 	}
849b50d902SRodney W. Grimes 	gethostname(hostname, sizeof (hostname));
859b50d902SRodney W. Grimes 	my_machine_name = hostname;
869b50d902SRodney W. Grimes 	/* check for, and strip out, the machine name of the target */
87c6189102SAndrey A. Chernov 	for (cp = argv[1]; *cp && !index("@:!", *cp); cp++)
889b50d902SRodney W. Grimes 		;
899b50d902SRodney W. Grimes 	if (*cp == '\0') {
909b50d902SRodney W. Grimes 		/* this is a local to local talk */
919b50d902SRodney W. Grimes 		his_name = argv[1];
929d61853dSBruce M Simpson 		my_machine_name = his_machine_name = "localhost";
939b50d902SRodney W. Grimes 	} else {
94c6189102SAndrey A. Chernov 		if (*cp++ == '@') {
959b50d902SRodney W. Grimes 			/* user@host */
969b50d902SRodney W. Grimes 			his_name = argv[1];
97c6189102SAndrey A. Chernov 			his_machine_name = cp;
98c6189102SAndrey A. Chernov 		} else {
99c6189102SAndrey A. Chernov 			/* host!user or host:user */
100c6189102SAndrey A. Chernov 			his_name = cp;
101c6189102SAndrey A. Chernov 			his_machine_name = argv[1];
102c6189102SAndrey A. Chernov 		}
103c6189102SAndrey A. Chernov 		*--cp = '\0';
1049b50d902SRodney W. Grimes 	}
1059b50d902SRodney W. Grimes 	if (argc > 2)
1069b50d902SRodney W. Grimes 		his_tty = argv[2];	/* tty name is arg 2 */
1079b50d902SRodney W. Grimes 	else
1089b50d902SRodney W. Grimes 		his_tty = "";
1099b50d902SRodney W. Grimes 	get_addrs(my_machine_name, his_machine_name);
1109b50d902SRodney W. Grimes 	/*
1119b50d902SRodney W. Grimes 	 * Initialize the message template.
1129b50d902SRodney W. Grimes 	 */
1139b50d902SRodney W. Grimes 	msg.vers = TALK_VERSION;
1149b50d902SRodney W. Grimes 	msg.addr.sa_family = htons(AF_INET);
1159b50d902SRodney W. Grimes 	msg.ctl_addr.sa_family = htons(AF_INET);
1169b50d902SRodney W. Grimes 	msg.id_num = htonl(0);
1171c8ee5f2SXin LI 	strlcpy(msg.l_name, my_name, NAME_SIZE);
1181c8ee5f2SXin LI 	strlcpy(msg.r_name, his_name, NAME_SIZE);
1191c8ee5f2SXin LI 	strlcpy(msg.r_tty, his_tty, TTY_SIZE);
1209b50d902SRodney W. Grimes }
121