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