11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*25235Smckusick static char sccsid[] = "@(#)GETNAME.c 1.12 10/18/85"; 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]; 56*25235Smckusick *filep->fname = NULL; 571656Smckusick /* 587969Smckusick * Check to see if file is global, or allocated in 591656Smckusick * the stack by checking its address against the 601656Smckusick * address of one of our routine's local variables. 611656Smckusick */ 621656Smckusick if (filep < &locvar) 631656Smckusick filep->flev = GLVL; 641656Smckusick else 651656Smckusick filep->flev = filep; 665058Smckusic for (_filefre++; _filefre < MAXFILES; _filefre++) 675058Smckusic if (_actfile[_filefre] == FILNIL) 685058Smckusic goto gotone; 695058Smckusic for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++) 705058Smckusic if (_actfile[_filefre] == FILNIL) 715058Smckusic goto gotone; 725058Smckusic ERROR("File table overflow\n"); 735058Smckusic return; 745058Smckusic gotone: 751656Smckusick filep->fblk = _filefre; 761656Smckusick _actfile[_filefre] = filep; 771656Smckusick /* 7810572Smckusick * Link the new record into the file chain. 791656Smckusick */ 801656Smckusick prev = (struct iorec *)&_fchain; 811656Smckusick next = _fchain.fchain; 821656Smckusick while (filep->flev > next->flev) { 831656Smckusick prev = next; 841656Smckusick next = next->fchain; 851656Smckusick } 867969Smckusick if (filep->flev == GLVL) 871656Smckusick /* 887969Smckusick * Must order global files so that all dynamic files 897969Smckusick * within a record are grouped together. 901656Smckusick */ 9110572Smckusick while ((next != FILNIL) && 9210572Smckusick (next->flev == GLVL) && 9310572Smckusick ((struct iorec *)filep > next)) { 947969Smckusick prev = next; 957969Smckusick next = next->fchain; 962107Smckusic } 977969Smckusick filep->fchain = next; 987969Smckusick prev->fchain = filep; 991656Smckusick } 1001656Smckusick /* 1017969Smckusick * Get the filename associated with the buffer. 1021656Smckusick */ 1031656Smckusick if (name == NULL) { 1041656Smckusick if (*filep->fname != NULL) { 1051656Smckusick return(filep); 1061656Smckusick } 1071656Smckusick /* 1087969Smckusick * No name given and no previous name, so generate 1097969Smckusick * a new one of the form #tmp.xxxxxx. 1101656Smckusick */ 1111656Smckusick filep->funit |= TEMP; 1125058Smckusic sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk], 1135023Smckusic getpid()); 1143662Smckusic filep->pfname = &filep->fname[0]; 1153662Smckusic return(filep); 1161656Smckusick } 1171656Smckusick /* 1187969Smckusick * Trim trailing blanks, and insure that the name 1197969Smckusick * will fit into the file structure. 1203662Smckusic */ 1213662Smckusic for (cnt = 0; cnt < maxnamlen; cnt++) 1223662Smckusic if (name[cnt] == '\0' || name[cnt] == ' ') 1233662Smckusic break; 1243662Smckusic if (cnt >= NAMSIZ) { 1253867Smckusic ERROR("%s: File name too long\n", name); 1263662Smckusic return; 1273662Smckusic } 1283662Smckusic maxnamlen = cnt; 1293662Smckusic filep->funit &= ~TEMP; 1303662Smckusic /* 1317969Smckusick * Put the new name into the structure. 1321656Smckusick */ 1331656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1341656Smckusick filep->fname[cnt] = name[cnt]; 1351656Smckusick filep->fname[cnt] = '\0'; 1361656Smckusick filep->pfname = &filep->fname[0]; 1371656Smckusick return(filep); 1381656Smckusick } 139