home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / misc / filewatcher02.lha / filewatcher.c < prev    next >
C/C++ Source or Header  |  1995-11-25  |  9KB  |  394 lines

  1. #include <dos/dos.h>
  2. #include <dos/notify.h>
  3. #include <dos/dostags.h>
  4. #include <exec/ports.h>
  5. #include <workbench/icon.h>
  6. #include <workbench/startup.h>
  7. #include <workbench/workbench.h>
  8.  
  9. #include <clib/wb_protos.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/exec_protos.h>
  12. #include <clib/alib_protos.h>
  13. #include <clib/icon_protos.h>
  14. #include <clib/wb_protos.h>
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <signal.h>
  19.  
  20. /*
  21.  * Version String
  22.  */
  23.  
  24. STRPTR Version = "$VER: FileWatcher 0.2 (25.11.95) © Tom Hayko";
  25.  
  26. /*
  27.  * Number of Args
  28.  */
  29.  
  30. #define NUM_OF_ARGS     5
  31.  
  32. /*
  33.  * Workbench Args Template
  34.  */
  35.  
  36. STRPTR WbArgTemplate = "FILE,FILEICON,NOFILEICON,NAME,CMD";
  37.  
  38. /*
  39.  * ReadArgs() Template
  40.  */
  41.  
  42. STRPTR ArgTemplate = "FILE/AK,FILEICON/AK,NOFILEICON/AK,NAME/K,CMD/K";
  43.  
  44. /*
  45.  * Define Name under AppIcon
  46.  */
  47.  
  48. #define DEF_NAME    "FileWatcher"
  49.  
  50. /*
  51.  * Local Prototypes
  52.  */
  53.  
  54. ULONG WatchFile(BYTE *ProgName, LONG *Args, FILE *fp);
  55. BOOL GetFileSize(STRPTR FileName, ULONG * FileSize);
  56.  
  57. main(int argc, char **argv)
  58. {
  59.     int i;
  60.     struct RDArgs *rdargs;
  61.     LONG Args[NUM_OF_ARGS];
  62.     ULONG   ulRC;
  63.  
  64.     for (i = 0; i < (sizeof(Args) / sizeof(LONG)); i++)
  65.     {
  66.         Args[i] = 0;
  67.     }
  68.  
  69.     Args[3] = (LONG) DEF_NAME;
  70.     Args[4] = (LONG) "";
  71.  
  72.     rdargs = ReadArgs(ArgTemplate, Args, NULL);
  73.  
  74.     if (rdargs == NULL)
  75.     {
  76.         printf("%s: required argument missing\n", argv[0]);
  77.         exit(RETURN_FAIL);
  78.     }
  79.  
  80.     ulRC = WatchFile(argv[0], Args, NULL);
  81.  
  82.     FreeArgs(rdargs);
  83.  
  84.     exit(0);
  85. }
  86.  
  87. wbmain(struct WBStartup *wbs)
  88. {
  89.     FILE               *fp;
  90.     struct DiskObject  *pDiskObject;
  91.     char              **pcToolArray;
  92.     char               *ToolType;
  93.     int                 i = 0;
  94.     LONG                Args[NUM_OF_ARGS];
  95.     STRPTR              NextArg;
  96.     ULONG   ulRC;
  97.  
  98.     fp = fopen("ram:test.out", "w");
  99.  
  100.     fprintf(fp, "got in\n");
  101.  
  102.     pDiskObject = (struct DiskObject *)GetDiskObject(wbs->sm_ArgList[0].wa_Name);
  103.  
  104.     if(pDiskObject == NULL)
  105.     {
  106.         fprintf(fp, "GetDiskObject failed\n");
  107.     }
  108.     else
  109.     {
  110.         fprintf(fp, "passed GetDiskObject\n");
  111.     }
  112.  
  113.     pcToolArray = pDiskObject->do_ToolTypes;
  114.  
  115.     NextArg = (STRPTR)strtok(WbArgTemplate, ",");
  116.  
  117.     i = 0;
  118.  
  119.     while(NextArg != (STRPTR) NULL)
  120.     {
  121.         ToolType = FindToolType(pcToolArray, NextArg);
  122.  
  123.         if(ToolType != NULL)
  124.         {
  125.             Args[i] = (LONG)strdup(ToolType);
  126.         }
  127.  
  128.         NextArg = (STRPTR)strtok(NULL, ",");
  129.         i++;
  130.     }
  131.  
  132.     FreeDiskObject(pDiskObject);
  133.  
  134.     fprintf(fp, "calling WatchFile\n");
  135.  
  136.     ulRC = WatchFile(wbs->sm_ArgList[0].wa_Name, Args, fp);
  137.  
  138.     fprintf(fp, "called WatchFile, rc = %ld\n", ulRC);
  139.  
  140.     for(i = 0 ; i < 5 ; i++)
  141.     {
  142.         free(Args[i]);
  143.     }
  144.  
  145.     fclose (fp);
  146. }
  147.  
  148. ULONG WatchFile(BYTE *ProgName, LONG *Args, FILE *fp)
  149. {
  150.     BOOL bSuccess;
  151.     struct MsgPort *msgport;
  152.     struct MsgPort *appmsgport;
  153.     struct NotifyRequest notifyreq;
  154.     struct Message *msg;
  155.     struct DiskObject *diskobjFile;
  156.     struct DiskObject *diskobjNoFile;
  157.     struct AppIcon *appicon;
  158.     ULONG filesize;
  159.     ULONG newfilesize;
  160.     ULONG sig;
  161.     ULONG sigcntrlc;
  162.     ULONG sigmsg;
  163.     ULONG sigappmsg;
  164.     ULONG lRC;
  165.  
  166.     signal(SIGINT, SIG_IGN);
  167.     signal(SIGTERM, SIG_IGN);
  168.  
  169.     diskobjFile = GetDiskObject((char *) Args[1]);
  170.  
  171.     if (diskobjFile == NULL)
  172.     {
  173.         fprintf(fp, "%s: couldn't load icon '%s'\n", ProgName, Args[1]);
  174.         exit(RETURN_FAIL);
  175.     }
  176.  
  177.     diskobjNoFile = GetDiskObject((char *) Args[2]);
  178.  
  179.     if (diskobjNoFile == NULL)
  180.     {
  181.         fprintf(fp, "%s: couldn't load icon '%s'\n", ProgName, Args[2]);
  182.         FreeDiskObject(diskobjFile);
  183.         exit(RETURN_FAIL);
  184.     }
  185.  
  186.     msgport = CreatePort((STRPTR) NULL, 0);
  187.  
  188.     if (msgport == NULL)
  189.     {
  190.         fprintf(fp, "%s: couldn't CreatePort()\n", ProgName);
  191.         FreeDiskObject(diskobjNoFile);
  192.         FreeDiskObject(diskobjFile);
  193.         exit(RETURN_FAIL);
  194.     }
  195.  
  196.     appmsgport = CreatePort((STRPTR) NULL, 0);
  197.  
  198.     if (appmsgport == NULL)
  199.     {
  200.         fprintf(fp, "%s: couldn't CreatePort()\n", ProgName);
  201.         DeletePort(msgport);
  202.         FreeDiskObject(diskobjNoFile);
  203.         FreeDiskObject(diskobjFile);
  204.         exit(0);
  205.     }
  206.  
  207.     bSuccess = GetFileSize((STRPTR) Args[0], &filesize);
  208.  
  209.     if (!bSuccess)
  210.     {
  211.         fprintf(fp, "%s: couldn't get size of file '%s'\n", ProgName, (char *) Args[0]);
  212.         DeletePort(msgport);
  213.         FreeDiskObject(diskobjNoFile);
  214.         FreeDiskObject(diskobjFile);
  215.         exit(0);
  216.     }
  217.  
  218.     notifyreq.nr_Name = (UBYTE *) Args[0];
  219.     notifyreq.nr_Flags = NRF_SEND_MESSAGE;
  220.     notifyreq.nr_stuff.nr_Msg.nr_Port = msgport;
  221.  
  222.     bSuccess = StartNotify(¬ifyreq);
  223.  
  224.     if (!bSuccess)
  225.     {
  226.         fprintf(fp, "StartNotify() failed\n");
  227.         DeletePort(msgport);
  228.         FreeDiskObject(diskobjNoFile);
  229.         FreeDiskObject(diskobjFile);
  230.         exit(0);
  231.     }
  232.  
  233.     if (filesize != 0)
  234.     {
  235.         appicon = AddAppIcon(0L,
  236.                              0L,
  237.                              (char *) Args[3],
  238.                              appmsgport,
  239.                              NULL,
  240.                              diskobjFile,
  241.                              TAG_DONE);
  242.     }
  243.     else
  244.     {
  245.         appicon = AddAppIcon(0L,
  246.                              0L,
  247.                              (char *) Args[3],
  248.                              appmsgport,
  249.                              NULL,
  250.                              diskobjNoFile,
  251.                              TAG_DONE);
  252.     }
  253.  
  254.     if (appicon == NULL)
  255.     {
  256.         fprintf(fp, "AddAppIcon() failed\n");
  257.         EndNotify(¬ifyreq);
  258.  
  259.         while (msg = GetMsg(msgport))
  260.             ReplyMsg(msg);
  261.  
  262.         DeletePort(appmsgport);
  263.         DeletePort(msgport);
  264.         exit(0);
  265.     }
  266.  
  267.     sigcntrlc = 1L << SIGBREAKB_CTRL_C;
  268.     sigmsg = 1L << msgport->mp_SigBit;
  269.     sigappmsg = 1L << appmsgport->mp_SigBit;
  270.  
  271.     while (sig = Wait(sigcntrlc | sigmsg | sigappmsg))
  272.     {
  273.         if (sig & sigcntrlc)
  274.         {
  275.             break;
  276.         }
  277.  
  278.         if (sig & sigmsg)
  279.         {
  280.             while (msg = GetMsg(msgport))
  281.             {
  282.                 ReplyMsg(msg);
  283.             }
  284.  
  285.             bSuccess = GetFileSize((STRPTR)Args[0], &newfilesize);
  286.  
  287.             RemoveAppIcon(appicon);
  288.  
  289.             if (newfilesize != 0)
  290.             {
  291.                 appicon = AddAppIcon(0L,
  292.                                      0L,
  293.                                      (char *) Args[3],
  294.                                      appmsgport,
  295.                                      NULL,
  296.                                      diskobjFile,
  297.                                      TAG_DONE);
  298.             }
  299.             else
  300.             {
  301.                 appicon = AddAppIcon(0L,
  302.                                      0L,
  303.                                      (char *) Args[3],
  304.                                      appmsgport,
  305.                                      NULL,
  306.                                      diskobjNoFile,
  307.                                      TAG_DONE);
  308.             }
  309.  
  310.             filesize = newfilesize;
  311.         }
  312.  
  313.         if (sig & sigappmsg)
  314.         {
  315.             while (msg = GetMsg(appmsgport))
  316.             {
  317.                 ReplyMsg(msg);
  318.             }
  319.  
  320.             if(strcmp((STRPTR) Args[4], "") != 0)
  321.             {
  322.                 fprintf(fp, "spawning command '%s'\n", (STRPTR) Args[4]);
  323.  
  324.                 lRC = SystemTags((STRPTR) Args[4], SYS_Asynch, 0, TAG_DONE, 0);
  325.  
  326.                 if(lRC != 0)
  327.                 {
  328.                     fprintf(fp,
  329.                             "Error spawning command '%s', rc = %ld\n",
  330.                             (STRPTR) Args[4],
  331.                             lRC);
  332.                     break;
  333.                 }
  334.             }
  335.         }
  336.     }
  337.  
  338.     EndNotify(¬ifyreq);
  339.  
  340.     RemoveAppIcon(appicon);
  341.  
  342.     while (msg = GetMsg(appmsgport))
  343.         ReplyMsg(msg);
  344.  
  345.     DeletePort(appmsgport);
  346.  
  347.     while (msg = GetMsg(msgport))
  348.         ReplyMsg(msg);
  349.  
  350.     DeletePort(msgport);
  351.  
  352.     FreeDiskObject(diskobjNoFile);
  353.     FreeDiskObject(diskobjFile);
  354. }
  355.  
  356. BOOL GetFileSize(STRPTR FileName, ULONG * FileSize)
  357. {
  358.     BPTR bptrLock;
  359.     struct FileInfoBlock *pfileinfoblock;
  360.     BOOL fSuccess;
  361.  
  362.     bptrLock = Lock(FileName, ACCESS_READ);
  363.  
  364.     if (bptrLock == 0)
  365.     {
  366.         *FileSize = 0L;
  367.         return (TRUE);
  368.     }
  369.  
  370.     pfileinfoblock = (struct FileInfoBlock *) AllocDosObjectTags(DOS_FIB, TAG_DONE);
  371.  
  372.     if (pfileinfoblock == NULL)
  373.     {
  374.         UnLock(bptrLock);
  375.         return (FALSE);
  376.     }
  377.  
  378.     fSuccess = Examine(bptrLock, pfileinfoblock);
  379.  
  380.     if (!fSuccess)
  381.     {
  382.         FreeDosObject(DOS_FIB, pfileinfoblock);
  383.         UnLock(bptrLock);
  384.         return (FALSE);
  385.     }
  386.  
  387.     *FileSize = pfileinfoblock->fib_Size;
  388.  
  389.     FreeDosObject(DOS_FIB, pfileinfoblock);
  390.     UnLock(bptrLock);
  391.  
  392.     return (TRUE);
  393. }
  394.