Annotation of 40BSD/lib/libpc/GETNAME.c, revision 1.1

1.1     ! root        1: /* Copyright (c) 1979 Regents of the University of California */
        !             2: 
        !             3: static char sccsid[] = "@(#)GETNAME.c 1.1 10/29/80";
        !             4: 
        !             5: #include "h00vars.h"
        !             6: #include "h01errs.h"
        !             7: 
        !             8: /*
        !             9:  * GETNAME - activate a file
        !            10:  *
        !            11:  * takes a name, name length, element size, and variable
        !            12:  * level and returns a pointer to a file structure.
        !            13:  *
        !            14:  * a new file structure is initialized if necessary.
        !            15:  * temporary names are generated, and given
        !            16:  * names are blank trimmed.
        !            17:  */
        !            18: 
        !            19: struct iorec *
        !            20: GETNAME(filep, name, maxnamlen, datasize)
        !            21: 
        !            22:        register struct iorec   *filep;
        !            23:        char                    *name;
        !            24:        int                     maxnamlen;
        !            25:        int                     datasize;
        !            26: {
        !            27:        struct iorec    *prev;
        !            28:        struct iorec    *next;
        !            29:        register int    cnt;
        !            30:        struct iorec    locvar;
        !            31:        extern char     *mktemp();
        !            32: 
        !            33:        if (filep->fblk >= MAXFILES || _actfile[filep->fblk] != filep) {
        !            34:                /*
        !            35:                 * initialize a new filerecord
        !            36:                 */
        !            37:                filep->funit = 0;
        !            38:                if (datasize == 0) {
        !            39:                        filep->funit |= FTEXT;
        !            40:                        datasize = 1;
        !            41:                }
        !            42:                filep->fsize = datasize;
        !            43:                filep->fbuf = 0;
        !            44:                filep->lcount = 0;
        !            45:                filep->llimit = 0x7fffffff;
        !            46:                filep->fileptr = &filep->window[0];
        !            47:                /*
        !            48:                 * check to see if file is global, or allocated in
        !            49:                 * the stack by checking its address against the
        !            50:                 * address of one of our routine's local variables.
        !            51:                 */
        !            52:                if (filep < &locvar)
        !            53:                        filep->flev = GLVL;
        !            54:                else
        !            55:                        filep->flev = filep;
        !            56:                do {
        !            57:                        if (++_filefre == MAXFILES)
        !            58:                                _filefre = PREDEF + 1;
        !            59:                } while (_actfile[_filefre] != FILNIL);
        !            60:                filep->fblk = _filefre;
        !            61:                _actfile[_filefre] = filep;
        !            62:                /*
        !            63:                 * link the newrecord into the file chain
        !            64:                 */
        !            65:                prev = (struct iorec *)&_fchain;
        !            66:                next = _fchain.fchain;
        !            67:                while (filep->flev > next->flev) {
        !            68:                        prev = next;
        !            69:                        next = next->fchain;
        !            70:                }
        !            71:                filep->fchain = next;
        !            72:                prev->fchain = filep;
        !            73:        } else {
        !            74:                if (filep->funit & FDEF) {
        !            75:                        filep->funit &= (TEMP | FTEXT);
        !            76:                } else {
        !            77:                        /*
        !            78:                         * have a previous buffer, close associated file
        !            79:                         */
        !            80:                        fclose(filep->fbuf);
        !            81:                        if (ferror(filep->fbuf)) {
        !            82:                                ERROR(ECLOSE, filep->pfname);
        !            83:                                return;
        !            84:                        }
        !            85:                        /*
        !            86:                         * renamed temporary files are discarded
        !            87:                         */
        !            88:                        if ((filep->funit & TEMP) &&
        !            89:                            (name != NULL) &&
        !            90:                            (unlink(filep->pfname))) {
        !            91:                                ERROR(EREMOVE, filep->pfname);
        !            92:                                return;
        !            93:                        }
        !            94:                        filep->funit &= (TEMP | FTEXT);
        !            95:                }
        !            96:        }
        !            97:        /*
        !            98:         * get the filename associated with the buffer
        !            99:         */
        !           100:        if (name == NULL) {
        !           101:                if (*filep->fname != NULL) {
        !           102:                        return(filep);
        !           103:                }
        !           104:                /*
        !           105:                 * no name given and no previous name, so generate
        !           106:                 * a new one of the form tmp.xxxxxx
        !           107:                 */
        !           108:                filep->funit |= TEMP;
        !           109:                name = mktemp("tmp.XXXXXX");
        !           110:                maxnamlen = 10;
        !           111:        } else {
        !           112:                /*
        !           113:                 * trim trailing blanks, and insure that the name 
        !           114:                 * will fit into the file structure
        !           115:                 */
        !           116:                for (cnt = 0; cnt < maxnamlen; cnt++)
        !           117:                        if (name[cnt] == '\0' || name[cnt] == ' ')
        !           118:                                break;
        !           119:                if (cnt >= NAMSIZ) {
        !           120:                        ERROR(ENAMESIZE, name);
        !           121:                        return;
        !           122:                }
        !           123:                maxnamlen = cnt;
        !           124:                filep->funit &= ~TEMP;
        !           125:        }
        !           126:        /*
        !           127:         * put the new name into the structure
        !           128:         */
        !           129:        for (cnt = 0; cnt < maxnamlen; cnt++)
        !           130:                filep->fname[cnt] = name[cnt];
        !           131:        filep->fname[cnt] = '\0';
        !           132:        filep->pfname = &filep->fname[0];
        !           133:        return(filep);
        !           134: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.