Debian Codegraph @

talk/get_names.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1983, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <protocols/talkd.h>
58 #include <pwd.h>
59 #include <libinetutils.h>
60 #include <unistd.h>
61 #include "talk.h"
62
63 char *getlogin (void);
64 char *ttyname (int);
65 extern CTL_MSG msg;
66
67 /*
68 * Determine the local and remote user, tty, and machines
69 */
70 int
71 get_names (int argc, char *argv[])
72 {
73 char *his_name, *my_name;
74 char *my_machine_name, *his_machine_name;
75 char *his_tty;
76 char *cp;
77
78 if ((my_name = getlogin ()) == NULL)
79 {
80 struct passwd *pw;
81
82 if ((pw = getpwuid (getuid ())) == NULL)
83 {
84 printf ("You don't exist. Go away.\n");
85 exit (-1);
86 }
87 my_name = pw->pw_name;
88 }
89
90 my_machine_name = localhost ();
91 if (!my_machine_name)
92 {
93 perror ("Cannot get local hostname");
94 exit (-1);
95 }
96
97 /* check for, and strip out, the machine name of the target */
98 for (cp = argv[0]; *cp && !strchr ("@:!.", *cp); cp++)
99 ;
100 if (*cp == '\0')
101 {
102 /* this is a local to local talk */
103 his_name = argv[0];
104 his_machine_name = my_machine_name;
105 }
106 else
107 {
108 if (*cp++ == '@')
109 {
110 /* user@host */
111 his_name = argv[0];
112 his_machine_name = cp;
113 }
114 else
115 {
116 /* host.user or host!user or host:user */
117 his_name = cp;
118 his_machine_name = argv[0];
119 }
120 *--cp = '\0';
121 }
122 if (argc > 1)
123 his_tty = argv[1]; /* tty name is arg 2 */
124 else
125 his_tty = "";
126 get_addrs (my_machine_name, his_machine_name);
127 /*
128 * Initialize the message template.
129 */
130 msg.vers = TALK_VERSION;
131 msg.addr.sa_family = htons (AF_INET);
132 msg.ctl_addr.sa_family = htons (AF_INET);
133 msg.id_num = htonl (0);
134 strncpy (msg.l_name, my_name, NAME_SIZE);
135 msg.l_name[NAME_SIZE - 1] = '\0';
136 strncpy (msg.r_name, his_name, NAME_SIZE);
137 msg.r_name[NAME_SIZE - 1] = '\0';
138 strncpy (msg.r_tty, his_tty, TTY_SIZE);
139 msg.r_tty[TTY_SIZE - 1] = '\0';
140
141 free (my_machine_name);
142
143 return 0;
144 }