home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Emulatory / AROS / dos / ungetc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1978-03-06  |  2.4 KB  |  107 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: ungetc.c,v 1.4 1996/10/24 15:50:38 aros Exp $
  4.     $Log: ungetc.c,v $
  5.     Revision 1.4  1996/10/24 15:50:38  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.3  1996/08/13 13:52:52  digulla
  9.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  10.     Replaced AROS_LA by AROS_LHA
  11.  
  12.     Revision 1.2  1996/08/01 17:40:58  digulla
  13.     Added standard header for all files
  14.  
  15.     Desc:
  16.     Lang: english
  17. */
  18. #include <clib/exec_protos.h>
  19. #include "dos_intern.h"
  20.  
  21. #ifndef EOF
  22. #define EOF -1
  23. #endif
  24.  
  25. /*****************************************************************************
  26.  
  27.     NAME */
  28.     #include <clib/dos_protos.h>
  29.  
  30.     AROS_LH2(LONG, UnGetC,
  31.  
  32. /*  SYNOPSIS */
  33.     AROS_LHA(BPTR, file,      D1),
  34.     AROS_LHA(LONG, character, D2),
  35.  
  36. /*  LOCATION */
  37.  
  38.     struct DosLibrary *, DOSBase, 53, Dos)
  39.  
  40. /*  FUNCTION
  41.     Push a character back into a read filehandle. If you've read
  42.     a character from that file you may always push at least 1 character
  43.     back. UnGetC(file,-1) ungets the last character read. This also
  44.     works for EOF.
  45.  
  46.     INPUTS
  47.     file      - Filehandle you've read from.
  48.     character - Character to push back or EOF.
  49.  
  50.     RESULT
  51.     !=0 if all went well, 0 if the character couldn't be pushed back.
  52.     IoErr() gives additional information in that case.
  53.  
  54.     NOTES
  55.  
  56.     EXAMPLE
  57.  
  58.     BUGS
  59.  
  60.     SEE ALSO
  61.     FGetC(), IoErr()
  62.  
  63.     INTERNALS
  64.  
  65.     HISTORY
  66.     29-10-95    digulla automatically created from
  67.                 dos_lib.fd and clib/dos_protos.h
  68.  
  69. *****************************************************************************/
  70. {
  71.     AROS_LIBFUNC_INIT
  72.     AROS_LIBBASE_EXT_DECL(struct DosLibrary *,DOSBase)
  73.  
  74.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  75.  
  76.     /* Get pointer to filehandle */
  77.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  78.  
  79.     /* If the file is in write mode there was nothing read recently */
  80.     if(fh->fh_Flags&FHF_WRITE)
  81.     {
  82.     *result=ERROR_SEEK_ERROR;
  83.     return 0;
  84.     }
  85.  
  86.     /* Unget EOF character if the last character read was an EOF */
  87.     if(character==EOF&&fh->fh_End==fh->fh_Buf)
  88.     {
  89.     fh->fh_Pos++;
  90.     return EOF;
  91.     }
  92.  
  93.     /* Test if I may unget a character on this file */
  94.     if(fh->fh_Pos==fh->fh_Buf)
  95.     {
  96.     *result=ERROR_SEEK_ERROR;
  97.     return 0;
  98.     }
  99.  
  100.     /* OK. Unget character and return. */
  101.     fh->fh_Pos--;
  102.     if(character!=EOF)
  103.     *fh->fh_Pos=character;
  104.     return character?character:1;
  105.     AROS_LIBFUNC_EXIT
  106. } /* UnGetC */
  107.