From c8285d9b313ea4ffa78262b5c278e1643befa298 Mon Sep 17 00:00:00 2001
From: Richard Nyberg <rnyberg@murmeldjur.se>
Date: Mon, 12 Jan 2009 22:14:10 +0100
Subject: [PATCH] Don't use the bsd err and warn family of functions. Solaris
 doesn't have them.

---
 cli/add.c    |  8 ++++----
 cli/btcli.c  | 27 +++++++++++++++++++++------
 cli/btcli.h  |  3 ++-
 cli/btinfo.c |  9 ++++++---
 cli/kill.c   |  2 +-
 cli/list.c   |  4 ++--
 cli/stat.c   |  2 +-
 cli/stop.c   |  2 +-
 8 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/cli/add.c b/cli/add.c
index c91bf41..7af08af 100644
--- a/cli/add.c
+++ b/cli/add.c
@@ -54,7 +54,7 @@ cmd_add(int argc, char **argv)
         case 'd':
             dir = optarg;
             if ((dirlen = strlen(dir)) == 0)
-                errx(1, "bad option value for -d");
+                diemsg("bad option value for -d.\n");
             break;
         case 'n':
             name = optarg;
@@ -77,7 +77,7 @@ cmd_add(int argc, char **argv)
     struct iobuf iob;
 
     if ((mi = mi_load(argv[0], &mi_size)) == NULL)
-        err(1, "error loading '%s'", argv[0]);
+        diemsg("error loading '%s' (%s).\n", argv[0], strerror(errno));
 
     iob = iobuf_init(PATH_MAX);
     iobuf_write(&iob, dir, dirlen);
@@ -90,7 +90,7 @@ cmd_add(int argc, char **argv)
     }
     iobuf_swrite(&iob, "\0");
     if ((errno = make_abs_path(iob.buf, dpath)) != 0)
-        err(1, "make_abs_path '%s'", dpath);
+        diemsg("make_abs_path '%s' failed (%s).\n", dpath, strerror(errno));
     code = btpd_add(ipc, mi, mi_size, dpath, name);
     if (code == 0 && start) {
         struct ipc_torrent tspec;
@@ -99,6 +99,6 @@ cmd_add(int argc, char **argv)
         code = btpd_start(ipc, &tspec);
     }
     if (code != IPC_OK)
-        errx(1, "%s", ipc_strerror(code));
+        diemsg("command failed (%s).\n", ipc_strerror(code));
     return;
 }
diff --git a/cli/btcli.c b/cli/btcli.c
index 04822b3..0aaab12 100644
--- a/cli/btcli.c
+++ b/cli/btcli.c
@@ -1,13 +1,26 @@
+#include <stdarg.h>
+
 #include "btcli.h"
 
 const char *btpd_dir;
 struct ipc *ipc;
 
+void
+diemsg(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
+    exit(1);
+}
+
 void
 btpd_connect(void)
 {
     if ((errno = ipc_open(btpd_dir, &ipc)) != 0)
-        err(1, "cannot open connection to btpd in %s", btpd_dir);
+        diemsg("cannot open connection to btpd in %s (%s).\n", btpd_dir,
+            strerror(errno));
 }
 
 enum ipc_err
@@ -17,9 +30,10 @@ handle_ipc_res(enum ipc_err code, const char *cmd, const char *target)
     case IPC_OK:
         break;
     case IPC_COMMERR:
-        errx(1, "%s", ipc_strerror(code));
+        diemsg("error in communication with btpd.\n");
     default:
-        warnx("%s '%s': %s", cmd, target, ipc_strerror(code));
+        fprintf(stderr, "btcli %s '%s': %s.\n", cmd, target,
+            ipc_strerror(code));
     }
     return code;
 }
@@ -68,7 +82,7 @@ tstate_char(enum ipc_tstate ts)
     case IPC_TSTATE_SEED:
         return 'S';
     }
-    errx(1, "bad state");
+    diemsg("unrecognized torrent state.\n");
 }
 
 int
@@ -81,7 +95,8 @@ torrent_spec(char *arg, struct ipc_torrent *tp)
         return 1;
     }
     if ((p = mi_load(arg, NULL)) == NULL) {
-        warnx("bad torrent '%s' (%s)", arg, strerror(errno));
+        fprintf(stderr, "btcli: bad torrent '%s' (%s).'n", arg,
+            strerror(errno));
         return 0;
     }
     tp->by_hash = 1;
@@ -170,7 +185,7 @@ main(int argc, char **argv)
 
     if (btpd_dir == NULL)
         if ((btpd_dir = find_btpd_dir()) == NULL)
-            errx(1, "cannot find the btpd directory");
+            diemsg("cannot find the btpd directory.\n");
 
     optind = 0;
     int found = 0;
diff --git a/cli/btcli.h b/cli/btcli.h
index 17c1ac1..19d360a 100644
--- a/cli/btcli.h
+++ b/cli/btcli.h
@@ -1,7 +1,6 @@
 #ifndef BTCLI_H
 #define BTCLI_H
 
-#include <err.h>
 #include <errno.h>
 #include <getopt.h>
 #include <inttypes.h>
@@ -23,6 +22,8 @@
 extern const char *btpd_dir;
 extern struct ipc *ipc;
 
+__attribute__((noreturn))
+void diemsg(const char *fmt, ...);
 void btpd_connect(void);
 enum ipc_err handle_ipc_res(enum ipc_err err, const char *cmd,
     const char *target);
diff --git a/cli/btinfo.c b/cli/btinfo.c
index 8c32475..dfcddcb 100644
--- a/cli/btinfo.c
+++ b/cli/btinfo.c
@@ -1,11 +1,11 @@
 #include <sys/types.h>
 
-#include <err.h>
 #include <errno.h>
 #include <getopt.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 
 #include "metainfo.h"
@@ -75,8 +75,11 @@ main(int argc, char **argv)
     while (argc > 0) {
         char *mi = NULL;
 
-        if ((mi = mi_load(*argv, NULL)) == NULL)
-            err(1, "mi_load: %s", *argv);
+        if ((mi = mi_load(*argv, NULL)) == NULL) {
+            fprintf(stderr, "failed to load torrent file '%s' (%s).\n",
+                *argv, strerror(errno));
+            exit(1);
+        }
 
         print_metainfo(mi);
         free(mi);
diff --git a/cli/kill.c b/cli/kill.c
index e7a6fa7..9ae57f1 100644
--- a/cli/kill.c
+++ b/cli/kill.c
@@ -33,5 +33,5 @@ cmd_kill(int argc, char **argv)
 
     btpd_connect();
     if ((code = btpd_die(ipc, seconds)) != 0)
-        errx(1, "%s", ipc_strerror(code));
+        diemsg("command failed (%s).\n", ipc_strerror(code));
 }
diff --git a/cli/list.c b/cli/list.c
index a6ed9d9..ce7cc64 100644
--- a/cli/list.c
+++ b/cli/list.c
@@ -60,7 +60,7 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
     struct items *itms = arg;
     struct item *itm = calloc(1, sizeof(*itm));
     if (objerr != IPC_OK)
-        errx(1, "list failed for '%s' (%s)", itms->argv[obji],
+        diemsg("list failed for '%s' (%s).\n", itms->argv[obji],
             ipc_strerror(objerr));
     itms->count++;
     itm->num = (unsigned)res[IPC_TVAL_NUM].v.num;
@@ -148,7 +148,7 @@ cmd_list(int argc, char **argv)
     else
         code = btpd_tget(ipc, itms.tps, itms.ntps, keys, nkeys, list_cb, &itms);
     if (code != IPC_OK)
-        errx(1, "%s", ipc_strerror(code));
+        diemsg("command failed (%s).\n", ipc_strerror(code));
     printf("%-40.40s  NUM ST   HAVE    SIZE   RATIO\n", "NAME");
     print_items(&itms);
 }
diff --git a/cli/stat.c b/cli/stat.c
index c2846c1..6d3b517 100644
--- a/cli/stat.c
+++ b/cli/stat.c
@@ -136,7 +136,7 @@ again:
     else
         err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba);
     if (err != IPC_OK)
-        errx(1, ipc_strerror(err));
+        diemsg("command failed (%s).\n", ipc_strerror(err));
     if (names)
         printf("-------\n");
     if (individual)
diff --git a/cli/stop.c b/cli/stop.c
index 218d7d6..031b00e 100644
--- a/cli/stop.c
+++ b/cli/stop.c
@@ -51,7 +51,7 @@ cmd_stop(int argc, char **argv)
     if (all) {
         enum ipc_err code = btpd_stop_all(ipc);
         if (code != IPC_OK)
-            errx(1, "%s", ipc_strerror(code));
+            diemsg("command failed (%s).\n", ipc_strerror(code));
     } else {
         for (int i = 0; i < argc; i++)
             if (torrent_spec(argv[i], &t))