2 * rdlib.c - routines for manipulating RDOFF libraries (.rdl)
13 /* See Texinfo documentation about new RDOFF libraries format */
17 char *rdl_errors
[5] = {
18 "no error","could not open file", "invalid file structure",
19 "file contains modules of an unsupported RDOFF version",
23 int rdl_verify(const char * filename
)
25 FILE * fp
= fopen(filename
, "rb");
29 static char lastverified
[256];
30 static int lastresult
= -1;
32 if (lastresult
!= -1 && !strcmp(filename
, lastverified
))
35 strcpy(lastverified
, filename
);
38 return (rdl_error
= lastresult
= 1);
44 while (fread(buf
+ i
,1,1,fp
) == 1 && buf
[i
] && i
< 257)
50 * A special module, eg a signature block or a directory.
51 * Format of such a module is defined to be:
52 * six char type identifier
53 * long count bytes content
55 * so we can handle it uniformaly with RDOFF2 modules.
59 /* Currently, nothing useful to do with signature block.. */
63 if (strncmp(buf
, "RDOFF", 5)) {
64 return rdl_error
= lastresult
= 2;
65 } else if (buf
[5] != '2') {
66 return rdl_error
= lastresult
= 3;
69 fread(&length
, 4, 1, fp
);
70 fseek(fp
, length
, SEEK_CUR
); /* skip over the module */
73 return lastresult
= 0; /* library in correct format */
76 int rdl_open (struct librarynode
* lib
, const char * name
)
78 int i
= rdl_verify(name
);
82 lib
->name
= strdup(name
);
88 void rdl_close (struct librarynode
* lib
)
95 int rdl_searchlib (struct librarynode
* lib
,
96 const char * label
, rdffile
* f
)
109 lib
->fp
= fopen(lib
->name
,"rb");
119 while (! feof(lib
->fp
) )
122 * read the module name from the file, and prepend
123 * the library name and '.' to it.
125 strcpy(buf
, lib
->name
);
127 i
= strlen(lib
->name
);
128 buf
[i
++] = '.'; t
= i
;
129 while (fread(buf
+ i
,1,1,lib
->fp
) == 1 && buf
[i
] && i
< 512)
134 if (feof(lib
->fp
)) break;
135 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
)))
156 if (r
->type
!= 3) /* not an export */
159 if (! strcmp(r
->e
.label
, label
) ) /* match! */
161 free(hdr
); /* reset to 'just open' */
162 f
->header_loc
= NULL
; /* state... */
168 /* find start of next module... */
171 fseek(lib
->fp
,i
,SEEK_SET
);
175 * close the file if nobody else is using it
178 if (! lib
->referenced
)
186 int rdl_openmodule (struct librarynode
* lib
, int moduleno
, rdffile
* f
)
196 lib
->fp
= fopen(lib
->name
, "rb");
199 return (rdl_error
= 1);
206 while (!feof(lib
->fp
))
208 strcpy(buf
, lib
->name
);
210 buf
[i
++] = '.'; t
= i
;
211 while (fread(buf
+ i
,1,1,lib
->fp
) == 1 && buf
[i
] && i
< 512)
214 if (feof(lib
->fp
)) break;
216 if (buf
[t
] != '.') /* special module - not counted in the numbering */
217 cmod
++; /* of RDOFF modules - must be referred to by name */
219 if (cmod
== moduleno
) {
221 rdfopenhere(f
, lib
->fp
, &lib
->referenced
, buf
);
223 if (!lib
->referenced
) {
230 fread(buf
, 6, 1, lib
->fp
);
235 else if (strncmp(buf
, "RDOFF", 5)) {
236 if (! --lib
->referenced
) {
240 return rdl_error
= 2;
242 else if (buf
[5] != '2') {
243 if (! --lib
->referenced
) {
247 return rdl_error
= 3;
250 fread(&length
, 4, 1, lib
->fp
);
251 fseek(lib
->fp
, length
, SEEK_CUR
); /* skip over the module */
253 if (! --lib
->referenced
) {
257 return rdl_error
= 4; /* module not found */
260 void rdl_perror(const char *apname
, const char *filename
)
263 rdfperror(apname
, filename
);
265 fprintf(stderr
,"%s:%s:%s\n",apname
,filename
,rdl_errors
[rdl_error
]);