summaryrefslogtreecommitdiff
path: root/cprogs
diff options
context:
space:
mode:
authorMatthew Vernon <mv3@sanger.ac.uk>2017-05-03 14:30:02 +0100
committerIan Jackson <ijackson@chiark.greenend.org.uk>2017-05-03 21:37:22 +0100
commit92f93b3cda23695e8c10ac611ea3ada274619282 (patch)
tree09375a417900a208847c770ce4185e5fab62ac71 /cprogs
parentb95ccd9ef90889e001d9d9061efd94af47466d64 (diff)
with-lock-ex: Replace ad-hoc option parser with getopt.
Also, fix typo in usage message (missing -ex). Signed-off-by: Matthew Vernon <mv3@sanger.ac.uk> Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk> --- v5: Fix commit message. v4: make coding style consistent fix typo in usage message (missing -ex) v3: split off from timeout patch add copyright notice Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Diffstat (limited to 'cprogs')
-rw-r--r--cprogs/with-lock-ex.c49
1 files changed, 32 insertions, 17 deletions
diff --git a/cprogs/with-lock-ex.c b/cprogs/with-lock-ex.c
index f10e682..d1b23db 100644
--- a/cprogs/with-lock-ex.c
+++ b/cprogs/with-lock-ex.c
@@ -26,6 +26,7 @@
*
* Copyright 1993-2016 Ian Jackson in some jurisdictions
* Copyright 2017 Ian Jackson in all jurisdictions
+ * Copyright 2017 Genome Research Ltd
*
* (MIT licence:)
*
@@ -65,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");