xref: /original-bsd/usr.bin/talk/get_names.c (revision f82e54c4)
1 #ifndef lint
2 static char sccsid[] = "@(#)get_names.c	1.2 (Berkeley) 04/11/84";
3 #endif
4 
5 #include "talk.h"
6 #include "ctl.h"
7 
8 char *getlogin(), *ttyname(), *rindex();
9 
10 extern CTL_MSG msg;
11 
12 struct hostent *gethostbyname();
13 
14 /*
15  * Determine the local and remote user, tty, and machines
16  */
17 get_names(argc, argv)
18 	int argc;
19 	char *argv[];
20 {
21 	char hostname[HOST_NAME_LENGTH];
22 	char *his_name;
23 	char *my_name;
24 	char *my_machine_name;
25 	char *his_machine_name;
26 	char *my_tty;
27 	char *his_tty;
28 	char *ptr;
29 
30 	if (argc < 2 ) {
31 		printf("Usage:	talk user [ttyname]\n");
32 		exit(-1);
33 	}
34 	if (!isatty(0)) {
35 		printf("Standard input must be a tty, not a pipe or a file\n");
36 		exit(-1);
37 	}
38 	my_name = getlogin();
39 	if (my_name == NULL) {
40 		printf("You don't exist. Go away.\n");
41 		exit(-1);
42 	}
43 	gethostname(hostname, sizeof (hostname));
44 	my_machine_name = hostname;
45 	my_tty = rindex(ttyname(0), '/') + 1;
46 	/* check for, and strip out, the machine name of the target */
47 	for (ptr = argv[1]; *ptr != '\0' && *ptr != '@' && *ptr != ':' &&
48 	    *ptr != '!' && *ptr != '.'; ptr++)
49 		;
50 	if (*ptr == '\0') {
51 		/* this is a local to local talk */
52 		his_name = argv[1];
53 		his_machine_name = my_machine_name;
54 	} else {
55 		if (*ptr == '@') {
56 			/* user@host */
57 			his_name = argv[1];
58 			his_machine_name = ptr + 1;
59 		} else {
60 			/* host.user or host!user or host:user */
61 			his_name = ptr + 1;
62 			his_machine_name = argv[1];
63 		}
64 		*ptr = '\0';
65 	}
66 	if (argc > 2)
67 		his_tty = argv[2];	/* tty name is arg 2 */
68 	else
69 		his_tty = "";
70 	get_addrs(my_machine_name, his_machine_name);
71 	/* Load these useful values into the standard message header */
72 	msg.id_num = 0;
73 	strncpy(msg.l_name, my_name, NAME_SIZE);
74 	msg.l_name[NAME_SIZE - 1] = '\0';
75 	strncpy(msg.r_name, his_name, NAME_SIZE);
76 	msg.r_name[NAME_SIZE - 1] = '\0';
77 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
78 	msg.r_tty[TTY_SIZE - 1] = '\0';
79 }
80