home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / SETVBUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  5.5 KB  |  141 lines

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