home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / cli / waitnotify-1.1.lha / WaitNotify / waitnotify.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-14  |  6.2 KB  |  263 lines

  1. /*
  2. **  WaitNotify.c
  3. **
  4. **  Copyright (c)1994 by Tobias Ferber,  All Rights Reserved
  5. */
  6.  
  7. #define VERSION "1.1"
  8. static char versiontag[] = "$VER: $Id: waitnotify.c,v 1.1 1994/12/14 18:38:48 tf Exp $";
  9.  
  10. /****** WaitNotify ***********************************************************
  11. *
  12. *   NAME
  13. *    WaitNotify -- Wait for a file or directory modification
  14. *
  15. *   SYNOPSIS
  16. *    WaitNotify NAME/A/M,QUIET/S,FMT=FORMAT/K
  17. *
  18. *   FUNCTION
  19. *    WaitNotify posts a notification request on the given files
  20. *    or directories.  The command will not return until at least
  21. *    one of them has been modified.  For files, it will return
  22. *    after the file is closed.
  23. *
  24. *    The optional format string will be printed when the file or
  25. *    directory changes.  The first "%s" in it will be replaced by
  26. *    the name.
  27. *
  28. *   INPUTS
  29. *    NAME          - name of the files or directories which you want
  30. *            to observe
  31. *    QUIET         - suppresses warnings and error messages (but sets
  32. *            the return value to allow later diagnostis
  33. *    FORMAT        - string to print on exit.
  34. *
  35. *   EXAMPLES
  36. *    ;Wait for a change in our startup-sequence
  37. *    WaitNotify S:Startup-Sequence
  38. *
  39. *    ;Wait for a change in either startup script and report its name
  40. *    WaitNotify S:Startup-Sequence S:User-Startup FMT="%s*N"
  41. *
  42. *    ;Show the first file which changes via More
  43. *    More `WaitNotify S:Startup-Sequence S:User-Startup FMT=%s`
  44. *
  45. *   BUGS
  46. *    Not all    filesystems will support file notification. In particular,
  47. *    most network filesystems won't support it.
  48. *
  49. *
  50. *   DISCLAIMER
  51. *    This file is part of the WaitNotify distribution.
  52. *
  53. *    WaitNotify is free software; you can redistribute it and/or modify
  54. *    it under the terms of the GNU General Public License as published
  55. *    by the Free Software Foundation; either version 1 of the License,
  56. *    or (at your option) any later version.
  57. *
  58. *    WaitNotify is distributed in the hope that it will be useful,
  59. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  60. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  61. *    See the GNU General Public License for more details.
  62. *
  63. *    You should have received a copy of the GNU General Public License
  64. *    along with the program; see the file COPYING.  If not, write to
  65. *    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  66. *
  67. *   COPYRIGHT
  68. *    WaitNotify is Copyright (C)1994 by Tobias Ferber.
  69. *    You can reach me via E-mail: ukjg@rz.uni-karlsruhe.de
  70. *
  71. ******************************************************************************
  72. *
  73. *
  74. */
  75.  
  76. #include <exec/types.h>
  77. #include <exec/memory.h>
  78. #include <dos/dos.h>
  79. #include <dos/notify.h>
  80.  
  81. #include <stdio.h>
  82. #include <stdarg.h>
  83.  
  84. #include <proto/exec.h>
  85. #include <proto/dos.h>
  86.  
  87. #define NIL(type) (type)0
  88.  
  89. void display_version_information(void)
  90. {
  91.   static char license[]=
  92.     "WaitNotify is free software; you can redistribute it and/or modify\n"
  93.     "it under the terms of the GNU General Public License as published\n"
  94.     "by the Free Software Foundation; either version 1 of the License,\n"
  95.     "or (at your option) any later version.\n"
  96.     "\n"
  97.     "WaitNotify is distributed in the hope that it will be useful,\n"
  98.     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  99.     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
  100.     "GNU General Public License for more details.\n"
  101.     "\n"
  102.     "You should have received a copy of the GNU General Public License\n"
  103.     "along with WaitNotify; see the file COPYING.  If not, write to the\n"
  104.     "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  105.     ;
  106.  
  107.   puts("\nWaitNotify Version " VERSION " (compiled " __DATE__ ", " __TIME__ ")\n"
  108.        "(c)Copyright 1994 by Tobias Ferber, ukjg@rz.uni-karlsruhe.de\n");
  109.  
  110.   puts(license);
  111. }
  112.  
  113. int quiet = 0;
  114.  
  115. void echo(const char *fmt, ...)
  116. {
  117.   va_list argp;
  118.   va_start(argp,fmt);
  119.  
  120.   if(!quiet)
  121.   {
  122.     fprintf(stderr,"WaitNotify: ");
  123.     vfprintf(stderr,(char *)fmt,argp);
  124.     fprintf(stderr,"\n");
  125.     fflush(stderr);
  126.   }
  127.  
  128.   va_end(argp);
  129. }
  130.  
  131.  
  132. void unset_notify(struct NotifyRequest *nr)
  133. {
  134.   if(nr)
  135.   {
  136.     BYTE sigbit;
  137.  
  138.     EndNotify(nr);
  139.  
  140.     sigbit= nr->nr_stuff.nr_Signal.nr_SignalNum;
  141.  
  142.     if(sigbit != -1)
  143.       FreeSignal(sigbit);
  144.  
  145.     FreeVec(nr);
  146.   }
  147. }
  148.  
  149.  
  150.  
  151. struct NotifyRequest *set_notify(char *fname)
  152. {
  153.   struct NotifyRequest *nr;
  154.  
  155.   if( (nr= AllocVec(sizeof(struct NotifyRequest), MEMF_CLEAR)) )
  156.   {
  157.     BYTE sigbit;
  158.  
  159.     if( (sigbit= AllocSignal(-1)) != -1)
  160.     {
  161.       nr->nr_Name = fname;
  162.       nr->nr_Flags = NRF_SEND_SIGNAL;
  163.       nr->nr_stuff.nr_Signal.nr_Task = FindTask(NULL);
  164.       nr->nr_stuff.nr_Signal.nr_SignalNum = sigbit;
  165.  
  166.       if (StartNotify(nr) == DOSFALSE)
  167.       {
  168.         echo("StartNotify() failed for \"%s\"",fname);
  169.         FreeVec(nr);
  170.         nr= NIL(struct NotifyRequest *);
  171.         FreeSignal(sigbit);
  172.       }
  173.     }
  174.     else
  175.     {
  176.       echo("AllocSignal() failed for \"%s\"",fname);
  177.       FreeVec(nr);
  178.       nr= NIL(struct NotifyRequest *);
  179.     }
  180.   }
  181.   else
  182.   {
  183.     echo("Not enough free store");
  184.   }
  185.  
  186.   return nr;
  187. }
  188.  
  189.  
  190.  
  191. int main(int argc, char **argv)
  192. {
  193.   struct NotifyRequest *notifys[32];
  194.   ULONG signals[32], sigmask = SIGBREAKF_CTRL_C;
  195.   struct RDArgs *a;
  196.   LONG args[3] = { 0,0,0 };
  197.   char *fmt= NIL(char *);
  198.   int rv = RETURN_OK;
  199.  
  200.   if( a= ReadArgs("NAME/A/M,QUIET/S,FMT=FORMAT/K", args, NULL) )
  201.   {
  202.     char **flist= (char **)args[0];
  203.     int i;
  204.  
  205.     if(args[1])
  206.       quiet= 1;
  207.  
  208.     if(args[2])
  209.       fmt= (char *)args[2];
  210.  
  211.     for(i=0; i<32; i++) notifys[i]= NIL(struct NotifyRequest *);
  212.  
  213.     for(i=0; (i<32) && flist[i] && (rv==RETURN_OK); i++)
  214.     {
  215.       struct NotifyRequest *nr;
  216.  
  217.       if( (nr= set_notify(flist[i])) )
  218.       {
  219.         notifys[i]= nr;
  220.         signals[i]= (1L << nr->nr_stuff.nr_Signal.nr_SignalNum);
  221.         sigmask |= signals[i];
  222.       }
  223.       else rv = RETURN_ERROR;
  224.     }
  225.  
  226.     if( i >= 32 )
  227.     {
  228.       echo("Too many notifys");
  229.       rv = RETURN_ERROR;
  230.     }
  231.  
  232.     if(rv == RETURN_OK)
  233.     {
  234.       ULONG sigset = Wait(sigmask);
  235.  
  236.       if(sigset & SIGBREAKF_CTRL_C)
  237.         rv = RETURN_WARN;
  238.  
  239.       if(fmt)
  240.       {
  241.         for(i=0; (i<32) && notifys[i]; i++)
  242.           if(sigset & signals[i])
  243.             printf(fmt,flist[i]);
  244.       }
  245.     }
  246.  
  247.     for(i=0; (i<32) && notifys[i]; i++)
  248.       unset_notify(notifys[i]);
  249.   }
  250.  
  251.   else /* !ReadArgs */
  252.   {
  253.     if(argc == 1)
  254.       display_version_information();
  255.     else
  256.       PrintFault(IoErr(), NULL);
  257.  
  258.     rv= RETURN_FAIL;
  259.   }
  260.  
  261.   return rv;
  262. }
  263.