]> git.draconx.ca Git - rarpd-dx.git/blobdiff - src/rarpd.c
Stop using GNU "error" functions.
[rarpd-dx.git] / src / rarpd.c
index 76b92e5585ad326e65309caf1ad29c5933d2857d..2374fb096ed665e0d3e735bab92197a57f46624d 100644 (file)
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 
-#if HAVE_ERROR_H
-#include <error.h>
-#endif
-
 int do_reload = 1;
 
 int debug;
@@ -48,8 +44,9 @@ int listen_arp;
 char *ifname;
 char *tftp_dir = "/etc/tftpboot";
 
+static const char *progname = "rarpd";
+
 extern int ether_ntohost(char *name, unsigned char *ea);
-void usage(void) __attribute__((noreturn));
 
 struct iflink
 {
@@ -80,22 +77,30 @@ struct rarp_map
        uint32_t                ipaddr;
 } *rarp_db;
 
-void usage(void)
+static void print_usage(FILE *f)
+{
+       fprintf(f, "Usage: %s [options] [interface]\n", progname);
+       if (f != stdout)
+               fprintf(f, "Try %s -H for more information.\n", progname);
+}
+
+static void print_help(void)
 {
-       fprintf(stderr,
-               "\nUsage:\n"
-               "  rarpd [options] [interface]\n"
-               "\nOptions:\n"
-               "  -A        listen also arp messages\n"
-               "  -a        listen on all the interfaces\n"
-               "  -b <dir>  tftpd boot directory\n"
-               "  -d        debug mode\n"
-               "  -e        /etc/ethers markup alone is fine\n"
-               "  -v        verbose mode\n"
-               "  -V        print version and exit\n"
-               "\nFor more details see rarpd(8).\n"
-       );
-       exit(1);
+       print_usage(stdout);
+       putchar('\n');
+
+       puts("Options:");
+       puts("  -A        listen also arp messages");
+       puts("  -a        listen on all the interfaces");
+       puts("  -b <dir>  tftpd boot directory");
+       puts("  -d        debug mode");
+       puts("  -e        /etc/ethers markup alone is fine");
+       puts("  -v        verbose mode");
+       puts("  -V        print version and then exit");
+       puts("  -H        print this message and then exit");
+       putchar('\n');
+
+       puts("For more information, see the rarpd(8) man page.");
 }
 
 static void print_version(void)
@@ -588,8 +593,11 @@ int main(int argc, char **argv)
        int psize;
        int opt;
 
+       if (argc > 0)
+               progname = argv[0];
+
        opterr = 0;
-       while ((opt = getopt(argc, argv, "aAb:dvoeV")) != EOF) {
+       while ((opt = getopt(argc, argv, "aAb:dvoeVH")) != EOF) {
                switch (opt) {
                case 'a':
                        ++all_ifaces;
@@ -621,13 +629,19 @@ int main(int argc, char **argv)
                case 'V':
                        print_version();
                        return 0;
+               case 'H':
+                       print_help();
+                       return 0;
                default:
-                       usage();
+                       print_usage(stderr);
+                       return EXIT_FAILURE;
                }
        }
        if (argc > optind) {
-               if (argc > optind+1)
-                       usage();
+               if (argc > optind+1) {
+                       print_usage(stderr);
+                       return EXIT_FAILURE;
+               }
                ifname = argv[optind];
        }
 
@@ -639,8 +653,10 @@ int main(int argc, char **argv)
                memset(&ifr, 0, sizeof(ifr));
                strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
                if (ioctl(pset[0].fd, SIOCGIFINDEX, &ifr)) {
-                       error(0, errno, "ioctl(SIOCGIFINDEX)");
-                       usage();
+                       fprintf(stderr, "%s: %s: ioctl(SIOCGIFINDEX): %s\n",
+                                       progname, ifname, strerror(errno));
+                       print_usage(stderr);
+                       return EXIT_FAILURE;
                }
                ifidx = ifr.ifr_ifindex;
        }
@@ -681,12 +697,17 @@ int main(int argc, char **argv)
                pset[0] = pset[1];
                psize--;
        }
-       if (psize == 0)
-               error(1, errno, "failed to bind any socket");
+       if (psize == 0) {
+               fprintf(stderr, "%s: error: failed to bind any socket\n", progname);
+               return EXIT_FAILURE;
+       }
 
        if (!debug) {
-               if (daemon(0, 0) < 0)
-                       error(1, errno, "failed to daemon()");
+               if (daemon(0, 0) < 0) {
+                       fprintf(stderr, "%s: error: daemon() failed: %s\n",
+                                       progname, strerror(errno));
+                       return EXIT_FAILURE;
+               }
        }
 
        openlog("rarpd", LOG_PID | LOG_CONS, LOG_DAEMON);