xref: /original-bsd/usr.bin/talk/get_names.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983, 1993
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	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #include <protocols/talkd.h>
15 #include <pwd.h>
16 #include "talk.h"
17 
18 char	*getlogin();
19 char	*ttyname();
20 char	*rindex();
21 extern	CTL_MSG msg;
22 
23 /*
24  * Determine the local and remote user, tty, and machines
25  */
26 get_names(argc, argv)
27 	int argc;
28 	char *argv[];
29 {
30 	char hostname[MAXHOSTNAMELEN];
31 	char *his_name, *my_name;
32 	char *my_machine_name, *his_machine_name;
33 	char *my_tty, *his_tty;
34 	register char *cp;
35 
36 	if (argc < 2 ) {
37 		printf("Usage: talk user [ttyname]\n");
38 		exit(-1);
39 	}
40 	if (!isatty(0)) {
41 		printf("Standard input must be a tty, not a pipe or a file\n");
42 		exit(-1);
43 	}
44 	if ((my_name = getlogin()) == NULL) {
45 		struct passwd *pw;
46 
47 		if ((pw = getpwuid(getuid())) == NULL) {
48 			printf("You don't exist. Go away.\n");
49 			exit(-1);
50 		}
51 		my_name = pw->pw_name;
52 	}
53 	gethostname(hostname, sizeof (hostname));
54 	my_machine_name = hostname;
55 	/* check for, and strip out, the machine name of the target */
56 	for (cp = argv[1]; *cp && !index("@:!.", *cp); cp++)
57 		;
58 	if (*cp == '\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 (*cp++ == '@') {
64 			/* user@host */
65 			his_name = argv[1];
66 			his_machine_name = cp;
67 		} else {
68 			/* host.user or host!user or host:user */
69 			his_name = cp;
70 			his_machine_name = argv[1];
71 		}
72 		*--cp = '\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 	/*
80 	 * Initialize the message template.
81 	 */
82 	msg.vers = TALK_VERSION;
83 	msg.addr.sa_family = htons(AF_INET);
84 	msg.ctl_addr.sa_family = htons(AF_INET);
85 	msg.id_num = htonl(0);
86 	strncpy(msg.l_name, my_name, NAME_SIZE);
87 	msg.l_name[NAME_SIZE - 1] = '\0';
88 	strncpy(msg.r_name, his_name, NAME_SIZE);
89 	msg.r_name[NAME_SIZE - 1] = '\0';
90 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
91 	msg.r_tty[TTY_SIZE - 1] = '\0';
92 }
93