use standard headers to get declarations; strdup is now in libc
[unix-history] / usr / src / usr.bin / 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 *
cb956e54 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
963ce42e 8#ifndef lint
cb956e54 9static char sccsid[] = "@(#)get_names.c 5.8 (Berkeley) %G%";
f9ac90b4 10#endif /* not lint */
3e758ab9
MK
11
12#include "talk.h"
4fa2ac2d
KM
13#include <sys/param.h>
14#include <protocols/talkd.h>
82439d4b 15#include <pwd.h>
3e758ab9 16
4fa2ac2d
KM
17char *getlogin();
18char *ttyname();
19char *rindex();
4fa2ac2d 20extern CTL_MSG msg;
963ce42e 21
3e758ab9
MK
22/*
23 * Determine the local and remote user, tty, and machines
24 */
3e758ab9 25get_names(argc, argv)
963ce42e
MK
26 int argc;
27 char *argv[];
3e758ab9 28{
4fa2ac2d
KM
29 char hostname[MAXHOSTNAMELEN];
30 char *his_name, *my_name;
31 char *my_machine_name, *his_machine_name;
32 char *my_tty, *his_tty;
33 register char *cp;
963ce42e
MK
34
35 if (argc < 2 ) {
4fa2ac2d 36 printf("Usage: talk user [ttyname]\n");
963ce42e
MK
37 exit(-1);
38 }
39 if (!isatty(0)) {
40 printf("Standard input must be a tty, not a pipe or a file\n");
41 exit(-1);
42 }
82439d4b
EW
43 if ((my_name = getlogin()) == NULL) {
44 struct passwd *pw;
45
46 if ((pw = getpwuid(getuid())) == NULL) {
47 printf("You don't exist. Go away.\n");
48 exit(-1);
49 }
50 my_name = pw->pw_name;
963ce42e
MK
51 }
52 gethostname(hostname, sizeof (hostname));
53 my_machine_name = hostname;
963ce42e 54 /* check for, and strip out, the machine name of the target */
4fa2ac2d 55 for (cp = argv[1]; *cp && !any(*cp, "@:!."); cp++)
963ce42e 56 ;
4fa2ac2d 57 if (*cp == '\0') {
963ce42e
MK
58 /* this is a local to local talk */
59 his_name = argv[1];
60 his_machine_name = my_machine_name;
3e758ab9 61 } else {
4fa2ac2d 62 if (*cp++ == '@') {
963ce42e
MK
63 /* user@host */
64 his_name = argv[1];
4fa2ac2d 65 his_machine_name = cp;
963ce42e
MK
66 } else {
67 /* host.user or host!user or host:user */
4fa2ac2d 68 his_name = cp;
963ce42e
MK
69 his_machine_name = argv[1];
70 }
4fa2ac2d 71 *--cp = '\0';
3e758ab9 72 }
963ce42e
MK
73 if (argc > 2)
74 his_tty = argv[2]; /* tty name is arg 2 */
75 else
76 his_tty = "";
77 get_addrs(my_machine_name, his_machine_name);
4fa2ac2d
KM
78 /*
79 * Initialize the message template.
80 */
81 msg.vers = TALK_VERSION;
82 msg.addr.sa_family = htons(AF_INET);
83 msg.ctl_addr.sa_family = htons(AF_INET);
84 msg.id_num = htonl(0);
963ce42e
MK
85 strncpy(msg.l_name, my_name, NAME_SIZE);
86 msg.l_name[NAME_SIZE - 1] = '\0';
87 strncpy(msg.r_name, his_name, NAME_SIZE);
88 msg.r_name[NAME_SIZE - 1] = '\0';
89 strncpy(msg.r_tty, his_tty, TTY_SIZE);
90 msg.r_tty[TTY_SIZE - 1] = '\0';
3e758ab9 91}
4fa2ac2d
KM
92
93static
94any(c, cp)
95 register char c, *cp;
96{
97
98 while (*cp)
99 if (c == *cp++)
100 return (1);
101 return (0);
102}