Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* Copyright (C) 2009 Eric Blake
  2.  * Permission to use, copy, modify, and distribute this software
  3.  * is freely granted, provided that this notice is preserved.
  4.  */
  5.  
  6. /*
  7. FUNCTION
  8. <<fpurge>>---discard pending file I/O
  9.  
  10. INDEX
  11.         fpurge
  12. INDEX
  13.         _fpurge_r
  14. INDEX
  15.         __fpurge
  16.  
  17. ANSI_SYNOPSIS
  18.         #include <stdio.h>
  19.         int fpurge(FILE *<[fp]>);
  20.  
  21.         int _fpurge_r(struct _reent *<[reent]>, FILE *<[fp]>);
  22.  
  23.         #include <stdio.h>
  24.         #include <stdio_ext.h>
  25.         void  __fpurge(FILE *<[fp]>);
  26.  
  27.  
  28. DESCRIPTION
  29. Use <<fpurge>> to clear all buffers of the given stream.  For output
  30. streams, this discards data not yet written to disk.  For input streams,
  31. this discards any data from <<ungetc>> and any data retrieved from disk
  32. but not yet read via <<getc>>.  This is more severe than <<fflush>>,
  33. and generally is only needed when manually altering the underlying file
  34. descriptor of a stream.
  35.  
  36. <<__fpurge>> behaves exactly like <<fpurge>> but does not return a value.
  37.  
  38. The alternate function <<_fpurge_r>> is a reentrant version, where the
  39. extra argument <[reent]> is a pointer to a reentrancy structure, and
  40. <[fp]> must not be NULL.
  41.  
  42. RETURNS
  43. <<fpurge>> returns <<0>> unless <[fp]> is not valid, in which case it
  44. returns <<EOF>> and sets <<errno>>.
  45.  
  46. PORTABILITY
  47. These functions are not portable to any standard.
  48.  
  49. No supporting OS subroutines are required.
  50. */
  51.  
  52. #include <_ansi.h>
  53. #include <stdio.h>
  54. #ifndef __rtems__
  55. #include <stdio_ext.h>
  56. #endif
  57. #include <errno.h>
  58. #include "local.h"
  59.  
  60. /* Discard I/O from a single file.  */
  61.  
  62. int
  63. _DEFUN(_fpurge_r, (ptr, fp),
  64.        struct _reent *ptr _AND
  65.        register FILE * fp)
  66. {
  67.   int t;
  68.  
  69.   CHECK_INIT (ptr, fp);
  70.  
  71.   _newlib_flockfile_start (fp);
  72.  
  73.   t = fp->_flags;
  74.   if (!t)
  75.     {
  76.       ptr->_errno = EBADF;
  77.       _newlib_flockfile_exit (fp);
  78.       return EOF;
  79.     }
  80.   fp->_p = fp->_bf._base;
  81.   if ((t & __SWR) == 0)
  82.     {
  83.       fp->_r = 0;
  84.       if (HASUB (fp))
  85.         FREEUB (ptr, fp);
  86.     }
  87.   else
  88.     fp->_w = t & (__SLBF | __SNBF) ? 0 : fp->_bf._size;
  89.   _newlib_flockfile_end (fp);
  90.   return 0;
  91. }
  92.  
  93. #ifndef _REENT_ONLY
  94.  
  95. int
  96. _DEFUN(fpurge, (fp),
  97.        register FILE * fp)
  98. {
  99.   return _fpurge_r (_REENT, fp);
  100. }
  101.  
  102. #ifndef __rtems__
  103.  
  104. void
  105. _DEFUN(__fpurge, (fp),
  106.        register FILE * fp)
  107. {
  108.   _fpurge_r (_REENT, fp);
  109. }
  110.  
  111. #endif
  112.  
  113. #endif /* _REENT_ONLY */
  114.