os/ossrv/genericopenlibs/cstdlib/LSTDIO/FINDFP.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FINDFP.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,210 @@
     1.4 +/* FINDFP.C
     1.5 + * 
     1.6 + * Portions Copyright (c) 1990-2008 Nokia Corporation and/or its subsidiary(-ies).
     1.7 + * All rights reserved.
     1.8 + */
     1.9 +
    1.10 +/* No user fns here.  Pesch 15apr92. */
    1.11 +
    1.12 +/*
    1.13 + * Copyright (c) 1990 The Regents of the University of California.
    1.14 + * All rights reserved.
    1.15 + *
    1.16 + * Redistribution and use in source and binary forms are permitted
    1.17 + * provided that the above copyright notice and this paragraph are
    1.18 + * duplicated in all such forms and that any documentation,
    1.19 + * advertising materials, and other materials related to such
    1.20 + * distribution and use acknowledge that the software was developed
    1.21 + * by the University of California, Berkeley.  The name of the
    1.22 + * University may not be used to endorse or promote products derived
    1.23 + * from this software without specific prior written permission.
    1.24 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    1.25 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    1.26 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.27 + */
    1.28 +
    1.29 +#include <stdio_r.h>
    1.30 +#include <stdlib_r.h>
    1.31 +#include <errno.h>
    1.32 +#include <string.h>
    1.33 +#include "LOCAL.H"
    1.34 +
    1.35 +/* Initialise a FILE structure to a starting state */
    1.36 +#ifdef __GCCXML__
    1.37 +static void
    1.38 +_std (FILE *ptr, int flags, int file, struct _reent *data)
    1.39 +#else
    1.40 +static void
    1.41 +std (FILE *ptr, int flags, int file, struct _reent *data)
    1.42 +#endif
    1.43 +{
    1.44 +  ptr->_p = 0;
    1.45 +  ptr->_r = 0;
    1.46 +  ptr->_w = 0;
    1.47 +  ptr->_flags = (short)flags;
    1.48 +  ptr->_file = (short)file;
    1.49 +  ptr->_bf._base = 0;
    1.50 +  ptr->_lbfsize = 0;
    1.51 +  ptr->_cookie = ptr;
    1.52 +  ptr->_read = __sread;
    1.53 +  ptr->_write = __swrite;
    1.54 +  ptr->_seek = __sseek;
    1.55 +  ptr->_close = __sclose;
    1.56 +  ptr->_data = data;
    1.57 +}
    1.58 +
    1.59 +struct _glue *
    1.60 +__sfmoreglue (struct _reent *d, register int n)
    1.61 +{
    1.62 +  struct _glue *g;
    1.63 +  FILE *p;
    1.64 +
    1.65 +  g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
    1.66 +  if (g == NULL)
    1.67 +    return NULL;
    1.68 +  p = (FILE *) (g + 1);
    1.69 +  g->_next = NULL;
    1.70 +  g->_niobs = n;
    1.71 +  g->_iobs = p;
    1.72 +  memset (p, 0, n * sizeof (FILE));
    1.73 +  return g;
    1.74 +}
    1.75 +
    1.76 +/*
    1.77 + * Find a free FILE for fopen et al.
    1.78 + */
    1.79 +
    1.80 +FILE *
    1.81 +__sfp (struct _reent *d)
    1.82 +{
    1.83 +  FILE *fp;
    1.84 +  int n;
    1.85 +  struct _glue *g;
    1.86 +
    1.87 +  if (!d->__sdidinit)
    1.88 +    __sinit (d);
    1.89 +  for (g = &d->__sglue;; g = g->_next)
    1.90 +    {
    1.91 +      for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
    1.92 +	if (fp->_flags == 0)
    1.93 +	  goto found;
    1.94 +      if (g->_next == NULL &&
    1.95 +	  (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
    1.96 +	break;
    1.97 +    }
    1.98 +  d->_errno = ENOMEM;
    1.99 +  return NULL;
   1.100 +
   1.101 +found:
   1.102 +  fp->_flags = 1;		/* reserve this slot; caller sets real flags */
   1.103 +  fp->_p = NULL;		/* no current pointer */
   1.104 +  fp->_w = 0;			/* nothing to read or write */
   1.105 +  fp->_r = 0;
   1.106 +  fp->_bf._base = NULL;		/* no buffer */
   1.107 +  fp->_bf._size = 0;
   1.108 +  fp->_lbfsize = 0;		/* not line buffered */
   1.109 +  fp->_file = -1;		/* no file */
   1.110 +  /* fp->_cookie = <any>; */	/* caller sets cookie, _read/_write etc */
   1.111 +  fp->_ub._base = NULL;		/* no ungetc buffer */
   1.112 +  fp->_ub._size = 0;
   1.113 +  fp->_lb._base = NULL;		/* no line buffer */
   1.114 +  fp->_lb._size = 0;
   1.115 +  fp->_data = d;
   1.116 +  return fp;
   1.117 +}
   1.118 +
   1.119 +/*
   1.120 + * exit() calls _cleanup() through *__cleanup, set whenever we
   1.121 + * open or buffer a file.  This chicanery is done so that programs
   1.122 + * that do not use stdio need not link it all in.
   1.123 + *
   1.124 + * The name `_cleanup' is, alas, fairly well known outside stdio.
   1.125 + */
   1.126 +/**
   1.127 +A reentrant version of _cleanup().
   1.128 +*/
   1.129 +EXPORT_C void
   1.130 +_cleanup_r (struct _reent *ptr)
   1.131 +{
   1.132 +  (void) _fwalk(ptr, fclose);
   1.133 +}
   1.134 +
   1.135 +#ifndef _REENT_ONLY
   1.136 +
   1.137 +/**
   1.138 +Performs cleanup
   1.139 +*/
   1.140 +EXPORT_C void
   1.141 +_cleanup (void)
   1.142 +{
   1.143 +  _cleanup_r (_REENT);
   1.144 +}
   1.145 +#endif
   1.146 +
   1.147 +/*
   1.148 + * __sinit() is called whenever stdio's internal variables must be set up.
   1.149 + */
   1.150 +
   1.151 +void
   1.152 +__sinit (struct _reent *s)
   1.153 +{
   1.154 +  /* make sure we clean up on exit */
   1.155 +  s->__cleanup = _cleanup_r;	/* conservative */
   1.156 +  s->__sdidinit = 1;
   1.157 +
   1.158 +#ifdef __GCCXML__
   1.159 +  _std (_stdin_r(s),  __SRD, 0, s);
   1.160 +  _std (_stdout_r(s), __SWR, 1, s);
   1.161 +  _std (_stderr_r(s), __SWR | __SNBF, 2, s);
   1.162 +#else
   1.163 +  std (_stdin_r(s),  __SRD, 0, s);
   1.164 +  std (_stdout_r(s), __SWR, 1, s);
   1.165 +  std (_stderr_r(s), __SWR | __SNBF, 2, s);
   1.166 +#endif
   1.167 +
   1.168 +  /* initialise the head of the glue chain to cover stdim/stdout/stderr */
   1.169 +  s->__sglue._next = NULL;
   1.170 +  s->__sglue._niobs = 3;
   1.171 +  s->__sglue._iobs = &s->_sf[0];
   1.172 +}
   1.173 +
   1.174 +/* Function interface to stdin/stdout/stderr 
   1.175 + *
   1.176 + * These functions should return a value which is fixed for any given
   1.177 + * reentrancy context, but it doesn't have to be fixed at compile time,
   1.178 + * and we don't need to have initialised stdio either.
   1.179 + */
   1.180 +
   1.181 +/**
   1.182 +Function interface to the "constants" stdin.
   1.183 +These functions guarantee to return a fixed value, so that it 
   1.184 +will be possible to use expressions such as if (fp != stdout) fclose(fp);
   1.185 +with complete confidence. Unfortunately it will rule out initialising global
   1.186 +variables with stdin/stdout/stderr, as in the common idiom:
   1.187 +
   1.188 +static FILE *log = stderr;
   1.189 +
   1.190 +This isn't currently possible with EPOC32. 
   1.191 +*/
   1.192 +EXPORT_C FILE *__stdin (void)
   1.193 +{
   1.194 +	return _stdin_r(_REENT);
   1.195 +}
   1.196 +
   1.197 +/**
   1.198 +Function interface to the "constants" stdout
   1.199 +See __stdin
   1.200 +*/
   1.201 +EXPORT_C FILE *__stdout (void)
   1.202 +{
   1.203 +	return _stdout_r(_REENT);
   1.204 +}
   1.205 +
   1.206 +/**
   1.207 +Function interface to the "constants" stderr
   1.208 +See __stdin
   1.209 +*/
   1.210 +EXPORT_C FILE *__stderr (void)
   1.211 +{
   1.212 +	return _stderr_r(_REENT);
   1.213 +}