11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*3860Smckusic static char sccsid[] = "@(#)GETNAME.c 1.5 06/08/81"; 41656Smckusick 51656Smckusick #include "h00vars.h" 61656Smckusick #include "h01errs.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 191656Smckusick struct iorec * 203006Smckusic GETNAME(filep, name, namlim, datasize) 211656Smckusick 221656Smckusick register struct iorec *filep; 231656Smckusick char *name; 243006Smckusic long namlim; 253006Smckusic long datasize; 261656Smckusick { 273006Smckusic int maxnamlen = namlim; 281656Smckusick struct iorec *prev; 291656Smckusick struct iorec *next; 301656Smckusick register int cnt; 311656Smckusick struct iorec locvar; 321656Smckusick 331656Smckusick if (filep->fblk >= MAXFILES || _actfile[filep->fblk] != filep) { 341656Smckusick /* 351656Smckusick * initialize a new filerecord 361656Smckusick */ 371656Smckusick filep->funit = 0; 381656Smckusick if (datasize == 0) { 391656Smckusick filep->funit |= FTEXT; 401656Smckusick datasize = 1; 411656Smckusick } 421656Smckusick filep->fsize = datasize; 431656Smckusick filep->fbuf = 0; 441656Smckusick filep->lcount = 0; 451656Smckusick filep->llimit = 0x7fffffff; 461656Smckusick filep->fileptr = &filep->window[0]; 471656Smckusick /* 481656Smckusick * check to see if file is global, or allocated in 491656Smckusick * the stack by checking its address against the 501656Smckusick * address of one of our routine's local variables. 511656Smckusick */ 521656Smckusick if (filep < &locvar) 531656Smckusick filep->flev = GLVL; 541656Smckusick else 551656Smckusick filep->flev = filep; 561656Smckusick do { 571656Smckusick if (++_filefre == MAXFILES) 581656Smckusick _filefre = PREDEF + 1; 591656Smckusick } while (_actfile[_filefre] != FILNIL); 601656Smckusick filep->fblk = _filefre; 611656Smckusick _actfile[_filefre] = filep; 621656Smckusick /* 631656Smckusick * link the newrecord into the file chain 641656Smckusick */ 651656Smckusick prev = (struct iorec *)&_fchain; 661656Smckusick next = _fchain.fchain; 671656Smckusick while (filep->flev > next->flev) { 681656Smckusick prev = next; 691656Smckusick next = next->fchain; 701656Smckusick } 711656Smckusick filep->fchain = next; 721656Smckusick prev->fchain = filep; 731656Smckusick } else { 74*3860Smckusic if ((filep->funit & FDEF) == 0 && filep->fbuf != NULL) { 751656Smckusick /* 761656Smckusick * have a previous buffer, close associated file 771656Smckusick */ 782107Smckusic if (filep->fblk > PREDEF) { 792107Smckusic fflush(filep->fbuf); 802107Smckusic setbuf(filep->fbuf, NULL); 812107Smckusic } 821656Smckusick fclose(filep->fbuf); 831656Smckusick if (ferror(filep->fbuf)) { 841656Smckusick ERROR(ECLOSE, filep->pfname); 851656Smckusick return; 861656Smckusick } 871656Smckusick /* 881656Smckusick * renamed temporary files are discarded 891656Smckusick */ 90*3860Smckusic if ((filep->funit & TEMP) && name != NULL) { 91*3860Smckusic if (unlink(filep->pfname)) { 92*3860Smckusic ERROR(EREMOVE, filep->pfname); 93*3860Smckusic return; 94*3860Smckusic } 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 108*3860Smckusic * a new one of the form #tmp.xxxxxx 1091656Smckusick */ 1101656Smckusick filep->funit |= TEMP; 111*3860Smckusic sprintf(filep->fname, "#tmp.%c%d", 'a' + filep->fblk, getpid()); 1123662Smckusic filep->pfname = &filep->fname[0]; 1133662Smckusic return(filep); 1141656Smckusick } 1151656Smckusick /* 1163662Smckusic * trim trailing blanks, and insure that the name 1173662Smckusic * will fit into the file structure 1183662Smckusic */ 1193662Smckusic for (cnt = 0; cnt < maxnamlen; cnt++) 1203662Smckusic if (name[cnt] == '\0' || name[cnt] == ' ') 1213662Smckusic break; 1223662Smckusic if (cnt >= NAMSIZ) { 1233662Smckusic ERROR(ENAMESIZE, name); 1243662Smckusic return; 1253662Smckusic } 1263662Smckusic maxnamlen = cnt; 1273662Smckusic filep->funit &= ~TEMP; 1283662Smckusic /* 1291656Smckusick * put the new name into the structure 1301656Smckusick */ 1311656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1321656Smckusick filep->fname[cnt] = name[cnt]; 1331656Smckusick filep->fname[cnt] = '\0'; 1341656Smckusick filep->pfname = &filep->fname[0]; 1351656Smckusick return(filep); 1361656Smckusick } 137