Commit | Line | Data |
---|---|---|
4c60c82b KM |
1 | /* Copyright (c) 1979 Regents of the University of California */ |
2 | ||
088815d3 | 3 | static char sccsid[] = "@(#)GETNAME.c 1.11 %G%"; |
4c60c82b KM |
4 | |
5 | #include "h00vars.h" | |
42a498d4 | 6 | #include "libpc.h" |
4c60c82b KM |
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 | ||
280af558 KM |
19 | static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
20 | ||
4c60c82b | 21 | struct iorec * |
492cc5d3 | 22 | GETNAME(filep, name, namlim, datasize) |
4c60c82b KM |
23 | |
24 | register struct iorec *filep; | |
25 | char *name; | |
492cc5d3 KM |
26 | long namlim; |
27 | long datasize; | |
4c60c82b | 28 | { |
492cc5d3 | 29 | int maxnamlen = namlim; |
4c60c82b KM |
30 | struct iorec *prev; |
31 | struct iorec *next; | |
32 | register int cnt; | |
33 | struct iorec locvar; | |
4c60c82b | 34 | |
42a498d4 KM |
35 | if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) { |
36 | /* | |
37 | * Close and immediately reactivate the file. | |
38 | */ | |
9e012f32 | 39 | PFCLOSE(filep, name != NULL); |
42a498d4 KM |
40 | _actfile[filep->fblk] = filep; |
41 | filep->funit &= (TEMP | FTEXT); | |
42 | } else { | |
4c60c82b | 43 | /* |
42a498d4 | 44 | * Initialize a new file record. |
4c60c82b KM |
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 | /* | |
42a498d4 | 57 | * Check to see if file is global, or allocated in |
4c60c82b KM |
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; | |
280af558 KM |
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: | |
4c60c82b KM |
74 | filep->fblk = _filefre; |
75 | _actfile[_filefre] = filep; | |
76 | /* | |
088815d3 | 77 | * Link the new record into the file chain. |
4c60c82b KM |
78 | */ |
79 | prev = (struct iorec *)&_fchain; | |
80 | next = _fchain.fchain; | |
81 | while (filep->flev > next->flev) { | |
82 | prev = next; | |
83 | next = next->fchain; | |
84 | } | |
42a498d4 | 85 | if (filep->flev == GLVL) |
4c60c82b | 86 | /* |
42a498d4 KM |
87 | * Must order global files so that all dynamic files |
88 | * within a record are grouped together. | |
4c60c82b | 89 | */ |
088815d3 KM |
90 | while ((next != FILNIL) && |
91 | (next->flev == GLVL) && | |
92 | ((struct iorec *)filep > next)) { | |
42a498d4 KM |
93 | prev = next; |
94 | next = next->fchain; | |
4c60c82b | 95 | } |
42a498d4 KM |
96 | filep->fchain = next; |
97 | prev->fchain = filep; | |
4c60c82b KM |
98 | } |
99 | /* | |
42a498d4 | 100 | * Get the filename associated with the buffer. |
4c60c82b KM |
101 | */ |
102 | if (name == NULL) { | |
103 | if (*filep->fname != NULL) { | |
104 | return(filep); | |
105 | } | |
106 | /* | |
42a498d4 KM |
107 | * No name given and no previous name, so generate |
108 | * a new one of the form #tmp.xxxxxx. | |
4c60c82b KM |
109 | */ |
110 | filep->funit |= TEMP; | |
280af558 | 111 | sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk], |
b3132924 | 112 | getpid()); |
6c0ee187 KM |
113 | filep->pfname = &filep->fname[0]; |
114 | return(filep); | |
115 | } | |
116 | /* | |
42a498d4 KM |
117 | * Trim trailing blanks, and insure that the name |
118 | * will fit into the file structure. | |
6c0ee187 KM |
119 | */ |
120 | for (cnt = 0; cnt < maxnamlen; cnt++) | |
121 | if (name[cnt] == '\0' || name[cnt] == ' ') | |
122 | break; | |
123 | if (cnt >= NAMSIZ) { | |
86997b19 | 124 | ERROR("%s: File name too long\n", name); |
6c0ee187 | 125 | return; |
4c60c82b | 126 | } |
6c0ee187 KM |
127 | maxnamlen = cnt; |
128 | filep->funit &= ~TEMP; | |
4c60c82b | 129 | /* |
42a498d4 | 130 | * Put the new name into the structure. |
4c60c82b KM |
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 | } |