home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / AsynchIO / asyncio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  20.7 KB  |  651 lines

  1.  
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <dos/dos.h>
  5. #include <dos/dosextens.h>
  6.  
  7. #include <clib/exec_protos.h>
  8. #include <clib/dos_protos.h>
  9.  
  10. #include <pragmas/exec_pragmas.h>
  11. #include <pragmas/dos_pragmas.h>
  12.  
  13. #include "asyncio.h"
  14.  
  15.  
  16. /*****************************************************************************/
  17.  
  18.  
  19. extern struct Library *DOSBase;
  20. extern struct Library *SysBase;
  21.  
  22.  
  23. /*****************************************************************************/
  24.  
  25.  
  26. /* this macro lets us long-align structures on the stack */
  27. #define D_S(type,name) char a_##name[sizeof(type)+3]; \
  28.                        type *name = (type *)((LONG)(a_##name+3) & ~3);
  29.  
  30.  
  31. /*****************************************************************************/
  32.  
  33.  
  34. /* send out an async packet to the file system. */
  35. static void SendPacket(AsyncFile *file, APTR arg2)
  36. {
  37.     file->af_Packet.sp_Pkt.dp_Port = &file->af_PacketPort;
  38.     file->af_Packet.sp_Pkt.dp_Arg2 = (LONG)arg2;
  39.     PutMsg(file->af_Handler, &file->af_Packet.sp_Msg);
  40.     file->af_PacketPending = TRUE;
  41. }
  42.  
  43.  
  44. /*****************************************************************************/
  45.  
  46.  
  47. /* this function waits for a packet to come back from the file system. If no
  48.  * packet is pending, state from the previous packet is returned. This ensures
  49.  * that once an error occurs, it state is maintained for the rest of the life
  50.  * of the file handle.
  51.  *
  52.  * This function also deals with IO errors, bringing up the needed DOS
  53.  * requesters to let the user retry an operation or cancel it.
  54.  */
  55. static LONG WaitPacket(AsyncFile *file)
  56. {
  57. LONG bytes;
  58.  
  59.     if (file->af_PacketPending)
  60.     {
  61.         while (TRUE)
  62.         {
  63.             /* This enables signalling when a packet comes back to the port */
  64.             file->af_PacketPort.mp_Flags = PA_SIGNAL;
  65.  
  66.             /* Wait for the packet to come back, and remove it from the message
  67.              * list. Since we know no other packets can come in to the port, we can
  68.              * safely use Remove() instead of GetMsg(). If other packets could come in,
  69.              * we would have to use GetMsg(), which correctly arbitrates access in such
  70.              * a case
  71.              */
  72.             Remove((struct Node *)WaitPort(&file->af_PacketPort));
  73.  
  74.             /* set the port type back to PA_IGNORE so we won't be bothered with
  75.              * spurious signals
  76.              */
  77.             file->af_PacketPort.mp_Flags = PA_IGNORE;
  78.  
  79.             /* mark packet as no longer pending since we removed it */
  80.             file->af_PacketPending = FALSE;
  81.  
  82.             bytes = file->af_Packet.sp_Pkt.dp_Res1;
  83.             if (bytes >= 0)
  84.             {
  85.                 /* packet didn't report an error, so bye... */
  86.                 return(bytes);
  87.             }
  88.  
  89.             /* see if the user wants to try again... */
  90.             if (ErrorReport(file->af_Packet.sp_Pkt.dp_Res2,REPORT_STREAM,file->af_File,NULL))
  91.                 return(-1);
  92.  
  93.             /* user wants to try again, resend the packet */
  94.             if (file->af_ReadMode)
  95.                 SendPacket(file,file->af_Buffers[file->af_CurrentBuf]);
  96.             else
  97.                 SendPacket(file,file->af_Buffers[1 - file->af_CurrentBuf]);
  98.          }
  99.     }
  100.  
  101.     /* last packet's error code, or 0 if packet was never sent */
  102.     SetIoErr(file->af_Packet.sp_Pkt.dp_Res2);
  103.  
  104.     return(file->af_Packet.sp_Pkt.dp_Res1);
  105. }
  106.  
  107.  
  108. /*****************************************************************************/
  109.  
  110.  
  111. /* this function puts the packet back on the message list of our
  112.  * message port.
  113.  */
  114. static void RequeuePacket(AsyncFile *file)
  115. {
  116.     AddHead(&file->af_PacketPort.mp_MsgList,&file->af_Packet.sp_Msg.mn_Node);
  117.     file->af_PacketPending = TRUE;
  118. }
  119.  
  120.  
  121. /*****************************************************************************/
  122.  
  123.  
  124. /* this function records a failure from a synchronous DOS call into the
  125.  * packet so that it gets picked up by the other IO routines in this module
  126.  */
  127. static void RecordSyncFailure(AsyncFile *file)
  128. {
  129.     file->af_Packet.sp_Pkt.dp_Res1 = -1;
  130.     file->af_Packet.sp_Pkt.dp_Res2 = IoErr();
  131. }
  132.  
  133.  
  134. /*****************************************************************************/
  135.  
  136.  
  137. AsyncFile *OpenAsync(const STRPTR fileName, OpenModes mode, LONG bufferSize)
  138. {
  139. AsyncFile         *file;
  140. struct FileHandle *fh;
  141. BPTR               handle;
  142. BPTR               lock;
  143. LONG               blockSize;
  144. D_S(struct InfoData,infoData);
  145.  
  146.     handle = NULL;
  147.     file   = NULL;
  148.     lock   = NULL;
  149.  
  150.     if (mode == MODE_READ)
  151.     {
  152.         if (handle = Open(fileName,MODE_OLDFILE))
  153.             lock = Lock(fileName,ACCESS_READ);
  154.     }
  155.     else
  156.     {
  157.         if (mode == MODE_WRITE)
  158.         {
  159.             handle = Open(fileName,MODE_NEWFILE);
  160.         }
  161.         else if (mode == MODE_APPEND)
  162.         {
  163.             /* in append mode, we open for writing, and then seek to the
  164.              * end of the file. That way, the initial write will happen at
  165.              * the end of the file, thus extending it
  166.              */
  167.  
  168.             if (handle = Open(fileName,MODE_READWRITE))
  169.             {
  170.                 if (Seek(handle,0,OFFSET_END) < 0)
  171.                 {
  172.                     Close(handle);
  173.                     handle = NULL;
  174.                 }
  175.             }
  176.         }
  177.  
  178.         /* we want a lock on the same device as where the file is. We can't
  179.          * use DupLockFromFH() for a write-mode file though. So we get sneaky
  180.          * and get a lock on the parent of the file
  181.          */
  182.         if (handle)
  183.             lock = ParentOfFH(handle);
  184.     }
  185.  
  186.     if (handle)
  187.     {
  188.         /* if it was possible to obtain a lock on the same device as the
  189.          * file we're working on, get the block size of that device and
  190.          * round up our buffer size to be a multiple of the block size.
  191.          * This maximizes DMA efficiency.
  192.          */
  193.  
  194.         blockSize = 512;
  195.         if (lock)
  196.         {
  197.             if (Info(lock,infoData))
  198.             {
  199.                 blockSize  = infoData->id_BytesPerBlock;
  200.                 bufferSize = (((bufferSize + (blockSize*2) - 1) / (blockSize*2)) * (blockSize*2));
  201.             }
  202.             UnLock(lock);
  203.         }
  204.  
  205.         /* now allocate the ASyncFile structure, as well as the read buffers.
  206.          * Add 15 bytes to the total size in order to allow for later
  207.          * quad-longword alignement of the buffers
  208.          */
  209.  
  210.         if (file = AllocVec(sizeof(AsyncFile) + bufferSize + 15,MEMF_PUBLIC | MEMF_ANY))
  211.         {
  212.             file->af_File      = handle;
  213.             file->af_ReadMode  = (mode == MODE_READ);
  214.             file->af_BlockSize = blockSize;
  215.  
  216.             /* initialize the ASyncFile structure. We do as much as we can here,
  217.              * in order to avoid doing it in more critical sections
  218.              *
  219.              * Note how the two buffers used are quad-longword aligned. This
  220.              * helps performance on 68040 systems with copyback cache. Aligning
  221.              * the data avoids a nasty side-effect of the 040 caches on DMA.
  222.              * Not aligning the data causes the device driver to have to do
  223.              * some magic to avoid the cache problem. This magic will generally
  224.              * involve flushing the CPU caches. This is very costly on an 040.
  225.              * Aligning things avoids the need for magic, at the cost of at
  226.              * most 15 bytes of ram.
  227.              */
  228.  
  229.             fh                     = BADDR(file->af_File);
  230.             file->af_Handler       = fh->fh_Type;
  231.             file->af_BufferSize    = bufferSize / 2;
  232.             file->af_Buffers[0]    = (APTR)(((ULONG)file + sizeof(AsyncFile) + 15) & 0xfffffff0);
  233.             file->af_Buffers[1]    = (APTR)((ULONG)file->af_Buffers[0] + file->af_BufferSize);
  234.             file->af_Offset        = file->af_Buffers[0];
  235.             file->af_CurrentBuf    = 0;
  236.             file->af_SeekOffset    = 0;
  237.             file->af_PacketPending = FALSE;
  238.  
  239.             /* this is the port used to get the packets we send out back.
  240.              * It is initialized to PA_IGNORE, which means that no signal is
  241.              * generated when a message comes in to the port. The signal bit
  242.              * number is initialized to SIGB_SINGLE, which is the special bit
  243.              * that can be used for one-shot signalling. The signal will never
  244.              * be set, since the port is of type PA_IGNORE. We'll change the
  245.              * type of the port later on to PA_SIGNAL whenever we need to wait
  246.              * for a message to come in.
  247.              *
  248.              * The trick used here avoids the need to allocate an extra signal
  249.              * bit for the port. It is quite efficient.
  250.              */
  251.  
  252.             file->af_PacketPort.mp_MsgList.lh_Head     = (struct Node *)&file->af_PacketPort.mp_MsgList.lh_Tail;
  253.             file->af_PacketPort.mp_MsgList.lh_Tail     = NULL;
  254.             file->af_PacketPort.mp_MsgList.lh_TailPred = (struct Node *)&file->af_PacketPort.mp_MsgList.lh_Head;
  255.             file->af_PacketPort.mp_Node.ln_Type        = NT_MSGPORT;
  256.             file->af_PacketPort.mp_Flags               = PA_IGNORE;
  257.             file->af_PacketPort.mp_SigBit              = SIGB_SINGLE;
  258.             file->af_PacketPort.mp_SigTask             = FindTask(NULL);
  259.  
  260.             file->af_Packet.sp_Pkt.dp_Link          = &file->af_Packet.sp_Msg;
  261.             file->af_Packet.sp_Pkt.dp_Arg1          = fh->fh_Arg1;
  262.             file->af_Packet.sp_Pkt.dp_Arg3          = file->af_BufferSize;
  263.             file->af_Packet.sp_Pkt.dp_Res1          = 0;
  264.             file->af_Packet.sp_Pkt.dp_Res2          = 0;
  265.             file->af_Packet.sp_Msg.mn_Node.ln_Name  = (STRPTR)&file->af_Packet.sp_Pkt;
  266.             file->af_Packet.sp_Msg.mn_Node.ln_Type  = NT_MESSAGE;
  267.             file->af_Packet.sp_Msg.mn_Length        = sizeof(struct StandardPacket);
  268.  
  269.             if (mode == MODE_READ)
  270.             {
  271.                 /* if we are in read mode, send out the first read packet to
  272.                  * the file system. While the application is getting ready to
  273.                  * read data, the file system will happily fill in this buffer
  274.                  * with DMA transfers, so that by the time the application
  275.                  * needs the data, it will be in the buffer waiting
  276.                  */
  277.  
  278.                 file->af_Packet.sp_Pkt.dp_Type = ACTION_READ;
  279.                 file->af_BytesLeft             = 0;
  280.                 if (file->af_Handler)
  281.                     SendPacket(file,file->af_Buffers[0]);
  282.             }
  283.             else
  284.             {
  285.                 file->af_Packet.sp_Pkt.dp_Type = ACTION_WRITE;
  286.                 file->af_BytesLeft             = file->af_BufferSize;
  287.             }
  288.         }
  289.         else
  290.         {
  291.             Close(handle);
  292.         }
  293.     }
  294.  
  295.     return(file);
  296. }
  297.  
  298.  
  299. /*****************************************************************************/
  300.  
  301.  
  302. LONG CloseAsync(AsyncFile *file)
  303. {
  304. LONG result;
  305.  
  306.     if (file)
  307.     {
  308.         result = WaitPacket(file);
  309.         if (result >= 0)
  310.         {
  311.             if (!file->af_ReadMode)
  312.             {
  313.                 /* this will flush out any pending data in the write buffer */
  314.                 if (file->af_BufferSize > file->af_BytesLeft)
  315.                     result = Write(file->af_File,file->af_Buffers[file->af_CurrentBuf],file->af_BufferSize - file->af_BytesLeft);
  316.             }
  317.         }
  318.  
  319.         Close(file->af_File);
  320.         FreeVec(file);
  321.     }
  322.     else
  323.     {
  324.         SetIoErr(ERROR_INVALID_LOCK);
  325.         result = -1;
  326.     }
  327.  
  328.     return(result);
  329. }
  330.  
  331.  
  332. /*****************************************************************************/
  333.  
  334.  
  335. LONG ReadAsync(AsyncFile *file, APTR buffer, LONG numBytes)
  336. {
  337. LONG totalBytes;
  338. LONG bytesArrived;
  339.  
  340.     totalBytes = 0;
  341.  
  342.     /* if we need more bytes than there are in the current buffer, enter the
  343.      * read loop
  344.      */
  345.  
  346.     while (numBytes > file->af_BytesLeft)
  347.     {
  348.         /* drain buffer */
  349.         CopyMem(file->af_Offset,buffer,file->af_BytesLeft);
  350.  
  351.         numBytes           -= file->af_BytesLeft;
  352.         buffer              = (APTR)((ULONG)buffer + file->af_BytesLeft);
  353.         totalBytes         += file->af_BytesLeft;
  354.         file->af_BytesLeft  = 0;
  355.  
  356.         bytesArrived = WaitPacket(file);
  357.         if (bytesArrived <= 0)
  358.         {
  359.             if (bytesArrived == 0)
  360.                 return(totalBytes);
  361.  
  362.             return(-1);
  363.         }
  364.  
  365.         /* ask that the buffer be filled */
  366.         SendPacket(file,file->af_Buffers[1-file->af_CurrentBuf]);
  367.  
  368.         if (file->af_SeekOffset > bytesArrived)
  369.             file->af_SeekOffset = bytesArrived;
  370.  
  371.         file->af_Offset      = (APTR)((ULONG)file->af_Buffers[file->af_CurrentBuf] + file->af_SeekOffset);
  372.         file->af_CurrentBuf  = 1 - file->af_CurrentBuf;
  373.         file->af_BytesLeft   = bytesArrived - file->af_SeekOffset;
  374.         file->af_SeekOffset  = 0;
  375.     }
  376.  
  377.     CopyMem(file->af_Offset,buffer,numBytes);
  378.     file->af_BytesLeft -= numBytes;
  379.     file->af_Offset     = (APTR)((ULONG)file->af_Offset + numBytes);
  380.  
  381.     return (totalBytes + numBytes);
  382. }
  383.  
  384.  
  385. /*****************************************************************************/
  386.  
  387.  
  388. LONG ReadCharAsync(AsyncFile *file)
  389. {
  390. unsigned char ch;
  391.  
  392.     if (file->af_BytesLeft)
  393.     {
  394.         /* if there is at least a byte left in the current buffer, get it
  395.          * directly. Also update all counters
  396.          */
  397.  
  398.         ch = *(char *)file->af_Offset;
  399.         file->af_BytesLeft--;
  400.         file->af_Offset = (APTR)((ULONG)file->af_Offset + 1);
  401.  
  402.         return((LONG)ch);
  403.     }
  404.  
  405.     /* there were no characters in the current buffer, so call the main read
  406.      * routine. This has the effect of sending a request to the file system to
  407.      * have the current buffer refilled. After that request is done, the
  408.      * character is extracted for the alternate buffer, which at that point
  409.      * becomes the "current" buffer
  410.      */
  411.  
  412.     if (ReadAsync(file,&ch,1) > 0)
  413.         return((LONG)ch);
  414.  
  415.     /* We couldn't read above, so fail */
  416.  
  417.     return(-1);
  418. }
  419.  
  420.  
  421. /*****************************************************************************/
  422.  
  423.  
  424. LONG WriteAsync(AsyncFile *file, APTR buffer, LONG numBytes)
  425. {
  426. LONG totalBytes;
  427.  
  428.     totalBytes = 0;
  429.  
  430.     while (numBytes > file->af_BytesLeft)
  431.     {
  432.         /* this takes care of NIL: */
  433.         if (!file->af_Handler)
  434.         {
  435.             file->af_Offset    = file->af_Buffers[0];
  436.             file->af_BytesLeft = file->af_BufferSize;
  437.             return(numBytes);
  438.         }
  439.  
  440.         if (file->af_BytesLeft)
  441.         {
  442.             CopyMem(buffer,file->af_Offset,file->af_BytesLeft);
  443.  
  444.             numBytes   -= file->af_BytesLeft;
  445.             buffer      = (APTR)((ULONG)buffer + file->af_BytesLeft);
  446.             totalBytes += file->af_BytesLeft;
  447.         }
  448.  
  449.         if (WaitPacket(file) < 0)
  450.             return(-1);
  451.  
  452.         /* send the current buffer out to disk */
  453.         SendPacket(file,file->af_Buffers[file->af_CurrentBuf]);
  454.  
  455.         file->af_CurrentBuf = 1 - file->af_CurrentBuf;
  456.         file->af_Offset     = file->af_Buffers[file->af_CurrentBuf];
  457.         file->af_BytesLeft  = file->af_BufferSize;
  458.     }
  459.  
  460.     CopyMem(buffer,file->af_Offset,numBytes);
  461.     file->af_BytesLeft -= numBytes;
  462.     file->af_Offset     = (APTR)((ULONG)file->af_Offset + numBytes);
  463.  
  464.     return (totalBytes + numBytes);
  465. }
  466.  
  467.  
  468. /*****************************************************************************/
  469.  
  470.  
  471. LONG WriteCharAsync(AsyncFile *file, UBYTE ch)
  472. {
  473.     if (file->af_BytesLeft)
  474.     {
  475.         /* if there's any room left in the current buffer, directly write
  476.          * the byte into it, updating counters and stuff.
  477.          */
  478.  
  479.         *(UBYTE *)file->af_Offset = ch;
  480.         file->af_BytesLeft--;
  481.         file->af_Offset = (APTR)((ULONG)file->af_Offset + 1);
  482.  
  483.         /* one byte written */
  484.         return(1);
  485.     }
  486.  
  487.     /* there was no room in the current buffer, so call the main write
  488.      * routine. This will effectively send the current buffer out to disk,
  489.      * wait for the other buffer to come back, and then put the byte into
  490.      * it.
  491.      */
  492.  
  493.     return(WriteAsync(file,&ch,1));
  494. }
  495.  
  496.  
  497. /*****************************************************************************/
  498.  
  499.  
  500. LONG SeekAsync(AsyncFile *file, LONG position, SeekModes mode)
  501. {
  502. LONG  current, target;
  503. LONG  minBuf, maxBuf;
  504. LONG  bytesArrived;
  505. LONG  diff;
  506. LONG  filePos;
  507. LONG  roundTarget;
  508. D_S(struct FileInfoBlock,fib);
  509.  
  510.     bytesArrived = WaitPacket(file);
  511.  
  512.     if (bytesArrived < 0)
  513.         return(-1);
  514.  
  515.     if (file->af_ReadMode)
  516.     {
  517.         /* figure out what the actual file position is */
  518.         filePos = Seek(file->af_File,0,OFFSET_CURRENT);
  519.         if (filePos < 0)
  520.         {
  521.             RecordSyncFailure(file);
  522.             return(-1);
  523.         }
  524.  
  525.         /* figure out what the caller's file position is */
  526.         current = filePos - (file->af_BytesLeft+bytesArrived) + file->af_SeekOffset;
  527.         file->af_SeekOffset = 0;
  528.  
  529.         /* figure out the absolute offset within the file where we must seek to */
  530.         if (mode == MODE_CURRENT)
  531.         {
  532.             target = current + position;
  533.         }
  534.         else if (mode == MODE_START)
  535.         {
  536.             target = position;
  537.         }
  538.         else /* if (mode == MODE_END) */
  539.         {
  540.             if (!ExamineFH(file->af_File,fib))
  541.             {
  542.                 RecordSyncFailure(file);
  543.                 return(-1);
  544.             }
  545.  
  546.             target = fib->fib_Size + position;
  547.         }
  548.  
  549.         /* figure out what range of the file is currently in our buffers */
  550.         minBuf = current - (LONG)((ULONG)file->af_Offset - (ULONG)file->af_Buffers[file->af_CurrentBuf]);
  551.         maxBuf = current + file->af_BytesLeft + bytesArrived;  /* WARNING: this is one too big */
  552.  
  553.         diff = target - current;
  554.  
  555.         if ((target < minBuf) || (target >= maxBuf))
  556.         {
  557.             /* the target seek location isn't currently in our buffers, so
  558.              * move the actual file pointer to the desired location, and then
  559.              * restart the async read thing...
  560.              */
  561.  
  562.             /* this is to keep our file reading block-aligned on the device.
  563.              * block-aligned reads are generally quite a bit faster, so it is
  564.              * worth the trouble to keep things aligned
  565.              */
  566.             roundTarget = (target / file->af_BlockSize) * file->af_BlockSize;
  567.  
  568.             if (Seek(file->af_File,roundTarget-filePos,OFFSET_CURRENT) < 0)
  569.             {
  570.                 RecordSyncFailure(file);
  571.                 return(-1);
  572.             }
  573.  
  574.             SendPacket(file,file->af_Buffers[0]);
  575.  
  576.             file->af_SeekOffset = target-roundTarget;
  577.             file->af_BytesLeft  = 0;
  578.             file->af_CurrentBuf = 0;
  579.             file->af_Offset     = file->af_Buffers[0];
  580.         }
  581.         else if ((target < current) || (diff <= file->af_BytesLeft))
  582.         {
  583.             /* one of the two following things is true:
  584.              *
  585.              * 1. The target seek location is within the current read buffer,
  586.              * but before the current location within the buffer. Move back
  587.              * within the buffer and pretend we never got the pending packet,
  588.              * just to make life easier, and faster, in the read routine.
  589.              *
  590.              * 2. The target seek location is ahead within the current
  591.              * read buffer. Advance to that location. As above, pretend to
  592.              * have never received the pending packet.
  593.              */
  594.  
  595.             RequeuePacket(file);
  596.  
  597.             file->af_BytesLeft -= diff;
  598.             file->af_Offset     = (APTR)((ULONG)file->af_Offset + diff);
  599.         }
  600.         else
  601.         {
  602.             /* at this point, we know the target seek location is within
  603.              * the buffer filled in by the packet that we just received
  604.              * at the start of this function. Throw away all the bytes in the
  605.              * current buffer, send a packet out to get the async thing going
  606.              * again, readjust buffer pointers to the seek location, and return
  607.              * with a grin on your face... :-)
  608.              */
  609.  
  610.             diff -= file->af_BytesLeft;
  611.  
  612.             SendPacket(file,file->af_Buffers[file->af_CurrentBuf]);
  613.  
  614.             file->af_Offset    = (APTR)((ULONG)file->af_Buffers[file->af_CurrentBuf] + diff);
  615.             file->af_BytesLeft = bytesArrived - diff;
  616.         }
  617.     }
  618.     else
  619.     {
  620.         if (file->af_BufferSize > file->af_BytesLeft)
  621.         {
  622.             if (Write(file->af_File,file->af_Buffers[file->af_CurrentBuf],file->af_BufferSize - file->af_BytesLeft) < 0)
  623.             {
  624.                 RecordSyncFailure(file);
  625.                 return(-1);
  626.             }
  627.         }
  628.  
  629.         /* this will unfortunately generally result in non block-aligned file
  630.          * access. We could be sneaky and try to resync our file pos at a
  631.          * later time, but we won't bother. Seeking in write-only files is
  632.          * relatively rare (except when writing IFF files with unknown chunk
  633.          * sizes, where the chunk size has to be written after the chunk data)
  634.          */
  635.  
  636.         current = Seek(file->af_File,position,mode);
  637.  
  638.         if (current < 0)
  639.         {
  640.             RecordSyncFailure(file);
  641.             return(-1);
  642.         }
  643.  
  644.         file->af_BytesLeft  = file->af_BufferSize;
  645.         file->af_CurrentBuf = 0;
  646.         file->af_Offset     = file->af_Buffers[0];
  647.     }
  648.  
  649.     return(current);
  650. }
  651.