chiark / gitweb /
watershed, common: Break out m_[v]asprintf into common.c (nfc0
[chiark-utils.git] / cprogs / watershed.c
index 3bf9b0763e3b1643e9a1cf20f0b85f08295af817..f8fbc898727eca2ff5eb00b406444173b9e397c7 100644 (file)
 /*
  */
 
-#define _GNU_SOURCE
+#include "common.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <nettle/sha.h>
 
+#define die  common_die
+#define diee common_diee
+
 static const struct option os[]= {
   { "--state-dir", 1,0,'d' },
   { "--command-id",1,0,'i' },
+  { "--help",      0,0,'h' },
   { 0 }
 };
 
@@ -294,18 +298,24 @@ static int cohort_fd, lock_fd;
   }while(0)
 
 
-static void badusage(void) {
+static void printusage(FILE *f) {
   fputs(_("usage: watershed [<options>] <command>...\n"
-         "options: -d|--state-dir <directory>  -i|--command-id <id>\n"
+         "options:\n"
+         "   -d|--state-dir <directory>\n"
+         "   -i|--command-id <id>\n"
+         "   -h|--help\n"
          "see /usr/share/doc/chiark-utils-bin/watershed.txt\n"),
-         stderr);
+         f);
+}
+static void badusage(void) {
+  printusage(stderr);
   exit(127);
 }
-static void die(const char *m) {
+void die(const char *m) {
   fprintf(stderr,_("watershed: error: %s\n"), m);
   exit(127);
 }
-static void diee(const char *m) {
+void diee(const char *m) {
   fprintf(stderr,_("watershed: error: %s failed: %s\n"), m, strerror(errno));
   exit(127);
 }
@@ -315,26 +325,15 @@ static void dieep(const char *action, const char *path) {
   exit(127);
 }
 
-static char *m_vasprintf(const char *fmt, va_list al) {
-  char *s;  int r;
-  r= vasprintf(&s,fmt,al);
-  if (r==-1) diee("vasprintf");
-  return s;
-}
-static char *m_asprintf(const char *fmt, ...) {
-  char *s;  va_list al;
-  va_start(al,fmt); s= m_vasprintf(fmt,al); va_end(al);
-  return s;
-}
-
 static void parse_args(int argc, char *const *argv) {
   int o;
   for (;;) {
-    o= getopt_long(argc, argv, "+d:i:", os,0);
+    o= getopt_long(argc, argv, "+d:i:h", os,0);
     if (o==-1) break;
     switch (o) {
     case 'd': state_dir= optarg; break;
     case 'i': command_id= optarg; break;
+    case 'h': printusage(stdout); exit(0); break;
     default: badusage();
     }
   }