Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/lib/libc/stdio/findfp.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. std
  2. __sfp
  3. _cleanup
  4. __sinit

   1 /*-
   2  * Copyright (c) 1990, 1993
   3  *      The Regents of the University of California.  All rights reserved.
   4  *
   5  * This code is derived from software contributed to Berkeley by
   6  * Chris Torek.
   7  *
   8  * Redistribution and use in source and binary forms, with or without
   9  * modification, are permitted provided that the following conditions
  10  * are met:
  11  * 1. Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  * 2. Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in the
  15  *    documentation and/or other materials provided with the distribution.
  16  * 3. Neither the name of the University nor the names of its contributors
  17  *    may be used to endorse or promote products derived from this software
  18  *    without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30  * SUCH DAMAGE.
  31  */
  32 
  33 #include <sys/param.h>
  34 #include <unistd.h>
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <errno.h>
  38 #include <string.h>
  39 #include "local.h"
  40 
  41 int     __sdidinit;
  42 FILE __sF[3];
  43 
  44 static void
  45 std(FILE *fp, FILE *next, short flags, short fileno)
  46 {
  47 
  48         memset(fp, 0, sizeof(FILE));
  49         fp->next = next;
  50         fp->_flags = flags;
  51         fp->_file = fileno;
  52 }
  53 
  54 /*
  55  * Find a free FILE for fopen et al.
  56  */
  57 FILE *
  58 __sfp()
  59 {
  60         FILE *fp, *tmp;
  61 
  62         if (!__sdidinit)
  63                 __sinit();
  64         for (fp = &__sF[0];; fp = fp->next) {
  65                 if (fp->_flags == 0)
  66                         goto found;
  67                 if (fp->next == NULL) {
  68                         if ((tmp = malloc(sizeof(FILE))) == NULL)
  69                                 break;
  70                         fp->next = tmp;
  71                         fp = tmp;
  72                         goto found;
  73                 }
  74         }
  75         return (NULL);
  76 found:
  77         std(fp, NULL, 1, -1);
  78         return (fp);
  79 }
  80 
  81 /*
  82  * exit() calls _cleanup() through *__cleanup, set whenever we
  83  * open or buffer a file.  This chicanery is done so that programs
  84  * that do not use stdio need not link it all in.
  85  *
  86  * The name `_cleanup' is, alas, fairly well known outside stdio.
  87 b */
  88 void
  89 _cleanup()
  90 {
  91         /* (void) _fwalk(fclose); */
  92         (void) _fwalk(__sflush);                /* `cheating' */
  93 }
  94 
  95 /*
  96  * __sinit() is called whenever stdio's internal variables must be set up.
  97  */
  98 void
  99 __sinit()
 100 {
 101 
 102         std(&__sF[0], &__sF[1], __SRD, STDIN_FILENO);
 103         std(&__sF[1], &__sF[2], __SWR, STDOUT_FILENO);
 104         std(&__sF[2], NULL, __SWR|__SNBF, STDERR_FILENO);
 105 
 106         /* make sure we clean up on exit */
 107         __cleanup = _cleanup;           /* conservative */
 108         __sdidinit = 1;
 109 }

/* [<][>][^][v][top][bottom][index][help] */