11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*2107Smckusic static char sccsid[] = "@(#)GETNAME.c 1.2 01/10/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 * 201656Smckusick GETNAME(filep, name, maxnamlen, datasize) 211656Smckusick 221656Smckusick register struct iorec *filep; 231656Smckusick char *name; 241656Smckusick int maxnamlen; 251656Smckusick int datasize; 261656Smckusick { 271656Smckusick struct iorec *prev; 281656Smckusick struct iorec *next; 291656Smckusick register int cnt; 301656Smckusick struct iorec locvar; 311656Smckusick extern char *mktemp(); 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 { 741656Smckusick if (filep->funit & FDEF) { 751656Smckusick filep->funit &= (TEMP | FTEXT); 761656Smckusick } else { 771656Smckusick /* 781656Smckusick * have a previous buffer, close associated file 791656Smckusick */ 80*2107Smckusic if (filep->fblk > PREDEF) { 81*2107Smckusic fflush(filep->fbuf); 82*2107Smckusic setbuf(filep->fbuf, NULL); 83*2107Smckusic } 841656Smckusick fclose(filep->fbuf); 851656Smckusick if (ferror(filep->fbuf)) { 861656Smckusick ERROR(ECLOSE, filep->pfname); 871656Smckusick return; 881656Smckusick } 891656Smckusick /* 901656Smckusick * renamed temporary files are discarded 911656Smckusick */ 921656Smckusick if ((filep->funit & TEMP) && 931656Smckusick (name != NULL) && 941656Smckusick (unlink(filep->pfname))) { 951656Smckusick ERROR(EREMOVE, filep->pfname); 961656Smckusick return; 971656Smckusick } 981656Smckusick filep->funit &= (TEMP | FTEXT); 991656Smckusick } 1001656Smckusick } 1011656Smckusick /* 1021656Smckusick * get the filename associated with the buffer 1031656Smckusick */ 1041656Smckusick if (name == NULL) { 1051656Smckusick if (*filep->fname != NULL) { 1061656Smckusick return(filep); 1071656Smckusick } 1081656Smckusick /* 1091656Smckusick * no name given and no previous name, so generate 1101656Smckusick * a new one of the form tmp.xxxxxx 1111656Smckusick */ 1121656Smckusick filep->funit |= TEMP; 1131656Smckusick name = mktemp("tmp.XXXXXX"); 1141656Smckusick maxnamlen = 10; 1151656Smckusick } else { 1161656Smckusick /* 1171656Smckusick * trim trailing blanks, and insure that the name 1181656Smckusick * will fit into the file structure 1191656Smckusick */ 1201656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 1211656Smckusick if (name[cnt] == '\0' || name[cnt] == ' ') 1221656Smckusick break; 1231656Smckusick if (cnt >= NAMSIZ) { 1241656Smckusick ERROR(ENAMESIZE, name); 1251656Smckusick return; 1261656Smckusick } 1271656Smckusick maxnamlen = cnt; 1281656Smckusick filep->funit &= ~TEMP; 1291656Smckusick } 1301656Smckusick /* 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