1*40865Sbostic /*- 2*40865Sbostic * Copyright (c) 1979 The Regents of the University of California. 3*40865Sbostic * All rights reserved. 4*40865Sbostic * 5*40865Sbostic * %sccs.include.redist.c% 6*40865Sbostic */ 71656Smckusick 8*40865Sbostic #ifndef lint 9*40865Sbostic static char sccsid[] = "@(#)GETNAME.c 1.13 (Berkeley) 04/09/90"; 10*40865Sbostic #endif /* not lint */ 111656Smckusick 121656Smckusick #include "h00vars.h" 137969Smckusick #include "libpc.h" 141656Smckusick 151656Smckusick /* 161656Smckusick * GETNAME - activate a file 171656Smckusick * 181656Smckusick * takes a name, name length, element size, and variable 191656Smckusick * level and returns a pointer to a file structure. 201656Smckusick * 211656Smckusick * a new file structure is initialized if necessary. 221656Smckusick * temporary names are generated, and given 231656Smckusick * names are blank trimmed. 241656Smckusick */ 251656Smckusick 265058Smckusic static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 275058Smckusic 281656Smckusick struct iorec * 293006Smckusic GETNAME(filep, name, namlim, datasize) 301656Smckusick 311656Smckusick register struct iorec *filep; 321656Smckusick char *name; 333006Smckusic long namlim; 343006Smckusic long datasize; 351656Smckusick { 363006Smckusic int maxnamlen = namlim; 371656Smckusick struct iorec *prev; 381656Smckusick struct iorec *next; 391656Smckusick register int cnt; 401656Smckusick struct iorec locvar; 411656Smckusick 427969Smckusick if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) { 437969Smckusick /* 447969Smckusick * Close and immediately reactivate the file. 457969Smckusick */ 4610564Smckusick PFCLOSE(filep, name != NULL); 477969Smckusick _actfile[filep->fblk] = filep; 487969Smckusick filep->funit &= (TEMP | FTEXT); 497969Smckusick } else { 501656Smckusick /* 517969Smckusick * Initialize a new file record. 521656Smckusick */ 531656Smckusick filep->funit = 0; 541656Smckusick if (datasize == 0) { 551656Smckusick filep->funit |= FTEXT; 561656Smckusick datasize = 1; 571656Smckusick } 581656Smckusick filep->fsize = datasize; 591656Smckusick filep->fbuf = 0; 601656Smckusick filep->lcount = 0; 611656Smckusick filep->llimit = 0x7fffffff; 621656Smckusick filep->fileptr = &filep->window[0]; 6325235Smckusick *filep->fname = NULL; 641656Smckusick /* 657969Smckusick * Check to see if file is global, or allocated in 661656Smckusick * the stack by checking its address against the 671656Smckusick * address of one of our routine's local variables. 681656Smckusick */ 691656Smckusick if (filep < &locvar) 701656Smckusick filep->flev = GLVL; 711656Smckusick else 721656Smckusick filep->flev = filep; 735058Smckusic for (_filefre++; _filefre < MAXFILES; _filefre++) 745058Smckusic if (_actfile[_filefre] == FILNIL) 755058Smckusic goto gotone; 765058Smckusic for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++) 775058Smckusic if (_actfile[_filefre] == FILNIL) 785058Smckusic goto gotone; 795058Smckusic ERROR("File table overflow\n"); 805058Smckusic return; 815058Smckusic gotone: 821656Smckusick filep->fblk = _filefre; 831656Smckusick _actfile[_filefre] = filep; 841656Smckusick /* 8510572Smckusick * Link the new record into the file chain. 861656Smckusick */ 871656Smckusick prev = (struct iorec *)&_fchain; 881656Smckusick next = _fchain.fchain; 891656Smckusick while (filep->flev > next->flev) { 901656Smckusick prev = next; 911656Smckusick next = next->fchain; 921656Smckusick } 937969Smckusick if (filep->flev == GLVL) 941656Smckusick /* 957969Smckusick * Must order global files so that all dynamic files 967969Smckusick * within a record are grouped together. 971656Smckusick */ 9810572Smckusick while ((next != FILNIL) && 9910572Smckusick (next->flev == GLVL) && 10010572Smckusick ((struct iorec *)filep > next)) { 1017969Smckusick prev = next; 1027969Smckusick next = next->fchain; 1032107Smckusic } 1047969Smckusick filep->fchain = next; 1057969Smckusick prev->fchain = filep; 1061656Smckusick } 1071656Smckusick /* 1087969Smckusick * Get the filename associated with the buffer. 1091656Smckusick */ 1101656Smckusick if (name == NULL) { 1111656Smckusick if (*filep->fname != NULL) { 1121656Smckusick return(filep); 1131656Smckusick } 1141656Smckusick /* 1157969Smckusick * No name given and no previous name, so generate 1167969Smckusick * a new one of the form #tmp.xxxxxx. 1171656Smckusick */ 1181656Smckusick filep->funit |= TEMP; 1195058Smckusic sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk], 1205023Smckusic getpid()); 1213662Smckusic filep->pfname = &filep->fname[0]; 1223662Smckusic return(filep); 1231656Smckusick } 1241656Smckusick /* 1257969Smckusick * Trim trailing blanks, and insure that the name 1267969Smckusick * will fit into the file structure. 1273662Smckusic */ 1283662Smckusic for (cnt = 0; cnt < maxnamlen; cnt++) 1293662Smckusic if (name[cnt] == '\0' || name[cnt] == ' ') 1303662Smckusic break; 1313662Smckusic if (cnt >= NAMSIZ) { 1323867Smckusic ERROR("%s: File name too long\n", name); 1333662Smckusic return; 1343662Smckusic } 1353662Smckusic maxnamlen = cnt; 1363662Smckusic filep->funit &= ~TEMP; 1373662Smckusic /* 1387969Smckusick * Put the new name into the structure. 1391656Smckusick */ 1401656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1411656Smckusick filep->fname[cnt] = name[cnt]; 1421656Smckusick filep->fname[cnt] = '\0'; 1431656Smckusick filep->pfname = &filep->fname[0]; 1441656Smckusick return(filep); 1451656Smckusick } 146