xref: /original-bsd/usr.bin/talk/get_names.c (revision 208c3823)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)get_names.c	5.7 (Berkeley) 08/16/89";
20 #endif /* not lint */
21 
22 #include "talk.h"
23 #include <sys/param.h>
24 #include <protocols/talkd.h>
25 #include <pwd.h>
26 
27 char	*getlogin();
28 char	*ttyname();
29 char	*rindex();
30 extern	CTL_MSG msg;
31 
32 /*
33  * Determine the local and remote user, tty, and machines
34  */
35 get_names(argc, argv)
36 	int argc;
37 	char *argv[];
38 {
39 	char hostname[MAXHOSTNAMELEN];
40 	char *his_name, *my_name;
41 	char *my_machine_name, *his_machine_name;
42 	char *my_tty, *his_tty;
43 	register char *cp;
44 
45 	if (argc < 2 ) {
46 		printf("Usage: talk user [ttyname]\n");
47 		exit(-1);
48 	}
49 	if (!isatty(0)) {
50 		printf("Standard input must be a tty, not a pipe or a file\n");
51 		exit(-1);
52 	}
53 	if ((my_name = getlogin()) == NULL) {
54 		struct passwd *pw;
55 
56 		if ((pw = getpwuid(getuid())) == NULL) {
57 			printf("You don't exist. Go away.\n");
58 			exit(-1);
59 		}
60 		my_name = pw->pw_name;
61 	}
62 	gethostname(hostname, sizeof (hostname));
63 	my_machine_name = hostname;
64 	/* check for, and strip out, the machine name of the target */
65 	for (cp = argv[1]; *cp && !any(*cp, "@:!."); cp++)
66 		;
67 	if (*cp == '\0') {
68 		/* this is a local to local talk */
69 		his_name = argv[1];
70 		his_machine_name = my_machine_name;
71 	} else {
72 		if (*cp++ == '@') {
73 			/* user@host */
74 			his_name = argv[1];
75 			his_machine_name = cp;
76 		} else {
77 			/* host.user or host!user or host:user */
78 			his_name = cp;
79 			his_machine_name = argv[1];
80 		}
81 		*--cp = '\0';
82 	}
83 	if (argc > 2)
84 		his_tty = argv[2];	/* tty name is arg 2 */
85 	else
86 		his_tty = "";
87 	get_addrs(my_machine_name, his_machine_name);
88 	/*
89 	 * Initialize the message template.
90 	 */
91 	msg.vers = TALK_VERSION;
92 	msg.addr.sa_family = htons(AF_INET);
93 	msg.ctl_addr.sa_family = htons(AF_INET);
94 	msg.id_num = htonl(0);
95 	strncpy(msg.l_name, my_name, NAME_SIZE);
96 	msg.l_name[NAME_SIZE - 1] = '\0';
97 	strncpy(msg.r_name, his_name, NAME_SIZE);
98 	msg.r_name[NAME_SIZE - 1] = '\0';
99 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
100 	msg.r_tty[TTY_SIZE - 1] = '\0';
101 }
102 
103 static
104 any(c, cp)
105 	register char c, *cp;
106 {
107 
108 	while (*cp)
109 		if (c == *cp++)
110 			return (1);
111 	return (0);
112 }
113