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

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [stdio/] [findfp.c] - Blame information for rev 1765

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

powered by: WebSVN 2.1.0

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