From 1c0cdde1a795434dbfb21f0220cae09dcca562e4 Mon Sep 17 00:00:00 2001 From: Matthew Vernon Date: Wed, 3 May 2017 14:30:02 +0100 Subject: [PATCH] with-lock-ex: Replace ad-hoc option parser with getopt. Signed-off-by: Matthew Vernon Signed-off-by: Ian Jackson --- v3: split off from timeout patch add copyright notice Signed-off-by: Ian Jackson --- cprogs/with-lock-ex.c | 50 +++++++++++++++++++++++++++---------------- debian/copyright | 1 + 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/cprogs/with-lock-ex.c b/cprogs/with-lock-ex.c index f10e682..e00309d 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,41 @@ static void fail(const char *why) { exit(255); } -int main(int argc, char **argv) { - int fd, mode, um; - 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 ...\n" +static void badusage(void) __attribute__((noreturn)); + +static void badusage(void) { + fputs("usage: with-lock-ex -w|-q|-f [-t ] ...\n" " with-lock-ex -l \n" " with-lock ...\n", stderr); exit(255); - } else { - argv++; argc--; - } +} + +static int mode; + +int main(int argc, char **argv) { + int fd, um, c; + struct stat stab, fstab; + long cloexec; + struct flock fl; + + 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"); diff --git a/debian/copyright b/debian/copyright index 4d40a01..1a71958 100644 --- a/debian/copyright +++ b/debian/copyright @@ -86,6 +86,7 @@ git-cache-proxy with-lock-ex Copyright 1993-2016 Ian Jackson in some jurisdictions Copyright 2017 Ian Jackson in all jurisdictions + Copyright 2017 Genome Research Ltd The chiark utilities are all free software; you can redistribute them and/or modify them under the terms of the GNU General Public License -- 2.30.2