locking.c: Compress vertically. Remove pointless `$Id' comment.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 Apr 2016 22:30:30 +0000 (23:30 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 29 Apr 2016 00:16:23 +0000 (01:16 +0100)
locking.c

index 8931dbd..9435318 100644 (file)
--- a/locking.c
+++ b/locking.c
@@ -1,7 +1,5 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Lock a file, run a program
  *
  * (c) 2003 Mark Wooding
@@ -61,9 +59,7 @@ static void usage(FILE *fp)
 }
 
 static void version(FILE *fp)
-{
-  pquis(fp, "$ (version " VERSION ")\n");
-}
+  { pquis(fp, "$ (version " VERSION ")\n"); }
 
 static void help(FILE *fp)
 {
@@ -127,42 +123,19 @@ int main(int argc, char *argv[])
 
     int i = mdwopt(argc, argv, "-hvuw+f+c+x+p:t:", opts,
                   0, 0, OPTF_NEGATION);
-    if (i < 0)
-      break;
+    if (i < 0) break;
     switch (i) {
-      case 'h':
-       help(stdout);
-       exit(0);
-      case 'v':
-       version(stdout);
-       exit(0);
-      case 'u':
-       usage(stdout);
-       exit(0);
-      case 'w':
-       f |= f_wait;
-       break;
-      case 'w' | OPTF_NEGATED:
-       f &= ~f_wait;
-       break;
-      case 'f':
-       f |= f_fail;
-       break;
-      case 'f' | OPTF_NEGATED:
-       f &= ~f_fail;
-       break;
-      case 'c':
-       f |= f_create;
-       break;
-      case 'c' | OPTF_NEGATED:
-       f &= ~f_create;
-       break;
-      case 'x':
-       f |= f_excl;
-       break;
-      case 'x' | OPTF_NEGATED:
-       f &= ~f_excl;
-       break;
+      case 'h': help(stdout); exit(0);
+      case 'v': version(stdout); exit(0);
+      case 'u': usage(stdout); exit(0);
+      case 'w': f |= f_wait; break;
+      case 'w' | OPTF_NEGATED: f &= ~f_wait; break;
+      case 'f': f |= f_fail; break;
+      case 'f' | OPTF_NEGATED: f &= ~f_fail; break;
+      case 'c': f |= f_create; break;
+      case 'c' | OPTF_NEGATED: f &= ~f_create; break;
+      case 'x': f |= f_excl; break;
+      case 'x' | OPTF_NEGATED: f &= ~f_excl; break;
       case 't':
        errno = 0;
        t = strtol(optarg, &p, 0);
@@ -174,23 +147,15 @@ int main(int argc, char *argv[])
          case 0: break;
          default: die(111, "unknown time unit `%c'", *p);
        }
-       if (*p || t < 0 || errno)
-         die(111, "bad time value `%s'", optarg);
+       if (*p || t < 0 || errno) die(111, "bad time value `%s'", optarg);
        f |= f_wait;
        break;
-      case 'p':
-       prog = optarg;
-       break;
+      case 'p': prog = optarg; break;
       case 0:
-       if (file) {
-         optind--;
-         goto doneopts;
-       }
+       if (file) { optind--; goto doneopts; }
        file = optarg;
        break;
-      default:
-       f |= f_bogus;
-       break;
+      default: f |= f_bogus; break;
     }
   }
 
@@ -201,8 +166,7 @@ doneopts:
   }
 
   av = &argv[optind];
-  if (!prog)
-    prog = av[0];
+  if (!prog) prog = av[0];
   if ((fd = open(file,
                 ((f & f_create ? O_CREAT : 0) |
                  (f & f_excl ? O_RDWR : O_RDONLY)), 0666)) < 0)
@@ -218,30 +182,24 @@ doneopts:
   } else {
     ot = alarm(0);
     oalrm = signal(SIGALRM, alrm);
-    if (t >= 0)
-      alarm(t);
-    if (fcntl(fd, f & f_wait ? F_SETLKW : F_SETLK, &l) >= 0)
-      errno = 0;
+    if (t >= 0) alarm(t);
+    if (fcntl(fd, f & f_wait ? F_SETLKW : F_SETLK, &l) >= 0) errno = 0;
   }
   signal(SIGALRM, oalrm);
   if (!ot)
     alarm(0);
   else {
     nt = time(0) - nt;
-    if (nt > ot)
-      raise(SIGALRM);
-    else
-      alarm(ot - nt);
+    if (nt > ot) raise(SIGALRM);
+    else alarm(ot - nt);
   }
   if (errno &&
       ((errno != EAGAIN && errno != EWOULDBLOCK && errno != EACCES) ||
        (f & f_fail)))
     die(111, "error locking `%s': %s", file, strerror(errno));
-  if (errno)
-    exit(0);
+  if (errno) exit(0);
 
-  if ((kid = fork()) < 0)
-    die(111, "error from fork: %s", strerror(errno));
+  if ((kid = fork()) < 0) die(111, "error from fork: %s", strerror(errno));
   if (!kid) {
     close(fd);
     execvp(prog, av);
@@ -255,10 +213,8 @@ doneopts:
   l.l_len = 0;
   fcntl(fd, F_SETLK, &l);
   close(fd);
-  if (WIFEXITED(st))
-    exit(WEXITSTATUS(st));
-  else
-    exit(255);
+  if (WIFEXITED(st)) exit(WEXITSTATUS(st));
+  else exit(255);
 }
 
 /*----- That's all, folks -------------------------------------------------*/