1*1656Smckusick /* Copyright (c) 1979 Regents of the University of California */ 2*1656Smckusick 3*1656Smckusick static char sccsid[] = "@(#)GETNAME.c 1.1 10/30/80"; 4*1656Smckusick 5*1656Smckusick #include "h00vars.h" 6*1656Smckusick #include "h01errs.h" 7*1656Smckusick 8*1656Smckusick /* 9*1656Smckusick * GETNAME - activate a file 10*1656Smckusick * 11*1656Smckusick * takes a name, name length, element size, and variable 12*1656Smckusick * level and returns a pointer to a file structure. 13*1656Smckusick * 14*1656Smckusick * a new file structure is initialized if necessary. 15*1656Smckusick * temporary names are generated, and given 16*1656Smckusick * names are blank trimmed. 17*1656Smckusick */ 18*1656Smckusick 19*1656Smckusick struct iorec * 20*1656Smckusick GETNAME(filep, name, maxnamlen, datasize) 21*1656Smckusick 22*1656Smckusick register struct iorec *filep; 23*1656Smckusick char *name; 24*1656Smckusick int maxnamlen; 25*1656Smckusick int datasize; 26*1656Smckusick { 27*1656Smckusick struct iorec *prev; 28*1656Smckusick struct iorec *next; 29*1656Smckusick register int cnt; 30*1656Smckusick struct iorec locvar; 31*1656Smckusick extern char *mktemp(); 32*1656Smckusick 33*1656Smckusick if (filep->fblk >= MAXFILES || _actfile[filep->fblk] != filep) { 34*1656Smckusick /* 35*1656Smckusick * initialize a new filerecord 36*1656Smckusick */ 37*1656Smckusick filep->funit = 0; 38*1656Smckusick if (datasize == 0) { 39*1656Smckusick filep->funit |= FTEXT; 40*1656Smckusick datasize = 1; 41*1656Smckusick } 42*1656Smckusick filep->fsize = datasize; 43*1656Smckusick filep->fbuf = 0; 44*1656Smckusick filep->lcount = 0; 45*1656Smckusick filep->llimit = 0x7fffffff; 46*1656Smckusick filep->fileptr = &filep->window[0]; 47*1656Smckusick /* 48*1656Smckusick * check to see if file is global, or allocated in 49*1656Smckusick * the stack by checking its address against the 50*1656Smckusick * address of one of our routine's local variables. 51*1656Smckusick */ 52*1656Smckusick if (filep < &locvar) 53*1656Smckusick filep->flev = GLVL; 54*1656Smckusick else 55*1656Smckusick filep->flev = filep; 56*1656Smckusick do { 57*1656Smckusick if (++_filefre == MAXFILES) 58*1656Smckusick _filefre = PREDEF + 1; 59*1656Smckusick } while (_actfile[_filefre] != FILNIL); 60*1656Smckusick filep->fblk = _filefre; 61*1656Smckusick _actfile[_filefre] = filep; 62*1656Smckusick /* 63*1656Smckusick * link the newrecord into the file chain 64*1656Smckusick */ 65*1656Smckusick prev = (struct iorec *)&_fchain; 66*1656Smckusick next = _fchain.fchain; 67*1656Smckusick while (filep->flev > next->flev) { 68*1656Smckusick prev = next; 69*1656Smckusick next = next->fchain; 70*1656Smckusick } 71*1656Smckusick filep->fchain = next; 72*1656Smckusick prev->fchain = filep; 73*1656Smckusick } else { 74*1656Smckusick if (filep->funit & FDEF) { 75*1656Smckusick filep->funit &= (TEMP | FTEXT); 76*1656Smckusick } else { 77*1656Smckusick /* 78*1656Smckusick * have a previous buffer, close associated file 79*1656Smckusick */ 80*1656Smckusick fclose(filep->fbuf); 81*1656Smckusick if (ferror(filep->fbuf)) { 82*1656Smckusick ERROR(ECLOSE, filep->pfname); 83*1656Smckusick return; 84*1656Smckusick } 85*1656Smckusick /* 86*1656Smckusick * renamed temporary files are discarded 87*1656Smckusick */ 88*1656Smckusick if ((filep->funit & TEMP) && 89*1656Smckusick (name != NULL) && 90*1656Smckusick (unlink(filep->pfname))) { 91*1656Smckusick ERROR(EREMOVE, filep->pfname); 92*1656Smckusick return; 93*1656Smckusick } 94*1656Smckusick filep->funit &= (TEMP | FTEXT); 95*1656Smckusick } 96*1656Smckusick } 97*1656Smckusick /* 98*1656Smckusick * get the filename associated with the buffer 99*1656Smckusick */ 100*1656Smckusick if (name == NULL) { 101*1656Smckusick if (*filep->fname != NULL) { 102*1656Smckusick return(filep); 103*1656Smckusick } 104*1656Smckusick /* 105*1656Smckusick * no name given and no previous name, so generate 106*1656Smckusick * a new one of the form tmp.xxxxxx 107*1656Smckusick */ 108*1656Smckusick filep->funit |= TEMP; 109*1656Smckusick name = mktemp("tmp.XXXXXX"); 110*1656Smckusick maxnamlen = 10; 111*1656Smckusick } else { 112*1656Smckusick /* 113*1656Smckusick * trim trailing blanks, and insure that the name 114*1656Smckusick * will fit into the file structure 115*1656Smckusick */ 116*1656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 117*1656Smckusick if (name[cnt] == '\0' || name[cnt] == ' ') 118*1656Smckusick break; 119*1656Smckusick if (cnt >= NAMSIZ) { 120*1656Smckusick ERROR(ENAMESIZE, name); 121*1656Smckusick return; 122*1656Smckusick } 123*1656Smckusick maxnamlen = cnt; 124*1656Smckusick filep->funit &= ~TEMP; 125*1656Smckusick } 126*1656Smckusick /* 127*1656Smckusick * put the new name into the structure 128*1656Smckusick */ 129*1656Smckusick for (cnt = 0; cnt < maxnamlen; cnt++) 130*1656Smckusick filep->fname[cnt] = name[cnt]; 131*1656Smckusick filep->fname[cnt] = '\0'; 132*1656Smckusick filep->pfname = &filep->fname[0]; 133*1656Smckusick return(filep); 134*1656Smckusick } 135