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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [findfp.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/* 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 <errno.h>
23
#include <string.h>
24
#include <fcntl.h>
25
#include "local.h"
26
 
27
static void
28
std (ptr, flags, file, data)
29
     FILE *ptr;
30
     int flags;
31
     int file;
32
     struct _reent *data;
33
{
34
  ptr->_p = 0;
35
  ptr->_r = 0;
36
  ptr->_w = 0;
37
  ptr->_flags = flags;
38
  ptr->_file = file;
39
  ptr->_bf._base = 0;
40
  ptr->_bf._size = 0;
41
  ptr->_lbfsize = 0;
42
  ptr->_cookie = ptr;
43
  ptr->_read = __sread;
44
  ptr->_write = __swrite;
45
  ptr->_seek = __sseek;
46
  ptr->_close = __sclose;
47
  ptr->_data = data;
48
 
49
#ifdef __SCLE
50
  if (__stextmode(ptr->_file))
51
    ptr->_flags |= __SCLE;
52
#endif
53
}
54
 
55
struct _glue *
56
__sfmoreglue (d, n)
57
     struct _reent *d;
58
     register int n;
59
{
60
  struct _glue *g;
61
  FILE *p;
62
 
63
  g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
64
  if (g == NULL)
65
    return NULL;
66
  p = (FILE *) (g + 1);
67
  g->_next = NULL;
68
  g->_niobs = n;
69
  g->_iobs = p;
70
  memset (p, 0, n * sizeof (FILE));
71
  return g;
72
}
73
 
74
/*
75
 * Find a free FILE for fopen et al.
76
 */
77
 
78
FILE *
79
__sfp (d)
80
     struct _reent *d;
81
{
82
  FILE *fp;
83
  int n;
84
  struct _glue *g;
85
 
86
  if (!d->__sdidinit)
87
    __sinit (d);
88
  for (g = &d->__sglue;; g = g->_next)
89
    {
90
      for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
91
        if (fp->_flags == 0)
92
          goto found;
93
      if (g->_next == NULL &&
94
          (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
95
        break;
96
    }
97
  d->_errno = ENOMEM;
98
  return NULL;
99
 
100
found:
101
  fp->_flags = 1;               /* reserve this slot; caller sets real flags */
102
  fp->_p = NULL;                /* no current pointer */
103
  fp->_w = 0;                    /* nothing to read or write */
104
  fp->_r = 0;
105
  fp->_bf._base = NULL;         /* no buffer */
106
  fp->_bf._size = 0;
107
  fp->_lbfsize = 0;              /* not line buffered */
108
  fp->_file = -1;               /* no file */
109
  /* fp->_cookie = <any>; */    /* caller sets cookie, _read/_write etc */
110
  fp->_ub._base = NULL;         /* no ungetc buffer */
111
  fp->_ub._size = 0;
112
  fp->_lb._base = NULL;         /* no line buffer */
113
  fp->_lb._size = 0;
114
  fp->_data = d;
115
  return fp;
116
}
117
 
118
/*
119
 * exit() calls _cleanup() through *__cleanup, set whenever we
120
 * open or buffer a file.  This chicanery is done so that programs
121
 * that do not use stdio need not link it all in.
122
 *
123
 * The name `_cleanup' is, alas, fairly well known outside stdio.
124
 */
125
 
126
void
127
_cleanup_r (ptr)
128
     struct _reent *ptr;
129
{
130
  /* (void) _fwalk(fclose); */
131
  (void) _fwalk (ptr, fflush);  /* `cheating' */
132
}
133
 
134
#ifndef _REENT_ONLY
135
void
136
_cleanup ()
137
{
138
  _cleanup_r (_REENT);
139
}
140
#endif
141
 
142
/*
143
 * __sinit() is called whenever stdio's internal variables must be set up.
144
 */
145
 
146
void
147
__sinit (s)
148
     struct _reent *s;
149
{
150
  /* make sure we clean up on exit */
151
  s->__cleanup = _cleanup_r;    /* conservative */
152
  s->__sdidinit = 1;
153
 
154
  std (s->__sf + 0, __SRD, 0, s);
155
 
156
  /* on platforms that have true file system I/O, we can verify whether stdout
157
     is an interactive terminal or not.  For all other platforms, we will
158
     default to line buffered mode here.  */
159
#ifdef HAVE_FCNTL
160
  std (s->__sf + 1, __SWR, 1, s);
161
#else
162
  std (s->__sf + 1, __SWR | __SLBF, 1, s);
163
#endif
164
 
165
  std (s->__sf + 2, __SWR | __SNBF, 2, s);
166
 
167
  s->__sglue._next = NULL;
168
  s->__sglue._niobs = 3;
169
  s->__sglue._iobs = &s->__sf[0];
170
}

powered by: WebSVN 2.1.0

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