4 * Simplified doublebuffered AmigaDOS IO.
6 * Author: Tomi Ollila <Tomi.Ollila@nsdi.fi>
8 * Copyright (c) 1994 Network Solutions Development Inc. OY
11 * Created: Thu Sep 15 06:11:55 1994 too
12 * Last modified: Thu Sep 15 08:33:18 1994 too
16 * Revision 1.1.1.2 2005/12/07 10:50:34 sonic_amiga
17 * First full import into the CVS
19 * Revision 1.1.1.1 2005/11/08 13:51:05 sonic_amiga
20 * Initial partial import upon request
22 * Revision 1.1 1996/01/21 01:26:36 jraja
27 #include <exec/types.h>
28 #include <exec/ports.h>
30 #include <dos/dosextens.h>
32 #include <string.h> /* memset() */
36 #include <inline/exec.h>
37 #include <inline/dos.h>
38 #error ios1 is SAS/C specific
40 #include <proto/exec.h>
41 #include <proto/dos.h>
42 #include <ios1.h> /* SAS/C, chkufb() */
49 extern int __io2errno(long);
52 initPktInfo(struct pktinfo
* pkti
, int fd
, char * buf1
, char * buf2
, int size
)
54 struct DosPacket
* pkt
;
55 struct MsgPort
* port
;
57 struct FileHandle
* fh
;
59 memset(pkti
, 0, sizeof(*pkti
));
62 * get AmigaDOS file handle from SAS/C file handle
65 if (ufb
== NULL
|| ufb
->ufbfh
== NULL
) {
69 /* convert DOS file handle to struct FileHandle * */
70 fh
= BADDR((BPTR
)ufb
->ufbfh
);
71 pkti
->fhType
= fh
->fh_Type
;
73 port
= CreateMsgPort();
77 pkt
= (struct DosPacket
*)AllocDosObject(DOS_STDPKT
, 0);
85 pkt
->dp_Arg1
= fh
->fh_Arg1
;
88 * Do the initial action. Do nothing if file is NIL:
90 if (pkti
->fhType
== NULL
) {
91 pkt
->dp_Res1
= 0; /* keep everyone happy */
93 AddTail(&port
->mp_MsgList
, (struct Node
*)pkt
->dp_Link
);
97 pkt
->dp_Type
= ACTION_WRITE
;
98 AddTail(&port
->mp_MsgList
, (struct Node
*)pkt
->dp_Link
);
101 pkt
->dp_Arg2
= (LONG
)buf1
;
103 pkt
->dp_Type
= ACTION_READ
;
104 SendPkt(pkt
, pkti
->fhType
, port
);
118 deInitPktInfo(struct pktinfo
* pkti
)
122 * Take the packet out of the port, save the return code
124 WaitPort(pkti
->port
);
125 rv
= pkti
->pkt
->dp_Res1
;
130 (pkti
->pkt
->dp_Type
== ACTION_WRITE
&& rv
< pkti
->pkt
->dp_Arg3
)) {
131 errno
= __io2errno(_OSERR
= pkti
->pkt
->dp_Res2
);
137 FreeDosObject(DOS_STDPKT
, pkti
->pkt
);
138 DeleteMsgPort(pkti
->port
);
140 memset(pkti
, 0, sizeof(*pkti
));
145 #define msgToPkt(msg) ((struct DosPacket *)msg->mn_Node.ln_Name)
148 readPkt(struct pktinfo
* pkti
, char ** buf
)
150 struct DosPacket
* pkt
;
154 * If file is NIL:, return 0 to end the reading
156 if (pkti
->fhType
== NULL
)
159 WaitPort(pkti
->port
);
160 pkt
= msgToPkt(GetMsg(pkti
->port
));
165 *buf
= pkti
->bufs
[pkti
->whichbuf
];
168 pkt
->dp_Arg2
= (LONG
)pkti
->bufs
[pkti
->whichbuf
];
169 SendPkt(pkt
, pkti
->fhType
, pkti
->port
);
173 errno
= __io2errno(_OSERR
= pkt
->dp_Res2
);
174 pkt
->dp_Res1
= 0; /* do not receive same error again */
175 AddTail(&pkti
->port
->mp_MsgList
, (struct Node
*)pkt
->dp_Link
);
182 writePkt(struct pktinfo
* pkti
, char ** buf
, int len
)
184 struct DosPacket
* pkt
;
186 WaitPort(pkti
->port
);
187 pkt
= msgToPkt(GetMsg(pkti
->port
));
189 if (pkt
->dp_Res1
< pkt
->dp_Arg3
) {
190 errno
= __io2errno(_OSERR
= pkt
->dp_Res2
);
191 pkt
->dp_Res1
= pkt
->dp_Arg3
= 0; /* do not receive same error again */
192 AddTail(&pkti
->port
->mp_MsgList
, (struct Node
*)pkt
->dp_Link
);
196 /* Assert (*buf == pkti->whichbuf); */
198 pkt
->dp_Arg2
= (LONG
)*buf
;
201 if (pkti
->fhType
!= NULL
)
202 SendPkt(pkt
, pkti
->fhType
, pkti
->port
);
204 pkt
->dp_Res1
= len
; /* writing to NIL: always succeeds */
205 AddTail(&pkti
->port
->mp_MsgList
, (struct Node
*)pkt
->dp_Link
);
209 *buf
= pkti
->bufs
[pkti
->whichbuf
];