Module Name:    src
Committed By:   christos
Date:           Sat Dec 29 23:44:23 UTC 2012

Modified Files:
        src/usr.bin/talk: get_addrs.c get_names.c invite.c

Log Message:
- use warn/err
- if both users are on the same machine, use the loopback address. This
  allows us to use talkd from inetd listening only to the loopback.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/talk/get_addrs.c
cvs rdiff -u -r1.15 -r1.16 src/usr.bin/talk/get_names.c
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/talk/invite.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/talk/get_addrs.c
diff -u src/usr.bin/talk/get_addrs.c:1.10 src/usr.bin/talk/get_addrs.c:1.11
--- src/usr.bin/talk/get_addrs.c:1.10	Tue Sep  6 14:32:03 2011
+++ src/usr.bin/talk/get_addrs.c	Sat Dec 29 18:44:22 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: get_addrs.c,v 1.10 2011/09/06 18:32:03 joerg Exp $	*/
+/*	$NetBSD: get_addrs.c,v 1.11 2012/12/29 23:44:22 christos Exp $	*/
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)get_addrs.c	8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: get_addrs.c,v 1.10 2011/09/06 18:32:03 joerg Exp $");
+__RCSID("$NetBSD: get_addrs.c,v 1.11 2012/12/29 23:44:22 christos Exp $");
 #endif /* not lint */
 
 #include "talk.h"
@@ -42,6 +42,7 @@ __RCSID("$NetBSD: get_addrs.c,v 1.10 201
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <err.h>
 #include "talk_ctl.h"
 
 void
@@ -51,34 +52,30 @@ get_addrs(const char *my_machine_name, c
 	struct servent *sp;
 
 	msg.pid = htonl(getpid());
-	/* look up the address of the local host */
-	hp = gethostbyname(my_machine_name);
-	if (hp == NULL) {
-		fprintf(stderr, "talk: %s: ", my_machine_name);
-		herror(NULL);
-		exit(1);
-	}
-	memmove((char *)&my_machine_addr, hp->h_addr, hp->h_length);
 	/*
-	 * If the callee is on-machine, just copy the
-	 * network address, otherwise do a lookup...
+	 * If the callee is on-machine, just use loopback
+	 * otherwise do a lookup...
 	 */
-	if (strcmp(his_machine_name, my_machine_name)) {
+	if (strcmp(his_machine_name, my_machine_name) != 0) {
+		/* look up the address of the local host */
+		hp = gethostbyname(my_machine_name);
+		if (hp == NULL)
+			errx(EXIT_FAILURE, "%s: %s", my_machine_name,
+			    hstrerror(h_errno));
+		memcpy(&my_machine_addr, hp->h_addr, sizeof(my_machine_addr));
 		hp = gethostbyname(his_machine_name);
-		if (hp == NULL) {
-			fprintf(stderr, "talk: %s: ", his_machine_name);
-			herror(NULL);
-			exit(1);
-		}
-		memmove((char *) &his_machine_addr, hp->h_addr, hp->h_length);
+		if (hp == NULL)
+			errx(EXIT_FAILURE, "%s: %s", his_machine_name,
+			    hstrerror(h_errno));
+		memcpy(&his_machine_addr, hp->h_addr, sizeof(his_machine_addr));
 	} else
-		his_machine_addr = my_machine_addr;
+		his_machine_addr.s_addr = my_machine_addr.s_addr =
+		    htonl(INADDR_LOOPBACK);
+
 	/* find the server's port */
 	sp = getservbyname("ntalk", "udp");
-	if (sp == 0) {
-		fprintf(stderr, "talk: %s/%s: service is not registered.\n",
+	if (sp == 0)
+		errx(EXIT_FAILURE, "%s/%s: service is not registered.\n",
 		     "ntalk", "udp");
-		exit(1);
-	}
 	daemon_port = sp->s_port;
 }

Index: src/usr.bin/talk/get_names.c
diff -u src/usr.bin/talk/get_names.c:1.15 src/usr.bin/talk/get_names.c:1.16
--- src/usr.bin/talk/get_names.c:1.15	Tue Sep  6 14:32:03 2011
+++ src/usr.bin/talk/get_names.c	Sat Dec 29 18:44:23 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: get_names.c,v 1.15 2011/09/06 18:32:03 joerg Exp $	*/
+/*	$NetBSD: get_names.c,v 1.16 2012/12/29 23:44:23 christos Exp $	*/
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)get_names.c	8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: get_names.c,v 1.15 2011/09/06 18:32:03 joerg Exp $");
+__RCSID("$NetBSD: get_names.c,v 1.16 2012/12/29 23:44:23 christos Exp $");
 #endif /* not lint */
 
 #include "talk.h"
@@ -61,20 +61,17 @@ get_names(int argc, char *argv[])
 	char *names;
 
 	if (argc < 2 ) {
-		printf("usage: talk user [ttyname]\n");
-		exit(1);
-	}
-	if (!isatty(0)) {
-		printf("Standard input must be a tty, not a pipe or a file\n");
+		fprintf(stderr, "Usage: %s user [ttyname]\n", getprogname());
 		exit(1);
 	}
+	if (!isatty(0))
+		errx(EXIT_FAILURE, "Standard input must be a tty, "
+		    "not a pipe or a file");
 	if ((my_name = getlogin()) == NULL) {
 		struct passwd *pw;
 
-		if ((pw = getpwuid(getuid())) == NULL) {
-			printf("You don't exist. Go away.\n");
-			exit(1);
-		}
+		if ((pw = getpwuid(getuid())) == NULL)
+			errx(EXIT_FAILURE, "You don't exist. Go away.");
 		my_name = pw->pw_name;
 	}
 	if ((cp = getenv("TALKHOST")) != NULL)

Index: src/usr.bin/talk/invite.c
diff -u src/usr.bin/talk/invite.c:1.9 src/usr.bin/talk/invite.c:1.10
--- src/usr.bin/talk/invite.c:1.9	Tue Sep  6 14:32:03 2011
+++ src/usr.bin/talk/invite.c	Sat Dec 29 18:44:23 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: invite.c,v 1.9 2011/09/06 18:32:03 joerg Exp $	*/
+/*	$NetBSD: invite.c,v 1.10 2012/12/29 23:44:23 christos Exp $	*/
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)invite.c	8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: invite.c,v 1.9 2011/09/06 18:32:03 joerg Exp $");
+__RCSID("$NetBSD: invite.c,v 1.10 2012/12/29 23:44:23 christos Exp $");
 #endif /* not lint */
 
 #include "talk.h"
@@ -43,6 +43,7 @@ __RCSID("$NetBSD: invite.c,v 1.9 2011/09
 #include <errno.h>
 #include <setjmp.h>
 #include <unistd.h>
+#include <err.h>
 #include "talk_ctl.h"
 
 /*
@@ -179,11 +180,11 @@ send_delete(void)
 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
 	    (struct sockaddr *)&daemon_addr,
 	    sizeof (daemon_addr)) != sizeof(msg))
-		perror("send_delete (remote)");
+		warn("send_delete (remote)");
 	msg.id_num = htonl(local_id);
 	daemon_addr.sin_addr = my_machine_addr;
 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
 	    (struct sockaddr *)&daemon_addr,
 	    sizeof (daemon_addr)) != sizeof (msg))
-		perror("send_delete (local)");
+		warn("send_delete (local)");
 }

Reply via email to