xref: /csrg-svn/usr.bin/talk/get_names.c (revision 42770)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)get_names.c	5.8 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include "talk.h"
13 #include <sys/param.h>
14 #include <protocols/talkd.h>
15 #include <pwd.h>
16 
17 char	*getlogin();
18 char	*ttyname();
19 char	*rindex();
20 extern	CTL_MSG msg;
21 
22 /*
23  * Determine the local and remote user, tty, and machines
24  */
25 get_names(argc, argv)
26 	int argc;
27 	char *argv[];
28 {
29 	char hostname[MAXHOSTNAMELEN];
30 	char *his_name, *my_name;
31 	char *my_machine_name, *his_machine_name;
32 	char *my_tty, *his_tty;
33 	register char *cp;
34 
35 	if (argc < 2 ) {
36 		printf("Usage: talk user [ttyname]\n");
37 		exit(-1);
38 	}
39 	if (!isatty(0)) {
40 		printf("Standard input must be a tty, not a pipe or a file\n");
41 		exit(-1);
42 	}
43 	if ((my_name = getlogin()) == NULL) {
44 		struct passwd *pw;
45 
46 		if ((pw = getpwuid(getuid())) == NULL) {
47 			printf("You don't exist. Go away.\n");
48 			exit(-1);
49 		}
50 		my_name = pw->pw_name;
51 	}
52 	gethostname(hostname, sizeof (hostname));
53 	my_machine_name = hostname;
54 	/* check for, and strip out, the machine name of the target */
55 	for (cp = argv[1]; *cp && !any(*cp, "@:!."); cp++)
56 		;
57 	if (*cp == '\0') {
58 		/* this is a local to local talk */
59 		his_name = argv[1];
60 		his_machine_name = my_machine_name;
61 	} else {
62 		if (*cp++ == '@') {
63 			/* user@host */
64 			his_name = argv[1];
65 			his_machine_name = cp;
66 		} else {
67 			/* host.user or host!user or host:user */
68 			his_name = cp;
69 			his_machine_name = argv[1];
70 		}
71 		*--cp = '\0';
72 	}
73 	if (argc > 2)
74 		his_tty = argv[2];	/* tty name is arg 2 */
75 	else
76 		his_tty = "";
77 	get_addrs(my_machine_name, his_machine_name);
78 	/*
79 	 * Initialize the message template.
80 	 */
81 	msg.vers = TALK_VERSION;
82 	msg.addr.sa_family = htons(AF_INET);
83 	msg.ctl_addr.sa_family = htons(AF_INET);
84 	msg.id_num = htonl(0);
85 	strncpy(msg.l_name, my_name, NAME_SIZE);
86 	msg.l_name[NAME_SIZE - 1] = '\0';
87 	strncpy(msg.r_name, his_name, NAME_SIZE);
88 	msg.r_name[NAME_SIZE - 1] = '\0';
89 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
90 	msg.r_tty[TTY_SIZE - 1] = '\0';
91 }
92 
93 static
94 any(c, cp)
95 	register char c, *cp;
96 {
97 
98 	while (*cp)
99 		if (c == *cp++)
100 			return (1);
101 	return (0);
102 }
103