chiark / gitweb /
with-lock-ex: Replace ad-hoc option parser with getopt.
[chiark-utils.git] / cprogs / with-lock-ex.c
index 2b7b45bee30c09637873123dc606aec9a9e0b64f..d1b23db9064a32bd520783e1473dd9393788442d 100644 (file)
  * compatibility with an earlier version).
  *
  * This file written by me, Ian Jackson, in 1993, 1994, 1995, 1996,
- * 1998, 1999, 2016.  I hereby place it in the public domain.
+ * 1998, 1999, 2016.
+ *
+ * Copyright 1993-2016 Ian Jackson in some jurisdictions
+ * Copyright 2017      Ian Jackson in all jurisdictions
+ * Copyright 2017      Genome Research Ltd
+ *
+ * (MIT licence:)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  */
 
 #include <errno.h>
@@ -42,28 +66,42 @@ static void fail(const char *why) {
   exit(255);
 }
 
+static void badusage(void) __attribute__((noreturn));
+
+static void badusage(void) {
+    fputs("usage: with-lock-ex -w|-q|-f [-t <secs>] <lockfile> <command> <args>...\n"
+         "       with-lock-ex -l       <lockfile>\n"
+         "       with-lock-ex          <lockfile> <command> <args>...\n",
+         stderr);
+    exit(255);
+}
+
+static int mode;
+
 int main(int argc, char **argv) {
-  int fd, mode, um;
+  int fd, um, c;
   struct stat stab, fstab;
   long cloexec;
   struct flock fl;
-  const char *p;
-
-  if (argc >= 3 && !strcmp((p= strrchr(argv[0],'/')) ? ++p : argv[0], "with-lock")) {
-    mode= 'f';
-  } else if (argc < 3 || argv[1][0] != '-' || argv[1][2] ||
-            ((mode= argv[1][1]) != 'w' && mode != 'q' && mode != 'f'
-             && mode != 'l') ||
-            (mode != 'l' && argc < 4) ||
-            (mode == 'l' && argc != 3)) {
-    fputs("usage: with-lock-ex -w|-q|-f <lockfile> <command> <args>...\n"
-         "       with-lock-ex -l       <lockfile>\n"
-         "       with-lock             <lockfile> <command> <args>...\n",
-         stderr);
-    exit(255);
-  } else {
-    argv++; argc--;
+
+  mode= 'x';
+  while ((c= getopt(argc,argv,"+wfqlt:")) != -1) {
+    switch(c) {
+    case 'l':
+    case 'w':
+    case 'f':
+    case 'q':
+      if (mode != 'x') badusage();
+      mode= c;
+      break;
+    default:
+      badusage();
+    }
   }
+
+  argv += optind-1; argc -= optind-1;
+  if (argc < 2) badusage();
+
   cmd= argv[2];
   um= umask(0777); if (um==-1) fail("find umask");
   if (umask(um)==-1) fail("reset umask");