home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdio / findfp.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  186 lines

  1. /*    $NetBSD: findfp.c,v 1.6 1995/02/02 02:09:17 jtc Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1990, 1993
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Chris Torek.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38.  
  39. #if defined(LIBC_SCCS) && !defined(lint)
  40. #if 0
  41. static char sccsid[] = "@(#)findfp.c    8.2 (Berkeley) 1/4/94";
  42. #endif
  43. static char rcsid[] = "$NetBSD: findfp.c,v 1.6 1995/02/02 02:09:17 jtc Exp $";
  44. #endif /* LIBC_SCCS and not lint */
  45.  
  46. #define _KERNEL
  47. #include "ixemul.h"
  48.  
  49. #include <sys/param.h>
  50. #include <unistd.h>
  51. #include <stdio.h>
  52. #include <errno.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include "local.h"
  56.  
  57. #define    NDYNAMIC 10        /* add ten more whenever necessary */
  58.  
  59. void
  60. __init_stdinouterr (void)
  61. {
  62.   FILE *fp;
  63.  
  64.   fp = u.u_sF[0] = __sfp();
  65.   if (fp)
  66.     {
  67.       fp->_flags  = __SRD;
  68.       fp->_file   = STDIN_FILENO;
  69.       fp->_cookie = fp;
  70.       fp->_close  = __sclose;
  71.       fp->_read   = __sread;
  72.       fp->_seek   = __sseek;
  73.       fp->_write  = __swrite;
  74.     }
  75.  
  76.   fp = u.u_sF[1] = __sfp();
  77.   if (fp)
  78.     {
  79.       fp->_flags  = __SWR;
  80.       fp->_file   = STDOUT_FILENO;
  81.       fp->_cookie = fp;
  82.       fp->_close  = __sclose;
  83.       fp->_read   = __sread;
  84.       fp->_seek   = __sseek;
  85.       fp->_write  = __swrite;
  86.     }
  87.  
  88.   fp = u.u_sF[2] = __sfp();
  89.   if (fp)
  90.     {
  91.       fp->_flags  = __SRW|__SNBF;
  92.       fp->_file   = STDERR_FILENO;
  93.       fp->_cookie = fp;
  94.       fp->_close  = __sclose;
  95.       fp->_read   = __sread;
  96.       fp->_seek   = __sseek;
  97.       fp->_write  = __swrite;
  98.     }
  99. }
  100.  
  101. static struct glue *
  102. moreglue(n)
  103.     register int n;
  104. {
  105.     register struct glue *g;
  106.     register FILE *p;
  107.     static FILE empty;
  108.  
  109.     g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
  110.     if (g == NULL)
  111.         return (NULL);
  112.     p = (FILE *)ALIGN(g + 1);
  113.     g->next = NULL;
  114.     g->niobs = n;
  115.     g->iobs = p;
  116.     while (--n >= 0)
  117.         *p++ = empty;
  118.     return (g);
  119. }
  120.  
  121. /*
  122.  * Find a free FILE for fopen et al.
  123.  */
  124. FILE *
  125. __sfp()
  126. {
  127.     register FILE *fp;
  128.     register int n;
  129.     register struct glue *g;
  130.  
  131.     for (g = &__sglue;; g = g->next) {
  132.         for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
  133.             if (fp->_flags == 0)
  134.                 goto found;
  135.         if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
  136.             break;
  137.     }
  138.     return (NULL);
  139. found:
  140.     fp->_flags = 1;        /* reserve this slot; caller sets real flags */
  141.     fp->_p = NULL;        /* no current pointer */
  142.     fp->_w = 0;        /* nothing to read or write */
  143.     fp->_r = 0;
  144.     fp->_bf._base = NULL;    /* no buffer */
  145.     fp->_bf._size = 0;
  146.     fp->_lbfsize = 0;    /* not line buffered */
  147.     fp->_file = -1;        /* no file */
  148. /*    fp->_cookie = <any>; */    /* caller sets cookie, _read/_write etc */
  149.     fp->_ub._base = NULL;    /* no ungetc buffer */
  150.     fp->_ub._size = 0;
  151.     fp->_lb._base = NULL;    /* no line buffer */
  152.     fp->_lb._size = 0;
  153.     return (fp);
  154. }
  155.  
  156. /*
  157.  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
  158.  * but documented historically for certain applications.  Bad applications.
  159.  */
  160. void
  161. f_prealloc()
  162. {
  163.     register struct glue *g;
  164.     int n;
  165.  
  166.     n = getdtablesize() - FOPEN_MAX + 20;        /* 20 for slop. */
  167.     for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
  168.         /* void */;
  169.     if (n > 0)
  170.         g->next = moreglue(n);
  171. }
  172.  
  173. /*
  174.  * exit() calls _cleanup() through *__cleanup, set whenever we
  175.  * open or buffer a file.  This chicanery is done so that programs
  176.  * that do not use stdio need not link it all in.
  177.  *
  178.  * The name `_cleanup' is, alas, fairly well known outside stdio.
  179.  */
  180. void
  181. _cleanup()
  182. {
  183.     /* (void) _fwalk(fclose); */
  184.     (void) _fwalk(__sflush);        /* `cheating' */
  185. }
  186.