home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / snoop.lzh / snoop.c < prev    next >
C/C++ Source or Header  |  1993-02-03  |  3KB  |  110 lines

  1.  
  2. /* Simple client-end example of a MaxSnoop app.
  3.  *
  4.  * Microsoft C 6.0a.
  5.  */
  6.  
  7. #define INCL_NOPM
  8. #define INCL_DOS
  9. #define INCL_DOSERRORS
  10. #include <os2.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14.  
  15. static HPIPE OpenPipe(char *name);
  16. static USHORT PRead(HPIPE hp, VOID *pv, USHORT cb, USHORT *pbr);
  17.  
  18. void main(int argc, char **argv)
  19. {
  20.     HPIPE hp;
  21.     char msg[258];
  22.     USHORT bytes;
  23.     int l;
  24.     for(;;){
  25.         hp = OpenPipe(argv[1]);
  26.         if(!hp)
  27.             break;
  28.         while(!PRead(hp, msg, sizeof(msg)-2, &bytes)){
  29.             msg[bytes] = 0;
  30.             l = strlen(msg);
  31.             if(msg[l-1] != '\n'){
  32.                 msg[l] = '\n';
  33.                 msg[l+1] = 0;
  34.             }
  35.             fputs(msg, stdout);
  36.         }
  37.         DosClose(hp);
  38.     }
  39. }
  40.  
  41. /* if the pipe doesn't exist, this function polls for it.  I don't
  42.  * like polling,  but we have no choice,  since the Snooper is
  43.  * usually always running,  but the Snoopee is not.
  44.  */
  45. static HPIPE OpenPipe(char *name)
  46. {
  47.     char *pname;
  48.     USHORT rc, us;
  49.     HPIPE hf;
  50.     char app[256];
  51.     USHORT bytes;
  52.     int shown = FALSE;
  53.  
  54.     pname = name;
  55.     if(!pname)
  56.         pname = "\\pipe\\maxsnoop";
  57.  
  58.  
  59.     printf("pipe name is '%s'\n", pname);
  60.     for(;;){
  61.         rc = DosOpen(pname, &hf, &us, 0L, FILE_NORMAL,
  62.                  FILE_OPEN,
  63.                  OPEN_ACCESS_READONLY  | OPEN_SHARE_DENYREADWRITE | OPEN_FLAGS_NOINHERIT,
  64.                  0L);
  65.         if(!rc)
  66.             break;      // success.
  67.         if(rc == ERROR_PIPE_BUSY){
  68.             printf("pipe is busy. .\n");
  69.             rc = DosWaitNmPipe(pname, 1000L);
  70.             if(rc && rc != ERROR_SEM_TIMEOUT)
  71.                 break;
  72.         }
  73.         else{
  74.             if(!shown){ // only print this mesg once.
  75.                 printf("Pipe doesn't exist; polling continues.\n");
  76.                 shown = TRUE;
  77.             }
  78.             DosSleep(500L);
  79.          }
  80.     }
  81.  
  82.     if(rc){
  83.         printf("dosopen error %d\n", rc);
  84.         return 0;
  85.     }
  86.     DosSetNmPHandState(hf,  NP_READMODE_MESSAGE | NP_WAIT);
  87.     if(!PRead(hf, app, sizeof(app), &bytes))
  88.         printf("\n\nConnected to application: %s\n", app);
  89.     return hf;
  90. }
  91.  
  92. static USHORT PRead(HPIPE hp, VOID *pv, USHORT cb, USHORT *pbr)
  93. {
  94.     USHORT rc;
  95.     USHORT br=0;
  96.     USHORT state;
  97.     AVAILDATA ad;
  98.  
  99.     rc = DosRead(hp, pv, cb, &br);
  100.     if(!br) printf(":read zero bytes\n");
  101.     if(!rc && br == 0){
  102.         DosSleep(1L);   // yield to LanMan on zero byte read,  so it can detect any pipe error.
  103.         rc = DosPeekNmPipe(hp, "", 0, &br, &ad, &state);
  104.         if(!rc && state != NP_CONNECTED)
  105.             rc = ERROR_BROKEN_PIPE;
  106.     }
  107.     *pbr = br;
  108.     return rc;
  109. }
  110.