11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*10564Smckusick static char sccsid[] = "@(#)GETNAME.c 1.10 01/21/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 */ 39*10564Smckusick 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 /* 777969Smckusick * Link the newrecord 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 */ 907969Smckusick while (next != FILNIL && (struct iorec *)filep > next) { 917969Smckusick prev = next; 927969Smckusick next = next->fchain; 932107Smckusic } 947969Smckusick filep->fchain = next; 957969Smckusick prev->fchain = filep; 961656Smckusick } 971656Smckusick /* 987969Smckusick * Get the filename associated with the buffer. 991656Smckusick */ 1001656Smckusick if (name == NULL) { 1011656Smckusick if (*filep->fname != NULL) { 1021656Smckusick return(filep); 1031656Smckusick } 1041656Smckusick /* 1057969Smckusick * No name given and no previous name, so generate 1067969Smckusick * a new one of the form #tmp.xxxxxx. 1071656Smckusick */ 1081656Smckusick filep->funit |= TEMP; 1095058Smckusic sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk], 1105023Smckusic getpid()); 1113662Smckusic filep->pfname = &filep->fname[0]; 1123662Smckusic return(filep); 1131656Smckusick } 1141656Smckusick /* 1157969Smckusick * Trim trailing blanks, and insure that the name 1167969Smckusick * will fit into the file structure. 1173662Smckusic */ 1183662Smckusic for (cnt = 0; cnt < maxnamlen; cnt++) 1193662Smckusic if (name[cnt] == '\0' || name[cnt] == ' ') 1203662Smckusic break; 1213662Smckusic if (cnt >= NAMSIZ) { 1223867Smckusic ERROR("%s: File name too long\n", name); 1233662Smckusic return; 1243662Smckusic } 1253662Smckusic maxnamlen = cnt; 1263662Smckusic filep->funit &= ~TEMP; 1273662Smckusic /* 1287969Smckusick * Put the new name into the structure. 1291656Smckusick */ 1301656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1311656Smckusick filep->fname[cnt] = name[cnt]; 1321656Smckusick filep->fname[cnt] = '\0'; 1331656Smckusick filep->pfname = &filep->fname[0]; 1341656Smckusick return(filep); 1351656Smckusick } 136