xref: /original-bsd/usr.bin/pascal/libpc/GETNAME.c (revision 6b7db209)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)GETNAME.c 1.9 08/29/82";
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);
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 newrecord 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 && (struct iorec *)filep > next) {
91 				prev = next;
92 				next = next->fchain;
93 			}
94 		filep->fchain = next;
95 		prev->fchain = filep;
96 	}
97 	/*
98 	 * Get the filename associated with the buffer.
99 	 */
100 	if (name == NULL) {
101 		if (*filep->fname != NULL) {
102 			return(filep);
103 		}
104 		/*
105 		 * No name given and no previous name, so generate
106 		 * a new one of the form #tmp.xxxxxx.
107 		 */
108 		filep->funit |= TEMP;
109 		sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk],
110 		    getpid());
111 		filep->pfname = &filep->fname[0];
112 		return(filep);
113 	}
114 	/*
115 	 * Trim trailing blanks, and insure that the name
116 	 * will fit into the file structure.
117 	 */
118 	for (cnt = 0; cnt < maxnamlen; cnt++)
119 		if (name[cnt] == '\0' || name[cnt] == ' ')
120 			break;
121 	if (cnt >= NAMSIZ) {
122 		ERROR("%s: File name too long\n", name);
123 		return;
124 	}
125 	maxnamlen = cnt;
126 	filep->funit &= ~TEMP;
127 	/*
128 	 * Put the new name into the structure.
129 	 */
130 	for (cnt = 0; cnt < maxnamlen; cnt++)
131 		filep->fname[cnt] = name[cnt];
132 	filep->fname[cnt] = '\0';
133 	filep->pfname = &filep->fname[0];
134 	return(filep);
135 }
136