summaryrefslogtreecommitdiff
path: root/rdoff/v1/rdlib.c
blob: bc8d1e351883f567be8e80ee9092ef586ab887da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>

#include "rdoff.h"
#include "rdlib.h"

int rdl_error = 0;

char *rdl_errors[3] = {
    "no error","could not open file", "invalid file structure",
};

int rdl_searchlib (struct librarynode * lib,
		   const char * label, rdffile * f)
{
    char		buf[257];
    int			i;
    void *		hdr;
    rdfheaderrec	* r;

    rdl_error = 0;
    lib->referenced ++;

    if (! lib->fp)
    {
	lib->fp = fopen(lib->name,"rb");
	
	if (! lib->fp) {
	    rdl_error = 1;
	    return 0;
	}
    }
    else
	rewind(lib->fp);

    while (! feof(lib->fp) )
    {
	i = 1;
	while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 257)
	    i++;
	buf[0] = ':';

	if (feof(lib->fp)) break;

	if ( rdfopenhere(f,lib->fp,&lib->referenced,buf) ) {
	    rdl_error = 2;
	    return 0;
	}
	
	hdr = malloc(f->header_len);
	rdfloadseg(f,RDOFF_HEADER,hdr);
	
	while ((r = rdfgetheaderrec(f)))
	{
	    if (r->type != 3)	/* not an export */
		continue;

	    if (! strcmp(r->e.label, label) )	/* match! */
	    {
		free(hdr);			/* reset to 'just open' */
		f->header_loc = NULL;		/* state... */
		f->header_fp = 0;
		return 1;
	    }
	}

	/* find start of next module... */
	i = f->data_ofs + f->data_len;
	rdfclose(f);
	fseek(lib->fp,i,SEEK_SET);
    }

    lib->referenced --;
    if (! lib->referenced)
    {
	fclose(lib->fp);
	lib->fp = NULL;
    }
    return 0;
}

void rdl_perror(const char *apname, const char *filename)
{
    fprintf(stderr,"%s:%s:%s\n",apname,filename,rdl_errors[rdl_error]);
}