Annotation of 43BSDReno/lib/libpc/GETNAME.c, revision 1.1.1.1

1.1       root        1: /*-
                      2:  * Copyright (c) 1979 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)GETNAME.c  1.13 (Berkeley) 4/9/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include "h00vars.h"
                     25: #include "libpc.h"
                     26: 
                     27: /*
                     28:  * GETNAME - activate a file
                     29:  *
                     30:  * takes a name, name length, element size, and variable
                     31:  * level and returns a pointer to a file structure.
                     32:  *
                     33:  * a new file structure is initialized if necessary.
                     34:  * temporary names are generated, and given
                     35:  * names are blank trimmed.
                     36:  */
                     37: 
                     38: static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                     39: 
                     40: struct iorec *
                     41: GETNAME(filep, name, namlim, datasize)
                     42: 
                     43:        register struct iorec   *filep;
                     44:        char                    *name;
                     45:        long                    namlim;
                     46:        long                    datasize;
                     47: {
                     48:        int             maxnamlen = namlim;
                     49:        struct iorec    *prev;
                     50:        struct iorec    *next;
                     51:        register int    cnt;
                     52:        struct iorec    locvar;
                     53: 
                     54:        if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) {
                     55:                /* 
                     56:                 * Close and immediately reactivate the file.
                     57:                 */
                     58:                PFCLOSE(filep, name != NULL);
                     59:                _actfile[filep->fblk] = filep;
                     60:                filep->funit &= (TEMP | FTEXT);
                     61:        } else {
                     62:                /*
                     63:                 * Initialize a new file record.
                     64:                 */
                     65:                filep->funit = 0;
                     66:                if (datasize == 0) {
                     67:                        filep->funit |= FTEXT;
                     68:                        datasize = 1;
                     69:                }
                     70:                filep->fsize = datasize;
                     71:                filep->fbuf = 0;
                     72:                filep->lcount = 0;
                     73:                filep->llimit = 0x7fffffff;
                     74:                filep->fileptr = &filep->window[0];
                     75:                *filep->fname = NULL;
                     76:                /*
                     77:                 * Check to see if file is global, or allocated in
                     78:                 * the stack by checking its address against the
                     79:                 * address of one of our routine's local variables.
                     80:                 */
                     81:                if (filep < &locvar)
                     82:                        filep->flev = GLVL;
                     83:                else
                     84:                        filep->flev = filep;
                     85:                for (_filefre++; _filefre < MAXFILES; _filefre++)
                     86:                        if (_actfile[_filefre] == FILNIL)
                     87:                                goto gotone;
                     88:                for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++)
                     89:                        if (_actfile[_filefre] == FILNIL)
                     90:                                goto gotone;
                     91:                ERROR("File table overflow\n");
                     92:                return;
                     93: gotone:
                     94:                filep->fblk = _filefre;
                     95:                _actfile[_filefre] = filep;
                     96:                /*
                     97:                 * Link the new record into the file chain.
                     98:                 */
                     99:                prev = (struct iorec *)&_fchain;
                    100:                next = _fchain.fchain;
                    101:                while (filep->flev > next->flev) {
                    102:                        prev = next;
                    103:                        next = next->fchain;
                    104:                }
                    105:                if (filep->flev == GLVL)
                    106:                        /*
                    107:                         * Must order global files so that all dynamic files
                    108:                         * within a record are grouped together.
                    109:                         */
                    110:                        while ((next != FILNIL) &&
                    111:                               (next->flev == GLVL) &&
                    112:                               ((struct iorec *)filep > next)) {
                    113:                                prev = next;
                    114:                                next = next->fchain;
                    115:                        }
                    116:                filep->fchain = next;
                    117:                prev->fchain = filep;
                    118:        }
                    119:        /*
                    120:         * Get the filename associated with the buffer.
                    121:         */
                    122:        if (name == NULL) {
                    123:                if (*filep->fname != NULL) {
                    124:                        return(filep);
                    125:                }
                    126:                /*
                    127:                 * No name given and no previous name, so generate
                    128:                 * a new one of the form #tmp.xxxxxx.
                    129:                 */
                    130:                filep->funit |= TEMP;
                    131:                sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk],
                    132:                    getpid());
                    133:                filep->pfname = &filep->fname[0];
                    134:                return(filep);
                    135:        }
                    136:        /*
                    137:         * Trim trailing blanks, and insure that the name 
                    138:         * will fit into the file structure.
                    139:         */
                    140:        for (cnt = 0; cnt < maxnamlen; cnt++)
                    141:                if (name[cnt] == '\0' || name[cnt] == ' ')
                    142:                        break;
                    143:        if (cnt >= NAMSIZ) {
                    144:                ERROR("%s: File name too long\n", name);
                    145:                return;
                    146:        }
                    147:        maxnamlen = cnt;
                    148:        filep->funit &= ~TEMP;
                    149:        /*
                    150:         * Put the new name into the structure.
                    151:         */
                    152:        for (cnt = 0; cnt < maxnamlen; cnt++)
                    153:                filep->fname[cnt] = name[cnt];
                    154:        filep->fname[cnt] = '\0';
                    155:        filep->pfname = &filep->fname[0];
                    156:        return(filep);
                    157: }

unix.superglobalmegacorp.com

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