11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*3006Smckusic static char sccsid[] = "@(#)GETNAME.c 1.3 03/07/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 * 20*3006Smckusic GETNAME(filep, name, namlim, datasize) 211656Smckusick 221656Smckusick register struct iorec *filep; 231656Smckusick char *name; 24*3006Smckusic long namlim; 25*3006Smckusic long datasize; 261656Smckusick { 27*3006Smckusic int maxnamlen = namlim; 281656Smckusick struct iorec *prev; 291656Smckusick struct iorec *next; 301656Smckusick register int cnt; 311656Smckusick struct iorec locvar; 321656Smckusick extern char *mktemp(); 331656Smckusick 341656Smckusick if (filep->fblk >= MAXFILES || _actfile[filep->fblk] != filep) { 351656Smckusick /* 361656Smckusick * initialize a new filerecord 371656Smckusick */ 381656Smckusick filep->funit = 0; 391656Smckusick if (datasize == 0) { 401656Smckusick filep->funit |= FTEXT; 411656Smckusick datasize = 1; 421656Smckusick } 431656Smckusick filep->fsize = datasize; 441656Smckusick filep->fbuf = 0; 451656Smckusick filep->lcount = 0; 461656Smckusick filep->llimit = 0x7fffffff; 471656Smckusick filep->fileptr = &filep->window[0]; 481656Smckusick /* 491656Smckusick * check to see if file is global, or allocated in 501656Smckusick * the stack by checking its address against the 511656Smckusick * address of one of our routine's local variables. 521656Smckusick */ 531656Smckusick if (filep < &locvar) 541656Smckusick filep->flev = GLVL; 551656Smckusick else 561656Smckusick filep->flev = filep; 571656Smckusick do { 581656Smckusick if (++_filefre == MAXFILES) 591656Smckusick _filefre = PREDEF + 1; 601656Smckusick } while (_actfile[_filefre] != FILNIL); 611656Smckusick filep->fblk = _filefre; 621656Smckusick _actfile[_filefre] = filep; 631656Smckusick /* 641656Smckusick * link the newrecord into the file chain 651656Smckusick */ 661656Smckusick prev = (struct iorec *)&_fchain; 671656Smckusick next = _fchain.fchain; 681656Smckusick while (filep->flev > next->flev) { 691656Smckusick prev = next; 701656Smckusick next = next->fchain; 711656Smckusick } 721656Smckusick filep->fchain = next; 731656Smckusick prev->fchain = filep; 741656Smckusick } else { 75*3006Smckusic if ((filep->funit & FDEF) == 0) { 761656Smckusick /* 771656Smckusick * have a previous buffer, close associated file 781656Smckusick */ 792107Smckusic if (filep->fblk > PREDEF) { 802107Smckusic fflush(filep->fbuf); 812107Smckusic setbuf(filep->fbuf, NULL); 822107Smckusic } 831656Smckusick fclose(filep->fbuf); 841656Smckusick if (ferror(filep->fbuf)) { 851656Smckusick ERROR(ECLOSE, filep->pfname); 861656Smckusick return; 871656Smckusick } 881656Smckusick /* 891656Smckusick * renamed temporary files are discarded 901656Smckusick */ 911656Smckusick if ((filep->funit & TEMP) && 921656Smckusick (name != NULL) && 931656Smckusick (unlink(filep->pfname))) { 941656Smckusick ERROR(EREMOVE, filep->pfname); 951656Smckusick return; 961656Smckusick } 971656Smckusick } 98*3006Smckusic filep->funit &= (TEMP | FTEXT); 991656Smckusick } 1001656Smckusick /* 1011656Smckusick * get the filename associated with the buffer 1021656Smckusick */ 1031656Smckusick if (name == NULL) { 1041656Smckusick if (*filep->fname != NULL) { 1051656Smckusick return(filep); 1061656Smckusick } 1071656Smckusick /* 1081656Smckusick * no name given and no previous name, so generate 1091656Smckusick * a new one of the form tmp.xxxxxx 1101656Smckusick */ 1111656Smckusick filep->funit |= TEMP; 1121656Smckusick name = mktemp("tmp.XXXXXX"); 1131656Smckusick maxnamlen = 10; 1141656Smckusick } else { 1151656Smckusick /* 1161656Smckusick * trim trailing blanks, and insure that the name 1171656Smckusick * will fit into the file structure 1181656Smckusick */ 1191656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1201656Smckusick if (name[cnt] == '\0' || name[cnt] == ' ') 1211656Smckusick break; 1221656Smckusick if (cnt >= NAMSIZ) { 1231656Smckusick ERROR(ENAMESIZE, name); 1241656Smckusick return; 1251656Smckusick } 1261656Smckusick maxnamlen = cnt; 1271656Smckusick filep->funit &= ~TEMP; 1281656Smckusick } 1291656Smckusick /* 1301656Smckusick * 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