2 * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
15 /* See Texinfo documentation about new RDOFF libraries format */
19 char *rdl_errors
[5] = {
20 "no error", "could not open file", "invalid file structure",
21 "file contains modules of an unsupported RDOFF version",
25 int rdl_verify(const char *filename
)
27 FILE *fp
= fopen(filename
, "rb");
31 static char lastverified
[256];
32 static int lastresult
= -1;
34 if (lastresult
!= -1 && !strcmp(filename
, lastverified
))
37 strcpy(lastverified
, filename
);
40 return (rdl_error
= lastresult
= 1);
45 while (fread(buf
+ i
, 1, 1, fp
) == 1 && buf
[i
] && i
< 257)
52 * A special module, eg a signature block or a directory.
53 * Format of such a module is defined to be:
54 * six char type identifier
55 * int32_t count bytes content
57 * so we can handle it uniformaly with RDOFF2 modules.
61 /* Currently, nothing useful to do with signature block.. */
65 if (strncmp(buf
, "RDOFF", 5)) {
66 return rdl_error
= lastresult
= 2;
67 } else if (buf
[5] != '2') {
68 return rdl_error
= lastresult
= 3;
71 fread(&length
, 4, 1, fp
);
72 fseek(fp
, length
, SEEK_CUR
); /* skip over the module */
75 return lastresult
= 0; /* library in correct format */
78 int rdl_open(struct librarynode
*lib
, const char *name
)
80 int i
= rdl_verify(name
);
85 lib
->name
= strdup(name
);
91 void rdl_close(struct librarynode
*lib
)
98 int rdl_searchlib(struct librarynode
*lib
, const char *label
, rdffile
* f
)
110 lib
->fp
= fopen(lib
->name
, "rb");
119 while (!feof(lib
->fp
)) {
121 * read the module name from the file, and prepend
122 * the library name and '.' to it.
124 strcpy(buf
, lib
->name
);
126 i
= strlen(lib
->name
);
129 while (fread(buf
+ i
, 1, 1, lib
->fp
) == 1 && buf
[i
] && i
< 512)
136 if (!strcmp(buf
+ t
, ".dir")) { /* skip over directory */
137 fread(&l
, 4, 1, lib
->fp
);
138 fseek(lib
->fp
, l
, SEEK_CUR
);
142 * open the RDOFF module
144 if (rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
)) {
145 rdl_error
= 16 * rdf_errno
;
149 * read in the header, and scan for exported symbols
151 hdr
= malloc(f
->header_len
);
152 rdfloadseg(f
, RDOFF_HEADER
, hdr
);
154 while ((r
= rdfgetheaderrec(f
))) {
155 if (r
->type
!= 3) /* not an export */
158 if (!strcmp(r
->e
.label
, label
)) { /* match! */
159 free(hdr
); /* reset to 'just open' */
160 f
->header_loc
= NULL
; /* state... */
166 /* find start of next module... */
169 fseek(lib
->fp
, i
, SEEK_SET
);
173 * close the file if nobody else is using it
176 if (!lib
->referenced
) {
183 int rdl_openmodule(struct librarynode
*lib
, int moduleno
, rdffile
* f
)
192 lib
->fp
= fopen(lib
->name
, "rb");
195 return (rdl_error
= 1);
201 while (!feof(lib
->fp
)) {
202 strcpy(buf
, lib
->name
);
206 while (fread(buf
+ i
, 1, 1, lib
->fp
) == 1 && buf
[i
] && i
< 512)
212 if (buf
[t
] != '.') /* special module - not counted in the numbering */
213 cmod
++; /* of RDOFF modules - must be referred to by name */
215 if (cmod
== moduleno
) {
217 rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
);
219 if (!lib
->referenced
) {
226 fread(buf
, 6, 1, lib
->fp
);
230 } else if (strncmp(buf
, "RDOFF", 5)) {
231 if (!--lib
->referenced
) {
235 return rdl_error
= 2;
236 } else if (buf
[5] != '2') {
237 if (!--lib
->referenced
) {
241 return rdl_error
= 3;
244 fread(&length
, 4, 1, lib
->fp
);
245 fseek(lib
->fp
, length
, SEEK_CUR
); /* skip over the module */
247 if (!--lib
->referenced
) {
251 return rdl_error
= 4; /* module not found */
254 void rdl_perror(const char *apname
, const char *filename
)
257 rdfperror(apname
, filename
);
259 fprintf(stderr
, "%s:%s:%s\n", apname
, filename
,
260 rdl_errors
[rdl_error
]);