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