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