OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [stdio/] [makebuf.c] - Blame information for rev 1775

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.