home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Uip / misc / flock.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  1.5 KB  |  84 lines

  1. /* flock.c: lock a file and execute a command */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Uip/misc/RCS/flock.c,v 6.0 1991/12/18 20:39:34 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Uip/misc/RCS/flock.c,v 6.0 1991/12/18 20:39:34 jpo Rel $
  9.  *
  10.  * $Log: flock.c,v $
  11.  * Revision 6.0  1991/12/18  20:39:34  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "util.h"
  19. #include <sys/wait.h>
  20.  
  21. #ifndef WEXITSTATUS
  22. #define WEXITSTATUS(x) (((union wait *)&(x)) -> w_retcode)
  23. #endif
  24. #ifndef WTERMSIG
  25. #define WTERMSIG(x) (((union wait *)&(x)) -> w_termsig)
  26. #endif
  27.  
  28. main (argc, argv)
  29. int    argc;
  30. char    **argv;
  31. {
  32.     FILE    *fp;
  33.     extern int errno;
  34.     int    pid, cpid;
  35. #ifdef SYS5
  36.     int     status;
  37. #else
  38.     union wait status;
  39. #endif
  40.  
  41.     uip_init (argv[0]);
  42.     if (argc < 3) {
  43.         fprintf (stderr, "Usage: %s file commands...\n", argv[0]);
  44.         exit (1);
  45.     }
  46.  
  47.     if ((fp = flckopen (argv[1], "a")) == NULL) {
  48.         fprintf (stderr, "%s: Can't open file %s: %s\n",
  49.              argv[0], argv[1], sys_errname (errno));
  50.         exit (1);
  51.     }
  52.  
  53.     if ((pid = tryfork () ) == NOTOK) {
  54.         fprintf (stderr, "%s: can't fork: %s\n", argv[0],
  55.              sys_errname (errno));
  56.         exit (1);
  57.     }
  58.  
  59.     if (pid == 0) {
  60.         fclose (fp);
  61.  
  62.         execvp (argv[2], &argv[2]);
  63.         _exit (1);
  64.     }
  65.  
  66. #ifdef SYS5
  67.     while ((cpid = wait( &status)) != pid && cpid != NOTOK)
  68. #else
  69.     while ((cpid = wait( &status.w_status)) != pid && cpid != NOTOK)
  70. #endif
  71.         continue;
  72.  
  73.     if (cpid == NOTOK)    /* ??? */
  74.         exit (1);
  75.  
  76.     if (WIFSIGNALED(status))
  77.         exit (1);
  78.  
  79.     if (WIFEXITED(status))
  80.         exit (WEXITSTATUS(status));
  81.  
  82.     exit (1);
  83. }
  84.