11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*10572Smckusick static char sccsid[] = "@(#)GETNAME.c 1.11 01/22/83"; 41656Smckusick 51656Smckusick #include "h00vars.h" 67969Smckusick #include "libpc.h" 71656Smckusick 81656Smckusick /* 91656Smckusick * GETNAME - activate a file 101656Smckusick * 111656Smckusick * takes a name, name length, element size, and variable 121656Smckusick * level and returns a pointer to a file structure. 131656Smckusick * 141656Smckusick * a new file structure is initialized if necessary. 151656Smckusick * temporary names are generated, and given 161656Smckusick * names are blank trimmed. 171656Smckusick */ 181656Smckusick 195058Smckusic static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 205058Smckusic 211656Smckusick struct iorec * 223006Smckusic GETNAME(filep, name, namlim, datasize) 231656Smckusick 241656Smckusick register struct iorec *filep; 251656Smckusick char *name; 263006Smckusic long namlim; 273006Smckusic long datasize; 281656Smckusick { 293006Smckusic int maxnamlen = namlim; 301656Smckusick struct iorec *prev; 311656Smckusick struct iorec *next; 321656Smckusick register int cnt; 331656Smckusick struct iorec locvar; 341656Smckusick 357969Smckusick if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) { 367969Smckusick /* 377969Smckusick * Close and immediately reactivate the file. 387969Smckusick */ 3910564Smckusick PFCLOSE(filep, name != NULL); 407969Smckusick _actfile[filep->fblk] = filep; 417969Smckusick filep->funit &= (TEMP | FTEXT); 427969Smckusick } else { 431656Smckusick /* 447969Smckusick * Initialize a new file record. 451656Smckusick */ 461656Smckusick filep->funit = 0; 471656Smckusick if (datasize == 0) { 481656Smckusick filep->funit |= FTEXT; 491656Smckusick datasize = 1; 501656Smckusick } 511656Smckusick filep->fsize = datasize; 521656Smckusick filep->fbuf = 0; 531656Smckusick filep->lcount = 0; 541656Smckusick filep->llimit = 0x7fffffff; 551656Smckusick filep->fileptr = &filep->window[0]; 561656Smckusick /* 577969Smckusick * Check to see if file is global, or allocated in 581656Smckusick * the stack by checking its address against the 591656Smckusick * address of one of our routine's local variables. 601656Smckusick */ 611656Smckusick if (filep < &locvar) 621656Smckusick filep->flev = GLVL; 631656Smckusick else 641656Smckusick filep->flev = filep; 655058Smckusic for (_filefre++; _filefre < MAXFILES; _filefre++) 665058Smckusic if (_actfile[_filefre] == FILNIL) 675058Smckusic goto gotone; 685058Smckusic for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++) 695058Smckusic if (_actfile[_filefre] == FILNIL) 705058Smckusic goto gotone; 715058Smckusic ERROR("File table overflow\n"); 725058Smckusic return; 735058Smckusic gotone: 741656Smckusick filep->fblk = _filefre; 751656Smckusick _actfile[_filefre] = filep; 761656Smckusick /* 77*10572Smckusick * Link the new record into the file chain. 781656Smckusick */ 791656Smckusick prev = (struct iorec *)&_fchain; 801656Smckusick next = _fchain.fchain; 811656Smckusick while (filep->flev > next->flev) { 821656Smckusick prev = next; 831656Smckusick next = next->fchain; 841656Smckusick } 857969Smckusick if (filep->flev == GLVL) 861656Smckusick /* 877969Smckusick * Must order global files so that all dynamic files 887969Smckusick * within a record are grouped together. 891656Smckusick */ 90*10572Smckusick while ((next != FILNIL) && 91*10572Smckusick (next->flev == GLVL) && 92*10572Smckusick ((struct iorec *)filep > next)) { 937969Smckusick prev = next; 947969Smckusick next = next->fchain; 952107Smckusic } 967969Smckusick filep->fchain = next; 977969Smckusick prev->fchain = filep; 981656Smckusick } 991656Smckusick /* 1007969Smckusick * Get the filename associated with the buffer. 1011656Smckusick */ 1021656Smckusick if (name == NULL) { 1031656Smckusick if (*filep->fname != NULL) { 1041656Smckusick return(filep); 1051656Smckusick } 1061656Smckusick /* 1077969Smckusick * No name given and no previous name, so generate 1087969Smckusick * a new one of the form #tmp.xxxxxx. 1091656Smckusick */ 1101656Smckusick filep->funit |= TEMP; 1115058Smckusic sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk], 1125023Smckusic getpid()); 1133662Smckusic filep->pfname = &filep->fname[0]; 1143662Smckusic return(filep); 1151656Smckusick } 1161656Smckusick /* 1177969Smckusick * Trim trailing blanks, and insure that the name 1187969Smckusick * will fit into the file structure. 1193662Smckusic */ 1203662Smckusic for (cnt = 0; cnt < maxnamlen; cnt++) 1213662Smckusic if (name[cnt] == '\0' || name[cnt] == ' ') 1223662Smckusic break; 1233662Smckusic if (cnt >= NAMSIZ) { 1243867Smckusic ERROR("%s: File name too long\n", name); 1253662Smckusic return; 1263662Smckusic } 1273662Smckusic maxnamlen = cnt; 1283662Smckusic filep->funit &= ~TEMP; 1293662Smckusic /* 1307969Smckusick * Put the new name into the structure. 1311656Smckusick */ 1321656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1331656Smckusick filep->fname[cnt] = name[cnt]; 1341656Smckusick filep->fname[cnt] = '\0'; 1351656Smckusick filep->pfname = &filep->fname[0]; 1361656Smckusick return(filep); 1371656Smckusick } 138