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