BSD 4_3_Tahoe release
[unix-history] / usr / src / ucb / talk / get_names.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
f9ac90b4
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
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.
d0aeaf5a
DF
16 */
17
963ce42e 18#ifndef lint
ca67e7b4 19static char sccsid[] = "@(#)get_names.c 5.5 (Berkeley) 6/29/88";
f9ac90b4 20#endif /* not lint */
3e758ab9
MK
21
22#include "talk.h"
4fa2ac2d
KM
23#include <sys/param.h>
24#include <protocols/talkd.h>
82439d4b 25#include <pwd.h>
3e758ab9 26
4fa2ac2d
KM
27char *getlogin();
28char *ttyname();
29char *rindex();
4fa2ac2d 30extern CTL_MSG msg;
963ce42e 31
3e758ab9
MK
32/*
33 * Determine the local and remote user, tty, and machines
34 */
3e758ab9 35get_names(argc, argv)
963ce42e
MK
36 int argc;
37 char *argv[];
3e758ab9 38{
4fa2ac2d
KM
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;
963ce42e
MK
44
45 if (argc < 2 ) {
4fa2ac2d 46 printf("Usage: talk user [ttyname]\n");
963ce42e
MK
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 }
82439d4b
EW
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;
963ce42e
MK
61 }
62 gethostname(hostname, sizeof (hostname));
63 my_machine_name = hostname;
963ce42e 64 /* check for, and strip out, the machine name of the target */
4fa2ac2d 65 for (cp = argv[1]; *cp && !any(*cp, "@:!."); cp++)
963ce42e 66 ;
4fa2ac2d 67 if (*cp == '\0') {
963ce42e
MK
68 /* this is a local to local talk */
69 his_name = argv[1];
70 his_machine_name = my_machine_name;
3e758ab9 71 } else {
4fa2ac2d 72 if (*cp++ == '@') {
963ce42e
MK
73 /* user@host */
74 his_name = argv[1];
4fa2ac2d 75 his_machine_name = cp;
963ce42e
MK
76 } else {
77 /* host.user or host!user or host:user */
4fa2ac2d 78 his_name = cp;
963ce42e
MK
79 his_machine_name = argv[1];
80 }
4fa2ac2d 81 *--cp = '\0';
3e758ab9 82 }
963ce42e
MK
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);
4fa2ac2d
KM
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);
963ce42e
MK
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';
3e758ab9 101}
4fa2ac2d
KM
102
103static
104any(c, cp)
105 register char c, *cp;
106{
107
108 while (*cp)
109 if (c == *cp++)
110 return (1);
111 return (0);
112}