get_names.c revision 32503
1324145Smm/*
2324145Smm * Copyright (c) 1983, 1993
3324145Smm *	The Regents of the University of California.  All rights reserved.
4324145Smm *
5324145Smm * Redistribution and use in source and binary forms, with or without
6324145Smm * modification, are permitted provided that the following conditions
7324145Smm * are met:
8324145Smm * 1. Redistributions of source code must retain the above copyright
9324145Smm *    notice, this list of conditions and the following disclaimer.
10324145Smm * 2. Redistributions in binary form must reproduce the above copyright
11324145Smm *    notice, this list of conditions and the following disclaimer in the
12324145Smm *    documentation and/or other materials provided with the distribution.
13324145Smm * 3. All advertising materials mentioning features or use of this software
14324145Smm *    must display the following acknowledgement:
15324145Smm *	This product includes software developed by the University of
16324145Smm *	California, Berkeley and its contributors.
17324145Smm * 4. Neither the name of the University nor the names of its contributors
18324145Smm *    may be used to endorse or promote products derived from this software
19324145Smm *    without specific prior written permission.
20324145Smm *
21324145Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22324145Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23324145Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24324145Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25324145Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26324145Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27324145Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28324145Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29324145Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30324145Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31324145Smm * SUCH DAMAGE.
32324145Smm */
33324145Smm
34324145Smm#ifndef lint
35324145Smm#if 0
36324145Smmstatic char sccsid[] = "@(#)get_names.c	8.1 (Berkeley) 6/6/93";
37324145Smm#endif
38324145Smmstatic const char rcsid[] =
39324145Smm	"$Id$";
40324145Smm#endif /* not lint */
41324145Smm
42324145Smm#include <err.h>
43324145Smm#include <pwd.h>
44324145Smm#include <string.h>
45324145Smm#include <sys/param.h>
46324145Smm#include "talk.h"
47324145Smm
48324145Smmextern	CTL_MSG msg;
49324145Smm
50324145Smmstatic void
51324145Smmusage(void)
52324145Smm{
53324145Smm	fprintf(stderr, "usage: talk person [ttyname]\n");
54324145Smm	exit(1);
55324145Smm}
56324145Smm
57324145Smm/*
58324145Smm * Determine the local and remote user, tty, and machines
59324145Smm */
60324145Smmvoid
61324145Smmget_names(argc, argv)
62324145Smm	int argc;
63324145Smm	char *argv[];
64324145Smm{
65324145Smm	char hostname[MAXHOSTNAMELEN];
66324145Smm	char *his_name, *my_name;
67324145Smm	char *my_machine_name, *his_machine_name;
68324145Smm	char *my_tty, *his_tty;
69324145Smm	register char *cp;
70324145Smm
71324145Smm	if (argc < 2 )
72324145Smm		usage();
73324145Smm	if (!isatty(0))
74324145Smm		errx(1, "standard input must be a tty, not a pipe or a file");
75324145Smm	if ((my_name = getlogin()) == NULL) {
76324145Smm		struct passwd *pw;
77324145Smm
78324145Smm		if ((pw = getpwuid(getuid())) == NULL)
79324145Smm			errx(1, "you don't exist. Go away");
80324145Smm		my_name = pw->pw_name;
81324145Smm	}
82324145Smm	gethostname(hostname, sizeof (hostname));
83324145Smm	my_machine_name = hostname;
84324145Smm	/* check for, and strip out, the machine name of the target */
85324145Smm	for (cp = argv[1]; *cp && !index("@:!", *cp); cp++)
86324145Smm		;
87324145Smm	if (*cp == '\0') {
88324145Smm		/* this is a local to local talk */
89324145Smm		his_name = argv[1];
90324145Smm		his_machine_name = my_machine_name;
91324145Smm	} else {
92324145Smm		if (*cp++ == '@') {
93324145Smm			/* user@host */
94324145Smm			his_name = argv[1];
95324145Smm			his_machine_name = cp;
96324145Smm		} else {
97324145Smm			/* host!user or host:user */
98324145Smm			his_name = cp;
99324145Smm			his_machine_name = argv[1];
100324145Smm		}
101324145Smm		*--cp = '\0';
102324145Smm	}
103324145Smm	if (argc > 2)
104324145Smm		his_tty = argv[2];	/* tty name is arg 2 */
105324145Smm	else
106324145Smm		his_tty = "";
107324145Smm	get_addrs(my_machine_name, his_machine_name);
108324145Smm	/*
109324145Smm	 * Initialize the message template.
110324145Smm	 */
111324145Smm	msg.vers = TALK_VERSION;
112324145Smm	msg.addr.sa_family = htons(AF_INET);
113324145Smm	msg.ctl_addr.sa_family = htons(AF_INET);
114324145Smm	msg.id_num = htonl(0);
115324145Smm	strncpy(msg.l_name, my_name, NAME_SIZE);
116324145Smm	msg.l_name[NAME_SIZE - 1] = '\0';
117324145Smm	strncpy(msg.r_name, his_name, NAME_SIZE);
118324145Smm	msg.r_name[NAME_SIZE - 1] = '\0';
119324145Smm	strncpy(msg.r_tty, his_tty, TTY_SIZE);
120324145Smm	msg.r_tty[TTY_SIZE - 1] = '\0';
121324145Smm}
122362133Smm