11656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 21656Smckusick 3*3662Smckusic static char sccsid[] = "@(#)GETNAME.c 1.4 05/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 * 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 { 743006Smckusic if ((filep->funit & FDEF) == 0) { 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 */ 901656Smckusick if ((filep->funit & TEMP) && 911656Smckusick (name != NULL) && 921656Smckusick (unlink(filep->pfname))) { 931656Smckusick ERROR(EREMOVE, filep->pfname); 941656Smckusick return; 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 1081656Smckusick * a new one of the form tmp.xxxxxx 1091656Smckusick */ 1101656Smckusick filep->funit |= TEMP; 111*3662Smckusic sprintf(filep->fname, "tmp.%c%d", 'a' + filep->fblk, getpid()); 112*3662Smckusic filep->pfname = &filep->fname[0]; 113*3662Smckusic return(filep); 1141656Smckusick } 1151656Smckusick /* 116*3662Smckusic * trim trailing blanks, and insure that the name 117*3662Smckusic * will fit into the file structure 118*3662Smckusic */ 119*3662Smckusic for (cnt = 0; cnt < maxnamlen; cnt++) 120*3662Smckusic if (name[cnt] == '\0' || name[cnt] == ' ') 121*3662Smckusic break; 122*3662Smckusic if (cnt >= NAMSIZ) { 123*3662Smckusic ERROR(ENAMESIZE, name); 124*3662Smckusic return; 125*3662Smckusic } 126*3662Smckusic maxnamlen = cnt; 127*3662Smckusic filep->funit &= ~TEMP; 128*3662Smckusic /* 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