home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / fax067.zip / killfw.c < prev    next >
C/C++ Source or Header  |  1996-11-25  |  4KB  |  221 lines

  1. #define INCL_DOS
  2. //#define INCL_NOPMAPI
  3. #define INCL_WINWINDOWMGR
  4. #include <os2.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <fcntl.h>
  10. #include <sys\stat.h>
  11. #include <share.h>
  12. #include <io.h>
  13.  
  14. #define DEFAULTSIZE 256
  15.  
  16. UCHAR tmpfn[] = "$$$$$$$$.$$$";
  17. struct LOG_ENTRY
  18.     {
  19.     ULONG fileno;
  20.     UCHAR date[10],time[6];
  21.     USHORT pages;
  22.     UCHAR status[8],duration[6];
  23.     UCHAR *coverpage;
  24.     UCHAR *dstname,*dstcompany,*dstfaxno;
  25.     UCHAR *srcname,*srccompany,*srcphoneno,*srcfaxno;
  26.     UCHAR *comment;
  27.     UCHAR *what,*dstTSI;
  28.     struct LOG_ENTRY *next;
  29.     };
  30.  
  31. typedef struct LOG_ENTRY LOGENTRY;
  32.  
  33.  
  34.  
  35. /************************************************************************/
  36. UCHAR *get_field(UCHAR *src,UCHAR **dst)
  37. {
  38. UCHAR end,*s;
  39. if (*src == '"') { end = '"'; src++; }
  40. else end = ',';
  41. s = strchr(src,end);
  42. if (s)
  43.     {
  44.     *s = 0;
  45.     *dst = calloc(1,strlen(src)+1);
  46.     strcpy(*dst,src);
  47.     if (end == ',') return(&s[1]);
  48.     else return(&s[2]);
  49.     }
  50. *dst = NULL;
  51. return(src);
  52. }
  53.  
  54. ULONG check_logentry(UCHAR *l)
  55. {
  56. ULONG i;
  57. UCHAR *s;
  58. for (i = 0;i < 23;i++)
  59.     {
  60.     l = get_field(l,&s);
  61.     if (s && strlen(s))
  62.         {
  63.         if (i == 4) // Status
  64.             {
  65.             if (strstr(s,"Send") || strstr(s,"Spool"))
  66.                 return(1);
  67.             }
  68.         }
  69.     }
  70. return(0);
  71. }
  72.  
  73. /**********************************************************************/
  74. int readlog(UCHAR *logfn)
  75. {
  76. int cnt = 0;
  77. FILE *fp;
  78. UCHAR *s,tmp[DEFAULTSIZE];
  79. int fh;
  80. printf("\nRead %s",logfn);
  81. fh = sopen(logfn, O_RDONLY, SH_DENYNO);
  82. if (fh != -1)
  83.     {
  84.     fp = fdopen(fh,"rt");
  85.     while (fgets(tmp,DEFAULTSIZE,fp) != NULL)
  86.         {
  87.         s = strchr(tmp,'\n');
  88.         if (s) *s = 0;
  89.         cnt += check_logentry(tmp);
  90.         }
  91.     fclose(fp);
  92.     }
  93. else
  94.     {
  95.     printf(" ... open error");
  96.     return(-1);
  97.     }
  98. if (cnt) printf(" ... Send or Spool in progress");
  99. else printf(" ... no fax activity");
  100. return(cnt);
  101. }
  102.  
  103. PID getID(UCHAR *pname,ULONG cnt)
  104. {
  105. FILE *fp;
  106. PID pid;
  107. ULONG ppid,sid;
  108. UCHAR l[255],proc[255],*p;
  109. strupr(pname);
  110. sprintf(l,"PSTAT /C > %s",tmpfn);
  111. system(l);
  112. fp = fopen(tmpfn,"r");
  113. while (fgets(l,255,fp))
  114.     {
  115.     if (isdigit(l[1]))
  116.         {
  117.         sscanf(&l[1],"%x %x %x %s",&pid,&ppid,&sid,proc);
  118.         p = strstr(proc,pname);
  119.         if (p && --cnt == 0)
  120.             {
  121.             printf("\n%s PID=%u",proc,pid);
  122.             fclose(fp);
  123.             unlink(tmpfn);
  124.             return(pid);
  125.             }
  126.         }
  127.     }
  128. fclose(fp);
  129. unlink(tmpfn);
  130. return((PID) 0);
  131. }
  132.  
  133. BOOL trytokill(void)
  134. {
  135. BOOL kill;
  136. APIRET  rc;
  137. PID ProcessID;
  138. HWND  hwndNext;
  139. HENUM  henum;
  140. PID  pid;   // Process identity of the thread that created the window
  141. TID  tid;   // Thread identity of the thread that created the window
  142. ProcessID = getID("FAXWORKS.EXE",1);
  143. kill = FALSE;
  144. henum = WinBeginEnumWindows(HWND_DESKTOP);
  145. while ((hwndNext = WinGetNextWindow(henum)) != NULLHANDLE)
  146.     {
  147.     if (WinQueryWindowProcess(hwndNext,&pid,&tid))
  148.         {
  149.         if (pid == ProcessID)
  150.             {
  151.             WinPostMsg(hwndNext,WM_QUIT,0,0);
  152.             printf(" ... WM_QUIT sent!");
  153.             kill = TRUE;
  154.             break;
  155.             }
  156.         }
  157.     }
  158. WinEndEnumWindows(henum);
  159. DosSleep(5000); // wait 5 sec
  160. ProcessID = getID("FAXWORKS.EXE",1);
  161. if (ProcessID)
  162.     {
  163.     rc = DosKillProcess(DKP_PROCESS,ProcessID);
  164.     if (rc != 0)
  165.         {
  166.         printf("DosKillProcess error: return code = %ld", rc);
  167.         return(FALSE);
  168.         }
  169.     else
  170.         {
  171.         kill = TRUE;
  172.         printf(" ... killed!");
  173.         }
  174.     }
  175. return(kill);
  176. }
  177.  
  178. int main(int argc,char **argv)
  179. {
  180. ULONG t = 60000;
  181. int ret,cnt = 2;
  182. if (argc < 2)
  183.     {
  184.     printf("\n%s <path_to_fax.log>",argv[0]);
  185.     return(1);
  186.     }
  187. strupr(argv[1]);
  188. printf("\nWaiting %u seconds till reading %s",t/1000,argv[1]);
  189. while (1)
  190.     {
  191.     DosSleep(t);
  192.     if (!access(argv[1],0))
  193.         {
  194.         ret = readlog(argv[1]);
  195.         switch(ret)
  196.             {
  197.             case 0:
  198.                 cnt--;
  199.             case -1:
  200.                 t = 2000;
  201.                 break;
  202.             default:
  203.                 cnt = 2;
  204.                 t = 10000;
  205.                 break;
  206.             }
  207.         }
  208.     else
  209.         {
  210.         printf("\n%s does not exist",argv[1]);
  211.         return(2);
  212.         }
  213.     if (cnt == 0)
  214.         {
  215.         trytokill();
  216.         return(0);
  217.         }
  218.     }
  219. }
  220.  
  221.