home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / OS2 / EDM2 / COMMON / SNIPPETS / PIPES.TXT < prev   
Encoding:
Text File  |  1997-04-29  |  3.7 KB  |  167 lines

  1. oobles@loose.apana.org.au (David Ryan):
  2.  
  3. Recently there have been quite a few queries about Pipes and stdin/stdout
  4. redirection on the comp.os.os2.programming.misc forum.  These two small
  5. programs do both, and serve as a good example.  I threw these together
  6. over a few hours, so it may do a few redundant things, however it does
  7. work.
  8.  
  9. What does it do?  Just a very simple Telnet type program over named pipes.
  10. I intended to use it so that I could compile my programmes on other
  11. machines in the office, but as we aren't running peer services, they are
  12. of no use to me.
  13.  
  14. --------------------------------SERV.C------------------------------
  15. #define INCL_DOSQUEUES
  16. #define INCL_DOS
  17. #include <os2.h>
  18.  
  19. #include <process.h>
  20.  
  21. #define PIPESIZE 256
  22. #define HF_STDIN 0
  23. #define HF_STDOUT 1
  24. #define HF_STDERR 2
  25.  
  26.  
  27. HPIPE hpOutR, hpOutW;
  28. HPIPE hpInR, hpInW;
  29. HPIPE hpErrR, hpErrW;
  30. HPIPE NmPipeHandle;
  31.  
  32. RESULTCODES resc;
  33.  
  34. CHAR szFailName[CCHMAXPATH];
  35.  
  36. void read( void *dummy )
  37. {
  38.     ULONG cbRead, cbWritten;
  39.     CHAR achBuf[PIPESIZE];
  40.  
  41.     do {
  42.        DosRead( hpOutR, achBuf, sizeof(achBuf), &cbRead );
  43.        DosWrite( NmPipeHandle, achBuf, cbRead, &cbWritten );
  44.     } while ( cbRead );
  45. }
  46.  
  47. void write( void *dummy )
  48. {
  49.    ULONG cbRead, cbWritten;
  50.    CHAR achBuf[PIPESIZE];
  51.  
  52.    do {
  53.        DosRead( NmPipeHandle, achBuf, sizeof(achBuf), &cbRead );
  54.        DosWrite( hpInW, achBuf, cbRead, &cbWritten );
  55.     } while ( cbRead );
  56. }
  57.  
  58. main(int argc, char *argv[], char *envp[])
  59. {
  60.     APIRET rc;
  61.  
  62.     HFILE hfSaveOut = -1, hfNewOut = HF_STDOUT;
  63.     HFILE hfSaveIn  = 10, hfNewIn  = HF_STDIN;
  64.  
  65.     DosDupHandle( HF_STDOUT , &hfSaveOut );
  66.     DosDupHandle( HF_STDIN  , &hfSaveIn  );
  67.  
  68.     DosCreatePipe( &hpOutR, &hpOutW, PIPESIZE);
  69.     DosCreatePipe( &hpInR , &hpInW , PIPESIZE);
  70.  
  71.     DosDupHandle( hpOutW, &hfNewOut );
  72.     DosDupHandle( hpInR,  &hfNewIn  );
  73.  
  74.     DosExecPgm( szFailName, sizeof(szFailName),
  75.                 EXEC_ASYNC, (PSZ) NULL, (PSZ) NULL, &resc,
  76.                 "C:\\OS2\\CMD.EXE");
  77.  
  78.     DosClose(hpOutW);
  79.     DosClose(hpInR);
  80.  
  81.     DosDupHandle( hfSaveOut, &hfNewOut );
  82.     DosDupHandle( hfSaveIn,  &hfNewIn  );
  83.  
  84.     rc = DosCreateNPipe( "\\\\W304743B\\PIPE\\CMDPIPE", &NmPipeHandle ,
  85. NP_ACCESS_DUPLEX,
  86.                 NP_WMESG | NP_RMESG | 0x01, 256 , 256 , -1 );
  87.     if ( rc ) return 0;
  88.  
  89.     rc = DosConnectNPipe( NmPipeHandle );
  90.     if ( rc ) return 0;
  91.  
  92.     _beginthread( &write , NULL, 64000, NULL );
  93.     read( NULL );
  94.     return 1;
  95. }
  96.  
  97. ---------------------------------------CLNT.C---------------------
  98. #define INCL_DOSQUEUES
  99. #define INCL_DOS
  100. #include <os2.h>
  101.  
  102. #include <process.h>
  103. #include <string.h>
  104. #include <stdio.h>
  105.  
  106. #define PIPESIZE 256
  107. #define SERVER_PIPE_NAME "\\PIPE\\CMDPIPE"
  108. #define HF_STDIN 0
  109. #define HF_STDOUT 1
  110.  
  111. HPIPE hp;
  112. ULONG ulAction;
  113. APIRET rc;
  114.  
  115.  
  116. void read( void *dummy )
  117. {
  118.     ULONG cbRead, cbWritten;
  119.     CHAR achBuf[PIPESIZE];
  120.  
  121.     do {
  122.        DosRead( hp, achBuf, sizeof(achBuf), &cbRead );
  123.        DosWrite( HF_STDOUT, achBuf, cbRead, &cbWritten );
  124.     } while ( cbRead );
  125. }
  126.  
  127. void write( void *dummy )
  128. {
  129.    ULONG cbRead, cbWritten;
  130.    CHAR achBuf[PIPESIZE];
  131.  
  132.    do {
  133.        DosRead( HF_STDIN, achBuf, sizeof(achBuf), &cbRead );
  134.        DosWrite( hp, achBuf, cbRead, &cbWritten );
  135.     } while ( cbRead );
  136. }
  137.  
  138.  
  139. main(int argc, char *argv[], char *envp[])
  140. {
  141.     CHAR ServerPipe[CCHMAXPATH];
  142.  
  143.     if ( argc != 2 ) return 0;
  144.  
  145.     strcpy( ServerPipe, "" );
  146.  
  147.     strcpy( ServerPipe, "\\\\" );
  148.     strcat( ServerPipe, argv[1] );
  149.  
  150.     strcat( ServerPipe, SERVER_PIPE_NAME );
  151.  
  152.     printf( "%s\n", ServerPipe );
  153.  
  154.     rc = DosOpen( ServerPipe, &hp, &ulAction, 0,
  155.                 FILE_NORMAL, FILE_OPEN,
  156.                 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE,
  157.                 NULL );
  158.  
  159.     if ( rc ) return 0;
  160.  
  161.     _beginthread( &write , NULL, 64000, NULL );
  162.     read( NULL );
  163.  
  164.     DosClose( hp );
  165.     return 0;
  166. }
  167.