|
|
1.1 root 1: /* Copyright (c) 1979 Regents of the University of California */
2:
3: static char sccsid[] = "@(#)GETNAME.c 1.11 1/22/83";
4:
5: #include "h00vars.h"
6: #include "libpc.h"
7:
8: /*
9: * GETNAME - activate a file
10: *
11: * takes a name, name length, element size, and variable
12: * level and returns a pointer to a file structure.
13: *
14: * a new file structure is initialized if necessary.
15: * temporary names are generated, and given
16: * names are blank trimmed.
17: */
18:
19: static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20:
21: struct iorec *
22: GETNAME(filep, name, namlim, datasize)
23:
24: register struct iorec *filep;
25: char *name;
26: long namlim;
27: long datasize;
28: {
29: int maxnamlen = namlim;
30: struct iorec *prev;
31: struct iorec *next;
32: register int cnt;
33: struct iorec locvar;
34:
35: if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) {
36: /*
37: * Close and immediately reactivate the file.
38: */
39: PFCLOSE(filep, name != NULL);
40: _actfile[filep->fblk] = filep;
41: filep->funit &= (TEMP | FTEXT);
42: } else {
43: /*
44: * Initialize a new file record.
45: */
46: filep->funit = 0;
47: if (datasize == 0) {
48: filep->funit |= FTEXT;
49: datasize = 1;
50: }
51: filep->fsize = datasize;
52: filep->fbuf = 0;
53: filep->lcount = 0;
54: filep->llimit = 0x7fffffff;
55: filep->fileptr = &filep->window[0];
56: /*
57: * Check to see if file is global, or allocated in
58: * the stack by checking its address against the
59: * address of one of our routine's local variables.
60: */
61: if (filep < &locvar)
62: filep->flev = GLVL;
63: else
64: filep->flev = filep;
65: for (_filefre++; _filefre < MAXFILES; _filefre++)
66: if (_actfile[_filefre] == FILNIL)
67: goto gotone;
68: for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++)
69: if (_actfile[_filefre] == FILNIL)
70: goto gotone;
71: ERROR("File table overflow\n");
72: return;
73: gotone:
74: filep->fblk = _filefre;
75: _actfile[_filefre] = filep;
76: /*
77: * Link the new record into the file chain.
78: */
79: prev = (struct iorec *)&_fchain;
80: next = _fchain.fchain;
81: while (filep->flev > next->flev) {
82: prev = next;
83: next = next->fchain;
84: }
85: if (filep->flev == GLVL)
86: /*
87: * Must order global files so that all dynamic files
88: * within a record are grouped together.
89: */
90: while ((next != FILNIL) &&
91: (next->flev == GLVL) &&
92: ((struct iorec *)filep > next)) {
93: prev = next;
94: next = next->fchain;
95: }
96: filep->fchain = next;
97: prev->fchain = filep;
98: }
99: /*
100: * Get the filename associated with the buffer.
101: */
102: if (name == NULL) {
103: if (*filep->fname != NULL) {
104: return(filep);
105: }
106: /*
107: * No name given and no previous name, so generate
108: * a new one of the form #tmp.xxxxxx.
109: */
110: filep->funit |= TEMP;
111: sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk],
112: getpid());
113: filep->pfname = &filep->fname[0];
114: return(filep);
115: }
116: /*
117: * Trim trailing blanks, and insure that the name
118: * will fit into the file structure.
119: */
120: for (cnt = 0; cnt < maxnamlen; cnt++)
121: if (name[cnt] == '\0' || name[cnt] == ' ')
122: break;
123: if (cnt >= NAMSIZ) {
124: ERROR("%s: File name too long\n", name);
125: return;
126: }
127: maxnamlen = cnt;
128: filep->funit &= ~TEMP;
129: /*
130: * Put the new name into the structure.
131: */
132: for (cnt = 0; cnt < maxnamlen; cnt++)
133: filep->fname[cnt] = name[cnt];
134: filep->fname[cnt] = '\0';
135: filep->pfname = &filep->fname[0];
136: return(filep);
137: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.