MCPcopy Create free account
hub / github.com/NetHack/NetHack / readlibdir

Function readlibdir

src/dlb.c:126–168  ·  view source on GitHub ↗

* Read the directory from the library file. This will allocate and * fill in our globals. The file pointer is reset back to position * zero. If any part fails, leave nothing that needs to be deallocated. * * Return TRUE on success, FALSE on failure. */

Source from the content-addressed store, hash-verified

124 * Return TRUE on success, FALSE on failure.
125 */
126staticfn boolean
127readlibdir(library *lp) /* library pointer to fill in */
128{
129 int i;
130 char *sp;
131 long liboffset, totalsize;
132
133 if (fscanf(lp->fdata, "%ld %ld %ld %ld %ld\n", &lp->rev, &lp->nentries,
134 &lp->strsize, &liboffset, &totalsize) != 5)
135 return FALSE;
136 if (lp->rev > DLB_MAX_VERS || lp->rev < DLB_MIN_VERS)
137 return FALSE;
138
139 lp->dir = (libdir *) alloc(FITSuint(lp->nentries * sizeof(libdir)));
140 lp->sspace = (char *) alloc(FITSuint(lp->strsize));
141
142 /* read in each directory entry */
143 for (i = 0, sp = lp->sspace; i < lp->nentries; i++) {
144 lp->dir[i].fname = sp;
145 if (fscanf(lp->fdata, "%c%s %ld\n", &lp->dir[i].handling, sp,
146 &lp->dir[i].foffset) != 3) {
147 free((genericptr_t) lp->dir);
148 free((genericptr_t) lp->sspace);
149 lp->dir = (libdir *) 0;
150 lp->sspace = (char *) 0;
151 return FALSE;
152 }
153 sp = eos(sp) + 1;
154 }
155
156 /* calculate file sizes using offset information */
157 for (i = 0; i < lp->nentries; i++) {
158 if (i == lp->nentries - 1)
159 lp->dir[i].fsize = totalsize - lp->dir[i].foffset;
160 else
161 lp->dir[i].fsize = lp->dir[i + 1].foffset - lp->dir[i].foffset;
162 }
163
164 (void) fseek(lp->fdata, 0L, SEEK_SET); /* reset back to zero */
165 lp->fmark = 0;
166
167 return TRUE;
168}
169
170/*
171 * Look for the file in our directory structure. Return 1 if successful,

Callers 1

open_libraryFunction · 0.85

Calls 4

fscanfFunction · 0.85
eosFunction · 0.85
fseekFunction · 0.85
allocFunction · 0.70

Tested by

no test coverage detected