home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / SETVBUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.2 KB  |  140 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - setvbuf.c
  3.  *
  4.  * function(s)
  5.  *        setvbuf - assigns buffering to a stream
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20. extern  void    (*_exitbuf)();
  21. extern  void      _xfflush();
  22.  
  23. static int _stdinStarted  = 0;
  24. static int _stdoutStarted = 0;
  25.  
  26. /*-----------------------------------------------------------------------*
  27.  
  28. Name            setvbuf - assigns buffering to a stream
  29.  
  30. Usage           #include <stdio.h>
  31.                 int setvbuf(FILE *stream, char *buf, int type,
  32.                             size_t size);
  33.  
  34. Prototype in    stdio.h
  35.  
  36. Description     setbuf and setvbuf cause the buffer buf to be used
  37.                 for I/O buffering instead of an automatically allocated buffer.
  38.                 They are used after the given stream is opened.
  39.  
  40.                 In setbuf, if buf is NULL, I/O will be unbuffered; otherwise,
  41.                 it will be fully buffered. The buffer must be BUFSIZ bytes long
  42.                 (specified in stdio.h). In setvbuf, if buf is NULL, a buffer
  43.                 will be allocated using malloc; the buffer will use size as the
  44.                 amount allocated. The size parameter specifies the buffer size
  45.                 and must be greater than zero.
  46.  
  47.                 stdin and stdout are unbuffered if they are not redirected;
  48.                 otherwise, they are fully buffered. setbuf may be used to
  49.                 change the buffering style being used.
  50.  
  51.                 Unbuffered means that characters written to a stream are
  52.                 immediately output to the file or device, while buffered
  53.                 means that the characters are accumulated and written as
  54.                 a block.
  55.  
  56.                 In setvbuf, the type parameter is one of the following:
  57.  
  58.                         _IOFBF  The file is fully buffered. When a buffer
  59.                                 is empty, the next input operation will
  60.                                 attempt to fill the entire buffer.
  61.  
  62.                                 On output the buffer will be completely
  63.                                 filled before any data is written to the file.
  64.  
  65.                         _IOLBF  The file is line buffered. When a buffer
  66.                                 is empty, the next input operation will still
  67.                                 attempt to fill the entire buffer. On output,
  68.                                 however, the buffer will be flushed whenever
  69.                                 a newline character is written to the file.
  70.  
  71.                         _IONBF  The file is unbuffered. The buf and size
  72.                                 parameters are ignored. Each input operation
  73.                                 will read directly from the file, and each
  74.                                 output operation will immediately write the
  75.                                 data to the file.
  76.  
  77.                 setbuf will produce unpredictable results if it is called for a
  78.                 stream, except immediately after opening the stream or any call
  79.                 to fseek. Calling setbuf after a stream has been unbuffered is
  80.                 legal and will not cause problems.
  81.  
  82.                 A common cause for error is to allocate the buffer as an
  83.                 automatic (local) variable and then fail to close the file
  84.                 before returning from the function where the buffer was
  85.                 declared.
  86.  
  87. Return value    setbuf returns nothing.
  88.  
  89.                 setvbuf returns 0 on success. It returns non-zero if an
  90.                 invalid value is given for type or size, if buf is NULL,
  91.                 or if there is not enough space to allocate a buffer.
  92.  
  93.                 setvbuf returns 0 on success.
  94.  
  95. *-----------------------------------------------------------------------*/
  96. int _FARFUNC setvbuf(register FILE *fp, char *buf, int type, size_t size)
  97. {
  98.     if (fp->token != (short) fp || _IONBF < type || 0x7fff < size)
  99.                 return(EOF);
  100.  
  101.     if (! _stdoutStarted  && ((short) fp == (short) stdout))
  102.         _stdoutStarted = 1;
  103.     else
  104.         if (! _stdinStarted  && ((short) fp == (short) stdin))
  105.         _stdinStarted = 1;
  106.  
  107. /*  Ensure the change in buffering causes no loss of characters.
  108.     fseek() will fflush and reposition safely.
  109. */
  110.     if (fp->level)
  111.         fseek (fp, 0L, SEEK_CUR);
  112.  
  113.     if (fp->flags & _F_BUF)
  114.         free (fp->buffer);
  115.  
  116.     fp->flags &= ~(_F_BUF | _F_LBUF);
  117.     fp->bsize = 0;
  118.     fp->curp = fp->buffer = & fp->hold;
  119.  
  120.     if (_IONBF != type && size > 0)
  121.     {
  122.         _exitbuf = _xfflush;
  123.  
  124.         if (NULL == buf)
  125.         {
  126.             if ((buf = malloc (size)) != NULL)
  127.                 fp->flags |= _F_BUF;
  128.             else
  129.                                 return(EOF);
  130.         }
  131.                 fp->buffer = fp->curp = (unsigned char *)buf;
  132.         fp->bsize = size;
  133.         if (_IOLBF == type)
  134.             fp->flags |= _F_LBUF;
  135.     }
  136.  
  137.         return 0;
  138.  
  139. }
  140.