Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/talk - use warn/err



details:   https://anonhg.NetBSD.org/src/rev/a5d2770acb63
branches:  trunk
changeset: 783573:a5d2770acb63
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Dec 29 23:44:22 2012 +0000

description:
- 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.

diffstat:

 usr.bin/talk/get_addrs.c |  45 +++++++++++++++++++++------------------------
 usr.bin/talk/get_names.c |  19 ++++++++-----------
 usr.bin/talk/invite.c    |   9 +++++----
 3 files changed, 34 insertions(+), 39 deletions(-)

diffs (164 lines):

diff -r 4af73ff0eb20 -r a5d2770acb63 usr.bin/talk/get_addrs.c
--- a/usr.bin/talk/get_addrs.c  Sat Dec 29 22:15:07 2012 +0000
+++ b/usr.bin/talk/get_addrs.c  Sat Dec 29 23:44:22 2012 +0000
@@ -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 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <err.h>
 #include "talk_ctl.h"
 
 void
@@ -51,34 +52,30 @@
        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;
 }
diff -r 4af73ff0eb20 -r a5d2770acb63 usr.bin/talk/get_names.c
--- a/usr.bin/talk/get_names.c  Sat Dec 29 22:15:07 2012 +0000
+++ b/usr.bin/talk/get_names.c  Sat Dec 29 23:44:22 2012 +0000
@@ -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 @@
        char *names;
 
        if (argc < 2 ) {
-               printf("usage: talk user [ttyname]\n");
+               fprintf(stderr, "Usage: %s user [ttyname]\n", getprogname());
                exit(1);
        }
-       if (!isatty(0)) {
-               printf("Standard input must be a tty, not a pipe or a file\n");
-               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)
diff -r 4af73ff0eb20 -r a5d2770acb63 usr.bin/talk/invite.c
--- a/usr.bin/talk/invite.c     Sat Dec 29 22:15:07 2012 +0000
+++ b/usr.bin/talk/invite.c     Sat Dec 29 23:44:22 2012 +0000
@@ -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 @@
 #include <errno.h>
 #include <setjmp.h>
 #include <unistd.h>
+#include <err.h>
 #include "talk_ctl.h"
 
 /*
@@ -179,11 +180,11 @@
        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)");
 }



Home | Main Index | Thread Index | Old Index