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);
46 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 * long 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
);
84 lib
->name
= strdup(name
);
90 void rdl_close (struct librarynode
* lib
)
97 int rdl_searchlib (struct librarynode
* lib
,
98 const char * label
, rdffile
* f
)
111 lib
->fp
= fopen(lib
->name
,"rb");
121 while (! feof(lib
->fp
) )
124 * read the module name from the file, and prepend
125 * the library name and '.' to it.
127 strcpy(buf
, lib
->name
);
129 i
= strlen(lib
->name
);
130 buf
[i
++] = '.'; t
= i
;
131 while (fread(buf
+ i
,1,1,lib
->fp
) == 1 && buf
[i
] && i
< 512)
136 if (feof(lib
->fp
)) break;
137 if (!strcmp(buf
+ t
, ".dir")) /* skip over directory */
139 fread (&l
, 4, 1, lib
->fp
);
140 fseek (lib
->fp
, l
, SEEK_CUR
);
144 * open the RDOFF module
146 if ( rdfopenhere(f
,lib
->fp
,&lib
->referenced
,buf
) ) {
147 rdl_error
= 16 * rdf_errno
;
151 * read in the header, and scan for exported symbols
153 hdr
= malloc(f
->header_len
);
154 rdfloadseg(f
,RDOFF_HEADER
,hdr
);
156 while ((r
= rdfgetheaderrec(f
)))
158 if (r
->type
!= 3) /* not an export */
161 if (! strcmp(r
->e
.label
, label
) ) /* match! */
163 free(hdr
); /* reset to 'just open' */
164 f
->header_loc
= NULL
; /* state... */
170 /* find start of next module... */
173 fseek(lib
->fp
,i
,SEEK_SET
);
177 * close the file if nobody else is using it
180 if (! lib
->referenced
)
188 int rdl_openmodule (struct librarynode
* lib
, int moduleno
, rdffile
* f
)
198 lib
->fp
= fopen(lib
->name
, "rb");
201 return (rdl_error
= 1);
208 while (!feof(lib
->fp
))
210 strcpy(buf
, lib
->name
);
212 buf
[i
++] = '.'; t
= i
;
213 while (fread(buf
+ i
,1,1,lib
->fp
) == 1 && buf
[i
] && i
< 512)
216 if (feof(lib
->fp
)) break;
218 if (buf
[t
] != '.') /* special module - not counted in the numbering */
219 cmod
++; /* of RDOFF modules - must be referred to by name */
221 if (cmod
== moduleno
) {
223 rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
);
225 if (!lib
->referenced
) {
232 fread(buf
, 6, 1, lib
->fp
);
237 else if (strncmp(buf
, "RDOFF", 5)) {
238 if (! --lib
->referenced
) {
242 return rdl_error
= 2;
244 else if (buf
[5] != '2') {
245 if (! --lib
->referenced
) {
249 return rdl_error
= 3;
252 fread(&length
, 4, 1, lib
->fp
);
253 fseek(lib
->fp
, length
, SEEK_CUR
); /* skip over the module */
255 if (! --lib
->referenced
) {
259 return rdl_error
= 4; /* module not found */
262 void rdl_perror(const char *apname
, const char *filename
)
265 rdfperror(apname
, filename
);
267 fprintf(stderr
,"%s:%s:%s\n",apname
,filename
,rdl_errors
[rdl_error
]);