File: ctrlpipe.c

package info (click to toggle)
saoimage 1.35.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,188 kB
  • ctags: 4,403
  • sloc: ansic: 52,848; makefile: 243; sh: 31
file content (92 lines) | stat: -rw-r--r-- 2,760 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	ctrlpipe.c (Control Pipe Device)
 * Purpose:	Open a pipe device for IO
 * Subroutine:	open_pipe()	returns: int
 * Subroutine:	close_pipe()	returns: void
 * Subroutine:	flush_pipe()	returns: void
 * Copyright:	1989 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		  25 May 1989
 *		{1} MVH simplified code	for modular strategy	10 March 1990
 *		{n} <who> -- <does what> -- <when>
 */

#ifndef VMS

#include <stdio.h>		/* define stderr, FILE, NULL, etc */

/*
 * Subroutine:	open_pipe
 * Purpose:	Open a channel on a named device for pipe I/O
 * Returns:	Channel number on success (0-64) else -1.
 * Note:	select_mask and mask_size are augmented, not initialized
 */
int open_pipe ( device_name, write_flag, flush_flag )
     char *device_name;		/* i: name of pipe device to open */
     int write_flag;		/* i: >0: write only, <=0: read */
     int flush_flag;		/* i: 1=flush, 0=don't flush */
{
  int ipc;			/* o: channel of new pipe connection */
  int open_disk();
  void fcntl_disk(), flush_disk(), close_disk();

  if( write_flag > 0 ) {
    int datain;
    /* open file to read (don't block) */
    if( (datain = open_disk(device_name, -1, 1)) == -1 )
      return( -1 );
    /* open same file to write (don't block) */
    if( (ipc = open_disk(device_name, 1, 1)) == -1 ) {
      close_disk(datain, device_name);
      return( -1 );
    }
    /* now enable blocking and close the reading connection */
    fcntl_disk(ipc, 1, 0, device_name);
    close_disk(datain, device_name);
  } else {
    /* open the device READ-WRITE, don't block - time_out on failure */
    if( (ipc = open_disk(device_name, 0, 1)) == -1 )
      return( -1 );
    if( flush_flag )
      /* if reading, flush old input, if any, with non-blocking reads */
      flush_disk(ipc, device_name);
    /* from now on block on read or write  */
    fcntl_disk(ipc, -1, 0, device_name);
  }
  return( ipc );
}

/*
 * Subroutine:	close_pipe
 * Purpose:	Close pipe connection
 */
void close_pipe ( ipc, device_name )
     int ipc;			/* i: channel number of open pipe */
     char *device_name;		/* i: name of pipe device */
{
  void close_disk();

  close_disk(ipc, device_name);
}

/*
 * Subroutine:	flush_pipe
 * Purpose:	Suck all bytes out of a pipe open for reading
 */
void flush_pipe ( ipc, filename )
     int ipc;
     char *filename;
{
  void flush_disk();

  flush_disk(ipc, filename);
}

#endif