home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / ctrlpipe.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  3KB  |  93 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    ctrlpipe.c (Control Pipe Device)
  6.  * Purpose:    Open a pipe device for IO
  7.  * Subroutine:    open_pipe()    returns: int
  8.  * Subroutine:    close_pipe()    returns: void
  9.  * Subroutine:    flush_pipe()    returns: void
  10.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Modified:    {0} Michael VanHilst    initial version          25 May 1989
  17.  *        {1} MVH simplified code    for modular strategy    10 March 1990
  18.  *        {n} <who> -- <does what> -- <when>
  19.  */
  20.  
  21. #ifndef VMS
  22.  
  23. #include <stdio.h>        /* define stderr, FILE, NULL, etc */
  24.  
  25. /*
  26.  * Subroutine:    open_pipe
  27.  * Purpose:    Open a channel on a named device for pipe I/O
  28.  * Returns:    Channel number on success (0-64) else -1.
  29.  * Note:    select_mask and mask_size are augmented, not initialized
  30.  */
  31. int open_pipe ( device_name, write_flag, flush_flag )
  32.      char *device_name;        /* i: name of pipe device to open */
  33.      int write_flag;        /* i: >0: write only, <=0: read */
  34.      int flush_flag;        /* i: 1=flush, 0=don't flush */
  35. {
  36.   int ipc;            /* o: channel of new pipe connection */
  37.   int open_disk();
  38.   void fcntl_disk(), flush_disk(), close_disk();
  39.  
  40.   if( write_flag > 0 ) {
  41.     int datain;
  42.     /* open file to read (don't block) */
  43.     if( (datain = open_disk(device_name, -1, 1)) == -1 )
  44.       return( -1 );
  45.     /* open same file to write (don't block) */
  46.     if( (ipc = open_disk(device_name, 1, 1)) == -1 ) {
  47.       close_disk(datain, device_name);
  48.       return( -1 );
  49.     }
  50.     /* now enable blocking and close the reading connection */
  51.     fcntl_disk(ipc, 1, 0, device_name);
  52.     close_disk(datain, device_name);
  53.   } else {
  54.     /* open the device READ-WRITE, don't block - time_out on failure */
  55.     if( (ipc = open_disk(device_name, 0, 1)) == -1 )
  56.       return( -1 );
  57.     if( flush_flag )
  58.       /* if reading, flush old input, if any, with non-blocking reads */
  59.       flush_disk(ipc, device_name);
  60.     /* from now on block on read or write  */
  61.     fcntl_disk(ipc, -1, 0, device_name);
  62.   }
  63.   return( ipc );
  64. }
  65.  
  66. /*
  67.  * Subroutine:    close_pipe
  68.  * Purpose:    Close pipe connection
  69.  */
  70. void close_pipe ( ipc, device_name )
  71.      int ipc;            /* i: channel number of open pipe */
  72.      char *device_name;        /* i: name of pipe device */
  73. {
  74.   void close_disk();
  75.  
  76.   close_disk(ipc, device_name);
  77. }
  78.  
  79. /*
  80.  * Subroutine:    flush_pipe
  81.  * Purpose:    Suck all bytes out of a pipe open for reading
  82.  */
  83. void flush_pipe ( ipc, filename )
  84.      int ipc;
  85.      char *filename;
  86. {
  87.   void flush_disk();
  88.  
  89.   flush_disk(ipc, filename);
  90. }
  91.  
  92. #endif
  93.