os/ossrv/genericopenlibs/cstdlib/LSTDIO/SETVBUF.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/SETVBUF.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,199 @@
     1.4 +/* SETVBUF.C
     1.5 + * 
     1.6 + * Portions Copyright (c) 1990-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +/*
    1.11 + * Copyright (c) 1990 The Regents of the University of California.
    1.12 + * All rights reserved.
    1.13 + *
    1.14 + * Redistribution and use in source and binary forms are permitted
    1.15 + * provided that the above copyright notice and this paragraph are
    1.16 + * duplicated in all such forms and that any documentation,
    1.17 + * advertising materials, and other materials related to such
    1.18 + * distribution and use acknowledge that the software was developed
    1.19 + * by the University of California, Berkeley.  The name of the
    1.20 + * University may not be used to endorse or promote products derived
    1.21 + * from this software without specific prior written permission.
    1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 +FUNCTION
    1.29 +<<setvbuf>>---specify file or stream buffering
    1.30 +
    1.31 +INDEX
    1.32 +	setvbuf
    1.33 +
    1.34 +ANSI_SYNOPSIS
    1.35 +	#include <stdio.h>
    1.36 +	int setvbuf(FILE *<[fp]>, char *<[buf]>,
    1.37 +	            int <[mode]>, size_t <[size]>);
    1.38 +
    1.39 +TRAD_SYNOPSIS
    1.40 +	#include <stdio.h>
    1.41 +	int setvbuf(<[fp]>, <[buf]>, <[mode]>, <[size]>)
    1.42 +	FILE *<[fp]>;
    1.43 +	char *<[buf]>;
    1.44 +	int <[mode]>;
    1.45 +	size_t <[size]>;
    1.46 +
    1.47 +DESCRIPTION
    1.48 +Use <<setvbuf>> to specify what kind of buffering you want for the
    1.49 +file or stream identified by <[fp]>, by using one of the following
    1.50 +values (from <<stdio.h>>) as the <[mode]> argument:
    1.51 +
    1.52 +o+
    1.53 +o _IONBF
    1.54 +Do not use a buffer: send output directly to the host system for the
    1.55 +file or stream identified by <[fp]>.
    1.56 +
    1.57 +o _IOFBF
    1.58 +Use full output buffering: output will be passed on to the host system
    1.59 +only when the buffer is full, or when an input operation intervenes.
    1.60 +
    1.61 +o _IOLBF
    1.62 +Use line buffering: pass on output to the host system at every
    1.63 +newline, as well as when the buffer is full, or when an input
    1.64 +operation intervenes.
    1.65 +o-
    1.66 +
    1.67 +Use the <[size]> argument to specify how large a buffer you wish.  You
    1.68 +can supply the buffer itself, if you wish, by passing a pointer to a
    1.69 +suitable area of memory as <[buf]>.  Otherwise, you may pass <<NULL>>
    1.70 +as the <[buf]> argument, and <<setvbuf>> will allocate the buffer.
    1.71 +
    1.72 +WARNINGS
    1.73 +You may only use <<setvbuf>> before performing any file operation other
    1.74 +than opening the file.
    1.75 +
    1.76 +If you supply a non-null <[buf]>, you must ensure that the associated
    1.77 +storage continues to be available until you close the stream
    1.78 +identified by <[fp]>.
    1.79 +
    1.80 +RETURNS
    1.81 +A <<0>> result indicates success, <<EOF>> failure (invalid <[mode]> or
    1.82 +<[size]> can cause failure).
    1.83 +
    1.84 +PORTABILITY
    1.85 +Both ANSI C and the System V Interface Definition (Issue 2) require
    1.86 +<<setvbuf>>. However, they differ on the meaning of a <<NULL>> buffer
    1.87 +pointer: the SVID issue 2 specification says that a <<NULL>> buffer
    1.88 +pointer requests unbuffered output.  For maximum portability, avoid
    1.89 +<<NULL>> buffer pointers.
    1.90 +
    1.91 +Both specifications describe the result on failure only as a
    1.92 +nonzero value.
    1.93 +
    1.94 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
    1.95 +<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
    1.96 +*/
    1.97 +
    1.98 +#include <stdio_r.h>
    1.99 +#include <stdlib_r.h>
   1.100 +#include "LOCAL.H"
   1.101 +
   1.102 +/**
   1.103 +Change stream buffering.
   1.104 +Changes the buffer to be used for I/O operations with the specified stream.
   1.105 +Size and mode for the buffer can be specified.
   1.106 +This function should be called once the file associated with the stream 
   1.107 +has been opened but before any input or output operation has been done.
   1.108 +The size of the buffer is specified by the size parameter,
   1.109 +and can be any value between 2 and 32767 in bytes, 
   1.110 +this value may be rounded down by some system due to specific alignment.
   1.111 +buffer can be NULL.
   1.112 +@return If the buffer is correctly assigned to the file a 0 value is returned.
   1.113 +On error, a non-zero value is returned. This can be because an invalid type or size has been specified or because an error allocating memory (if NULL buffer was specified).
   1.114 +@param fp pointer to an open file. 
   1.115 +@param buf User allocated buffer. Must have at least a size of size bytes. 
   1.116 +@param mode Specifies a mode for file buffering
   1.117 +@param Buffer size in bytes, must be more than 0 and less than 32768, this value may be rounded down by some systems due to specific alignment, in which case the minimum value should be 2.
   1.118 +*/
   1.119 +EXPORT_C int
   1.120 +setvbuf (FILE * fp, char *buf, int mode, size_t size)
   1.121 +{
   1.122 +  int ret = 0;
   1.123 +  CHECK_INIT (fp);
   1.124 +
   1.125 +  /*
   1.126 +   * Verify arguments.  The `int' limit on `size' is due to this
   1.127 +   * particular implementation.
   1.128 +   */
   1.129 +
   1.130 +  if ((mode != _IOFBF && mode != _IOLBF && mode != _IONBF) || (int) size < 0)
   1.131 +    return (EOF);
   1.132 +
   1.133 +  /*
   1.134 +   * Write current buffer, if any; drop read count, if any.
   1.135 +   * Make sure putc() will not think fp is line buffered.
   1.136 +   * Free old buffer if it was from malloc().  Clear line and
   1.137 +   * non buffer flags, and clear malloc flag.
   1.138 +   */
   1.139 +
   1.140 +  (void) fflush (fp);
   1.141 +  fp->_r = 0;
   1.142 +  fp->_lbfsize = 0;
   1.143 +  if (fp->_flags & __SMBF)
   1.144 +    _free_r (fp->_data, (void *) fp->_bf._base);
   1.145 +  fp->_flags &= ~(__SLBF | __SNBF | __SMBF);
   1.146 +
   1.147 +  if (mode == _IONBF)
   1.148 +    goto nbf;
   1.149 +
   1.150 +  /*
   1.151 +   * Allocate buffer if needed. */
   1.152 +  if (buf == NULL)
   1.153 +    {
   1.154 +      if ((buf = (char *)malloc (size)) == NULL)
   1.155 +	{
   1.156 +	  ret = EOF;
   1.157 +	  /* Try another size... */
   1.158 +	  buf = (char *)malloc (BUFSIZ);
   1.159 +	}
   1.160 +      if (buf == NULL)
   1.161 +	{
   1.162 +	  /* Can't allocate it, let's try another approach */
   1.163 +nbf:
   1.164 +	  fp->_flags |= __SNBF;
   1.165 +	  fp->_w = 0;
   1.166 +	  fp->_bf._base = fp->_p = fp->_nbuf;
   1.167 +	  fp->_bf._size = 1;
   1.168 +	  return (ret);
   1.169 +	}
   1.170 +      fp->_flags |= __SMBF;
   1.171 +    }
   1.172 +  /*
   1.173 +   * Now put back whichever flag is needed, and fix _lbfsize
   1.174 +   * if line buffered.  Ensure output flush on exit if the
   1.175 +   * stream will be buffered at all.
   1.176 +   * Force the buffer to be flushed and hence malloced on first use
   1.177 +   */
   1.178 +
   1.179 +  switch (mode)
   1.180 +    {
   1.181 +    case _IOLBF:
   1.182 +      fp->_flags |= __SLBF;
   1.183 +      fp->_lbfsize = -(int)size;
   1.184 +      /* FALLTHROUGH */
   1.185 +
   1.186 +    case _IOFBF:
   1.187 +      /* no flag */
   1.188 +      fp->_data->__cleanup = _cleanup_r;
   1.189 +      fp->_bf._base = fp->_p = (unsigned char *) buf;
   1.190 +      fp->_bf._size = size;
   1.191 +      break;
   1.192 +    }
   1.193 +
   1.194 +  /*
   1.195 +   * Patch up write count if necessary.
   1.196 +   */
   1.197 +
   1.198 +  if (fp->_flags & __SWR)
   1.199 +    fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : size;
   1.200 +
   1.201 +  return 0;
   1.202 +}