os/ossrv/genericopenlibs/cstdlib/LSTDIO/MAKEBUF.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/MAKEBUF.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* MAKEBUF.C
     1.5 + *
     1.6 + * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +/* No user fns here.  Pesch 15apr92. */
    1.11 +
    1.12 +/*
    1.13 + * Copyright (c) 1990 The Regents of the University of California.
    1.14 + * All rights reserved.
    1.15 + *
    1.16 + * Redistribution and use in source and binary forms are permitted
    1.17 + * provided that the above copyright notice and this paragraph are
    1.18 + * duplicated in all such forms and that any documentation,
    1.19 + * advertising materials, and other materials related to such
    1.20 + * distribution and use acknowledge that the software was developed
    1.21 + * by the University of California, Berkeley.  The name of the
    1.22 + * University may not be used to endorse or promote products derived
    1.23 + * from this software without specific prior written permission.
    1.24 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.25 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.26 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.27 + */
    1.28 +
    1.29 +#include <stdio_r.h>
    1.30 +#include <stdlib_r.h>
    1.31 +#include <sys/stat.h>
    1.32 +#include <sys/types.h>
    1.33 +#include <sys/unistd.h>
    1.34 +
    1.35 +#include "LOCAL.H"
    1.36 +
    1.37 +/*
    1.38 + * Allocate a file buffer, or switch to unbuffered I/O.
    1.39 + * Per the ANSI C standard, ALL tty devices default to line buffered.
    1.40 + *
    1.41 + * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
    1.42 + * optimization) right after the _fstat() that finds the buffer size.
    1.43 + */
    1.44 +
    1.45 +void
    1.46 +__smakebuf (register FILE *fp)
    1.47 +{
    1.48 +  register size_t size, couldbetty;
    1.49 +  register void* p;
    1.50 +  struct stat st;
    1.51 +
    1.52 +  if (fp->_flags & __SNBF)
    1.53 +    {
    1.54 +      fp->_bf._base = fp->_p = fp->_nbuf;
    1.55 +      fp->_bf._size = 1;
    1.56 +      return;
    1.57 +    }
    1.58 +  if (fp->_file < 0 || _fstat_r (fp->_data, fp->_file, &st) < 0)
    1.59 +    {
    1.60 +      couldbetty = 0;
    1.61 +      size = BUFSIZ;
    1.62 +      /* do not try to optimise fseek() */
    1.63 +      fp->_flags |= __SNPT;
    1.64 +    }
    1.65 +  else
    1.66 +    {
    1.67 +      couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
    1.68 +      size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
    1.69 +      /*
    1.70 +       * Optimize fseek() only if it is a regular file.
    1.71 +       * (The test for __sseek is mainly paranoia.)
    1.72 +       */
    1.73 +      if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek)
    1.74 +	{
    1.75 +	  fp->_flags |= __SOPT;
    1.76 +	  fp->_blksize = st.st_blksize;
    1.77 +	}
    1.78 +      else
    1.79 +	fp->_flags |= __SNPT;
    1.80 +    }
    1.81 +  if ((p = _malloc_r (fp->_data, size)) == NULL)
    1.82 +    {
    1.83 +      fp->_flags |= __SNBF;
    1.84 +      fp->_bf._base = fp->_p = fp->_nbuf;
    1.85 +      fp->_bf._size = 1;
    1.86 +    }
    1.87 +  else
    1.88 +    {
    1.89 +      fp->_data->__cleanup = _cleanup_r;
    1.90 +      fp->_flags |= __SMBF;
    1.91 +      fp->_bf._base = fp->_p = (unsigned char *) p;
    1.92 +      fp->_bf._size = size;
    1.93 +      if (couldbetty && isatty (fp->_file))
    1.94 +	fp->_flags |= __SLBF;
    1.95 +    }
    1.96 +}