11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*5023Smckusic static char sccsid[] = "@(#)GETNAME.c 1.7 11/22/81"; 41656Smckusick 51656Smckusick #include "h00vars.h" 61656Smckusick 71656Smckusick /* 81656Smckusick * GETNAME - activate a file 91656Smckusick * 101656Smckusick * takes a name, name length, element size, and variable 111656Smckusick * level and returns a pointer to a file structure. 121656Smckusick * 131656Smckusick * a new file structure is initialized if necessary. 141656Smckusick * temporary names are generated, and given 151656Smckusick * names are blank trimmed. 161656Smckusick */ 171656Smckusick 181656Smckusick struct iorec * 193006Smckusic GETNAME(filep, name, namlim, datasize) 201656Smckusick 211656Smckusick register struct iorec *filep; 221656Smckusick char *name; 233006Smckusic long namlim; 243006Smckusic long datasize; 251656Smckusick { 263006Smckusic int maxnamlen = namlim; 271656Smckusick struct iorec *prev; 281656Smckusick struct iorec *next; 291656Smckusick register int cnt; 301656Smckusick struct iorec locvar; 311656Smckusick 321656Smckusick if (filep->fblk >= MAXFILES || _actfile[filep->fblk] != filep) { 331656Smckusick /* 341656Smckusick * initialize a new filerecord 351656Smckusick */ 361656Smckusick filep->funit = 0; 371656Smckusick if (datasize == 0) { 381656Smckusick filep->funit |= FTEXT; 391656Smckusick datasize = 1; 401656Smckusick } 411656Smckusick filep->fsize = datasize; 421656Smckusick filep->fbuf = 0; 431656Smckusick filep->lcount = 0; 441656Smckusick filep->llimit = 0x7fffffff; 451656Smckusick filep->fileptr = &filep->window[0]; 461656Smckusick /* 471656Smckusick * check to see if file is global, or allocated in 481656Smckusick * the stack by checking its address against the 491656Smckusick * address of one of our routine's local variables. 501656Smckusick */ 511656Smckusick if (filep < &locvar) 521656Smckusick filep->flev = GLVL; 531656Smckusick else 541656Smckusick filep->flev = filep; 551656Smckusick do { 561656Smckusick if (++_filefre == MAXFILES) 571656Smckusick _filefre = PREDEF + 1; 581656Smckusick } while (_actfile[_filefre] != FILNIL); 591656Smckusick filep->fblk = _filefre; 601656Smckusick _actfile[_filefre] = filep; 611656Smckusick /* 621656Smckusick * link the newrecord into the file chain 631656Smckusick */ 641656Smckusick prev = (struct iorec *)&_fchain; 651656Smckusick next = _fchain.fchain; 661656Smckusick while (filep->flev > next->flev) { 671656Smckusick prev = next; 681656Smckusick next = next->fchain; 691656Smckusick } 701656Smckusick filep->fchain = next; 711656Smckusick prev->fchain = filep; 721656Smckusick } else { 733860Smckusic if ((filep->funit & FDEF) == 0 && filep->fbuf != NULL) { 741656Smckusick /* 751656Smckusick * have a previous buffer, close associated file 761656Smckusick */ 772107Smckusic if (filep->fblk > PREDEF) { 782107Smckusic fflush(filep->fbuf); 792107Smckusic setbuf(filep->fbuf, NULL); 802107Smckusic } 811656Smckusick fclose(filep->fbuf); 821656Smckusick if (ferror(filep->fbuf)) { 833867Smckusic ERROR("%s: Close failed\n", filep->pfname); 841656Smckusick return; 851656Smckusick } 861656Smckusick /* 871656Smckusick * renamed temporary files are discarded 881656Smckusick */ 893860Smckusic if ((filep->funit & TEMP) && name != NULL) { 903860Smckusic if (unlink(filep->pfname)) { 913867Smckusic PERROR("Could not remove ", 923867Smckusic filep->pfname); 933860Smckusic return; 943860Smckusic } 951656Smckusick } 961656Smckusick } 973006Smckusic filep->funit &= (TEMP | FTEXT); 981656Smckusick } 991656Smckusick /* 1001656Smckusick * get the filename associated with the buffer 1011656Smckusick */ 1021656Smckusick if (name == NULL) { 1031656Smckusick if (*filep->fname != NULL) { 1041656Smckusick return(filep); 1051656Smckusick } 1061656Smckusick /* 1071656Smckusick * no name given and no previous name, so generate 1083860Smckusic * a new one of the form #tmp.xxxxxx 1091656Smckusick */ 1101656Smckusick filep->funit |= TEMP; 111*5023Smckusic sprintf(filep->fname, "#tmp.%c%d", filep->fblk <= 'z' - 'a' + 1 112*5023Smckusic ? 'a' + filep->fblk : 'A' + filep->fblk - ('z' - 'a' + 1), 113*5023Smckusic getpid()); 1143662Smckusic filep->pfname = &filep->fname[0]; 1153662Smckusic return(filep); 1161656Smckusick } 1171656Smckusick /* 1183662Smckusic * trim trailing blanks, and insure that the name 1193662Smckusic * 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 /* 1311656Smckusick * 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