[BACK]Return to findfp.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / lib / libc / stdio

Annotation of prex-old/usr/lib/libc/stdio/findfp.c, Revision 1.1

1.1     ! nbrk        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:
        !            43: #define        std(next, flags, file) \
        !            44:        {next,NULL,0,0,flags,file,{0,0},{0,0},NULL,0,{0},{0}}
        !            45: /*      p r w flags file _bf z */
        !            46:
        !            47: FILE __sF[3] = {
        !            48:        std(&__sF[1], __SRD, STDIN_FILENO),
        !            49:        std(&__sF[2], __SWR, STDOUT_FILENO),
        !            50:        std(NULL, __SWR|__SNBF, STDERR_FILENO)
        !            51: };
        !            52:
        !            53: /*
        !            54:  * Find a free FILE for fopen et al.
        !            55:  */
        !            56: FILE *
        !            57: __sfp()
        !            58: {
        !            59:        FILE *fp, *tmp;
        !            60:
        !            61:        if (!__sdidinit)
        !            62:                __sinit();
        !            63:        for (fp = &__sF[0];; fp = fp->next) {
        !            64:                if (fp->_flags == 0)
        !            65:                        goto found;
        !            66:                if (fp->next == NULL) {
        !            67:                        if ((tmp = malloc(sizeof(FILE))) == NULL)
        !            68:                                break;
        !            69:                        tmp->next = NULL;
        !            70:                        fp->next = tmp;
        !            71:                        fp = tmp;
        !            72:                        goto found;
        !            73:                }
        !            74:        }
        !            75:        return (NULL);
        !            76: found:
        !            77:        fp->_flags = 1;         /* reserve this slot; caller sets real flags */
        !            78:        fp->_p = NULL;          /* no current pointer */
        !            79:        fp->_w = 0;             /* nothing to read or write */
        !            80:        fp->_r = 0;
        !            81:        fp->_bf._base = NULL;   /* no buffer */
        !            82:        fp->_bf._size = 0;
        !            83:        fp->_file = -1;         /* no file */
        !            84:        fp->_ub._base = NULL;   /* no ungetc buffer */
        !            85:        fp->_ub._size = 0;
        !            86:        return (fp);
        !            87: }
        !            88:
        !            89: /*
        !            90:  * exit() calls _cleanup() through *__cleanup, set whenever we
        !            91:  * open or buffer a file.  This chicanery is done so that programs
        !            92:  * that do not use stdio need not link it all in.
        !            93:  *
        !            94:  * The name `_cleanup' is, alas, fairly well known outside stdio.
        !            95: b */
        !            96: void
        !            97: _cleanup()
        !            98: {
        !            99:        /* (void) _fwalk(fclose); */
        !           100:        (void) _fwalk(__sflush);                /* `cheating' */
        !           101: }
        !           102:
        !           103: /*
        !           104:  * __sinit() is called whenever stdio's internal variables must be set up.
        !           105:  */
        !           106: void
        !           107: __sinit()
        !           108: {
        !           109:        /* make sure we clean up on exit */
        !           110:        __cleanup = _cleanup;           /* conservative */
        !           111:        __sdidinit = 1;
        !           112: }

CVSweb