1 /****************************************************************************
3 ** Program: pipe-handler - an AmigaDOS handler for named pipes
5 ** Author: Ed Puckett qix@mit-oz
7 ** Copyright 1987 by EpAc Software. All Rights Reserved.
9 ** History: 05-Jan-87 Original Version (1.0)
12 #include <libraries/dos.h>
13 #include <libraries/dosextens.h>
14 #include <libraries/filehandler.h>
15 #include <exec/exec.h>
17 #include "pipelists.h"
20 #include "pipecreate.h"
21 #include "pipesched.h"
22 #include "pipe-handler.h"
26 /*---------------------------------------------------------------------------
29 ** This module contains functions that manage the circular buffer for pipes.
33 ** PIPEBUF *AllocPipebuf (len)
34 ** ULONG MoveFromPipebuf (pb, dest, amt)
35 ** ULONG MoveToPipebuf (pb, src, amt)
37 ** Macros (in pipebuf.h)
38 ** ---------------------
50 /*---------------------------------------------------------------------------
51 ** AllocPipebuf() returns a pointer to a new PIPEBUF structure if there is
52 ** enough free memory to allocate one with the requested ("len") storage.
53 ** The structure is iinitialized as empty. Notice that the buffer storage
54 ** area is the tail part of the structure.
57 PIPEBUF
*AllocPipebuf (len
)
64 if ( (len
> 0) && (len
<= MAX_PIPELEN
) &&
65 ((pb
= (PIPEBUF
*) AllocMem (sizeof (PIPEBUF
) - 1 + len
, ALLOCMEM_FLAGS
)) != NULL
) )
66 { pb
->head
= pb
->tail
= 0;
76 /*---------------------------------------------------------------------------
77 ** Move bytes from the PIPEBUF to the memory pointed to by "dest". At most
78 ** "amt" bytes are moved. The actual number moved is returned.
81 ULONG
MoveFromPipebuf (pb
, dest
, amt
)
92 if ((amt
<= 0) || PipebufEmpty (pb
))
96 src
= pb
->buf
+ pb
->tail
;
98 if (pb
->tail
>= pb
->head
) /* then have to wrap around */
99 { if ((ct
= (pb
->len
- pb
->tail
)) > amtleft
)
100 ct
= amtleft
; /* more than needed in end of pipebuf */
102 CopyMem (src
, dest
, ct
);
103 pb
->tail
= (pb
->tail
+ ct
) % pb
->len
;
106 src
= pb
->buf
+ pb
->tail
;
110 if ( (amtleft
> 0) && (ct
= (pb
->head
- pb
->tail
)) )
112 ct
= amtleft
; /* more than needed */
114 CopyMem (src
, dest
, ct
);
115 pb
->tail
+= ct
; /* no need to mod */
119 pb
->full
= FALSE
; /* has to be: nonzero amt */
121 return (amt
- amtleft
);
126 /*---------------------------------------------------------------------------
127 ** Move bytes to the PIPEBUF from the memory pointed to by "src". At most
128 ** "amt" bytes are moved. The actual number moved is returned.
131 ULONG
MoveToPipebuf (pb
, src
, amt
)
137 { register BYTE
*dest
;
142 if ((amt
<= 0) || PipebufFull (pb
))
146 dest
= pb
->buf
+ pb
->head
;
148 if (pb
->head
>= pb
->tail
) /* then have to wrap around */
149 { if ((ct
= (pb
->len
- pb
->head
)) > amtleft
)
150 ct
= amtleft
; /* more than will fit in end of pipebuf */
152 CopyMem (src
, dest
, ct
);
153 pb
->head
= (pb
->head
+ ct
) % pb
->len
;
157 dest
= pb
->buf
+ pb
->head
;
160 if ( (amtleft
> 0) && (ct
= (pb
->tail
- pb
->head
)) )
162 ct
= amtleft
; /* more than will fit */
164 CopyMem (src
, dest
, ct
);
165 pb
->head
+= ct
; /* no need to mod */
169 pb
->full
= (pb
->head
== pb
->tail
);
171 return (amt
- amtleft
);