os/ossrv/genericopenlibs/cstdlib/LSTDIO/MAKEBUF.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* MAKEBUF.C
sl@0
     2
 *
sl@0
     3
 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     4
 * All rights reserved.
sl@0
     5
 */
sl@0
     6
sl@0
     7
/* No user fns here.  Pesch 15apr92. */
sl@0
     8
sl@0
     9
/*
sl@0
    10
 * Copyright (c) 1990 The Regents of the University of California.
sl@0
    11
 * All rights reserved.
sl@0
    12
 *
sl@0
    13
 * Redistribution and use in source and binary forms are permitted
sl@0
    14
 * provided that the above copyright notice and this paragraph are
sl@0
    15
 * duplicated in all such forms and that any documentation,
sl@0
    16
 * advertising materials, and other materials related to such
sl@0
    17
 * distribution and use acknowledge that the software was developed
sl@0
    18
 * by the University of California, Berkeley.  The name of the
sl@0
    19
 * University may not be used to endorse or promote products derived
sl@0
    20
 * from this software without specific prior written permission.
sl@0
    21
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0
    22
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0
    23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    24
 */
sl@0
    25
sl@0
    26
#include <stdio_r.h>
sl@0
    27
#include <stdlib_r.h>
sl@0
    28
#include <sys/stat.h>
sl@0
    29
#include <sys/types.h>
sl@0
    30
#include <sys/unistd.h>
sl@0
    31
sl@0
    32
#include "LOCAL.H"
sl@0
    33
sl@0
    34
/*
sl@0
    35
 * Allocate a file buffer, or switch to unbuffered I/O.
sl@0
    36
 * Per the ANSI C standard, ALL tty devices default to line buffered.
sl@0
    37
 *
sl@0
    38
 * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
sl@0
    39
 * optimization) right after the _fstat() that finds the buffer size.
sl@0
    40
 */
sl@0
    41
sl@0
    42
void
sl@0
    43
__smakebuf (register FILE *fp)
sl@0
    44
{
sl@0
    45
  register size_t size, couldbetty;
sl@0
    46
  register void* p;
sl@0
    47
  struct stat st;
sl@0
    48
sl@0
    49
  if (fp->_flags & __SNBF)
sl@0
    50
    {
sl@0
    51
      fp->_bf._base = fp->_p = fp->_nbuf;
sl@0
    52
      fp->_bf._size = 1;
sl@0
    53
      return;
sl@0
    54
    }
sl@0
    55
  if (fp->_file < 0 || _fstat_r (fp->_data, fp->_file, &st) < 0)
sl@0
    56
    {
sl@0
    57
      couldbetty = 0;
sl@0
    58
      size = BUFSIZ;
sl@0
    59
      /* do not try to optimise fseek() */
sl@0
    60
      fp->_flags |= __SNPT;
sl@0
    61
    }
sl@0
    62
  else
sl@0
    63
    {
sl@0
    64
      couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
sl@0
    65
      size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
sl@0
    66
      /*
sl@0
    67
       * Optimize fseek() only if it is a regular file.
sl@0
    68
       * (The test for __sseek is mainly paranoia.)
sl@0
    69
       */
sl@0
    70
      if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek)
sl@0
    71
	{
sl@0
    72
	  fp->_flags |= __SOPT;
sl@0
    73
	  fp->_blksize = st.st_blksize;
sl@0
    74
	}
sl@0
    75
      else
sl@0
    76
	fp->_flags |= __SNPT;
sl@0
    77
    }
sl@0
    78
  if ((p = _malloc_r (fp->_data, size)) == NULL)
sl@0
    79
    {
sl@0
    80
      fp->_flags |= __SNBF;
sl@0
    81
      fp->_bf._base = fp->_p = fp->_nbuf;
sl@0
    82
      fp->_bf._size = 1;
sl@0
    83
    }
sl@0
    84
  else
sl@0
    85
    {
sl@0
    86
      fp->_data->__cleanup = _cleanup_r;
sl@0
    87
      fp->_flags |= __SMBF;
sl@0
    88
      fp->_bf._base = fp->_p = (unsigned char *) p;
sl@0
    89
      fp->_bf._size = size;
sl@0
    90
      if (couldbetty && isatty (fp->_file))
sl@0
    91
	fp->_flags |= __SLBF;
sl@0
    92
    }
sl@0
    93
}