home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / dos / fputc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  3.2 KB  |  148 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fputc.c,v 1.7 1997/01/27 00:36:20 ldp Exp $
  4.     $Log: fputc.c,v $
  5.     Revision 1.7  1997/01/27 00:36:20  ldp
  6.     Polish
  7.  
  8.     Revision 1.6  1996/12/09 13:53:28  aros
  9.     Added empty templates for all missing functions
  10.  
  11.     Moved #include's into first column
  12.  
  13.     Revision 1.5  1996/10/24 15:50:29  aros
  14.     Use the official AROS macros over the __AROS versions.
  15.  
  16.     Revision 1.4  1996/08/13 13:52:46  digulla
  17.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  18.     Replaced AROS_LA by AROS_LHA
  19.  
  20.     Revision 1.3  1996/08/12 14:22:57  digulla
  21.     Removed irritating empty line
  22.  
  23.     Revision 1.2  1996/08/01 17:40:51  digulla
  24.     Added standard header for all files
  25.  
  26.     Desc:
  27.     Lang: english
  28. */
  29. #include <exec/memory.h>
  30. #include <proto/exec.h>
  31. #include <dos/dosextens.h>
  32. #include "dos_intern.h"
  33.  
  34. /*****************************************************************************
  35.  
  36.     NAME */
  37. #include <proto/dos.h>
  38.  
  39.     AROS_LH2(LONG, FPutC,
  40.  
  41. /*  SYNOPSIS */
  42.     AROS_LHA(BPTR, file,      D1),
  43.     AROS_LHA(LONG, character, D2),
  44.  
  45. /*  LOCATION */
  46.     struct DosLibrary *, DOSBase, 52, Dos)
  47.  
  48. /*  FUNCTION
  49.  
  50.     INPUTS
  51.     file      - Filehandle to write to.
  52.     character - Character to write.
  53.  
  54.     RESULT
  55.     The character written or EOF in case of an error.
  56.     IoErr() gives additional information in that case.
  57.  
  58.     NOTES
  59.  
  60.     EXAMPLE
  61.  
  62.     BUGS
  63.  
  64.     SEE ALSO
  65.     FGetC(), IoErr()
  66.  
  67.     INTERNALS
  68.  
  69.     HISTORY
  70.     29-10-95    digulla automatically created from
  71.                 dos_lib.fd and clib/dos_protos.h
  72.  
  73. *****************************************************************************/
  74. {
  75.     AROS_LIBFUNC_INIT
  76.     AROS_LIBBASE_EXT_DECL(struct DosLibrary *,DOSBase)
  77.  
  78.     /* Get pointer to result. */
  79.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  80.  
  81.     /* Get pointer to filehandle */
  82.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  83.  
  84.     /* Check if file is in write mode */
  85.     if(!(fh->fh_Flags&FHF_WRITE))
  86.     {
  87.     if(fh->fh_Pos<fh->fh_End)
  88.         /* Read mode. Try to seek back to the current position. */
  89.         if(Seek(file,fh->fh_Pos-fh->fh_End,OFFSET_CURRENT)<0)
  90.         {
  91.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  92.         return EOF;
  93.         }
  94.  
  95.     /* Is there a buffer? */
  96.     if(fh->fh_Buf==NULL)
  97.     {
  98.         /* No. Get one. */
  99.         fh->fh_Buf=AllocMem(IOBUFSIZE,MEMF_ANY);
  100.         if(fh->fh_Buf==NULL)
  101.         {
  102.         /* Couldn't get buffer. Return error. */
  103.         *result=ERROR_NO_FREE_STORE;
  104.         return EOF;
  105.         }
  106.         /* Got it. Use it. */
  107.         fh->fh_Flags|=FHF_BUF;
  108.         fh->fh_Size=IOBUFSIZE;
  109.     }
  110.  
  111.     /* Prepare buffer */
  112.     fh->fh_Pos=fh->fh_Buf;
  113.     fh->fh_End=fh->fh_Buf+fh->fh_Size;
  114.     fh->fh_Flags|=FHF_WRITE;
  115.     }
  116.  
  117.     /* Check if there is still some space in the buffer */
  118.     if(fh->fh_Pos>=fh->fh_End)
  119.     {
  120.     UBYTE *pos;
  121.  
  122.     /* Write the data. (In many pieces if the first one isn't enough). */
  123.     pos=fh->fh_Buf;
  124.     while(pos!=fh->fh_Pos)
  125.     {
  126.         LONG size;
  127.         size=Write(file,pos,fh->fh_Pos-pos);
  128.  
  129.         /* An error happened? No success. */
  130.         if(size<0)
  131.         {
  132.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  133.         fh->fh_Flags&=~FHF_WRITE;
  134.         return EOF;
  135.         }
  136.         pos+=size;
  137.     }
  138.  
  139.     /* Reset buffer */
  140.     fh->fh_Pos=fh->fh_Buf;
  141.     }
  142.  
  143.     /* Write data and return */
  144.     *fh->fh_Pos++=character;
  145.     return character;
  146.     AROS_LIBFUNC_EXIT
  147. } /* FPutC */
  148.