PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/setlock.c

https://bitbucket.org/vaporoid/daemontools
C | 50 lines | 41 code | 9 blank | 0 comment | 10 complexity | b54769efed50e3d1abb43529aaab1d81 MD5 | raw file
  1. #include <unistd.h>
  2. #include "lock.h"
  3. #include "open.h"
  4. #include "strerr.h"
  5. #include "pathexec.h"
  6. #include "sgetopt.h"
  7. #define FATAL "setlock: fatal: "
  8. void usage() {
  9. strerr_die1x(100,"setlock: usage: setlock [ -nNxX ] file program [ arg ... ]");
  10. }
  11. int flagndelay = 0;
  12. int flagx = 0;
  13. int main(int argc,const char *const *argv,const char *const *envp)
  14. {
  15. int opt;
  16. int fd;
  17. const char *file;
  18. while ((opt = getopt(argc,argv,"nNxX")) != opteof)
  19. switch(opt) {
  20. case 'n': flagndelay = 1; break;
  21. case 'N': flagndelay = 0; break;
  22. case 'x': flagx = 1; break;
  23. case 'X': flagx = 0; break;
  24. default: usage();
  25. }
  26. argv += optind;
  27. if (!*argv) usage();
  28. file = *argv++;
  29. if (!*argv) usage();
  30. fd = open_append(file);
  31. if (fd == -1) {
  32. if (flagx) _exit(0);
  33. strerr_die4sys(111,FATAL,"unable to open ",file,": ");
  34. }
  35. if ((flagndelay ? lock_exnb : lock_ex)(fd) == -1) {
  36. if (flagx) _exit(0);
  37. strerr_die4sys(111,FATAL,"unable to lock ",file,": ");
  38. }
  39. pathexec_run(*argv,argv,envp);
  40. strerr_die4sys(111,FATAL,"unable to run ",*argv,": ");
  41. }