aboutsummaryrefslogtreecommitdiffstats
path: root/g-ir-extract-deps.c
blob: 1c1735d00d1636fcb4dd6e35541d8fdd49d6abff (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
/*
	extract the dependencies string from a GObject Introspection 1.0 typelib file
	and print it on stdout
*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

#define G_IR_MAGIC "GOBJ\nMETADATA\r\n\032"

int main(int argc, char ** argv)
{
	FILE *		typelib;
	char *		magic;
	uint32_t	deps_offset;
	char *		deps;

	if (argc < 2)
	{
		fprintf(stderr, "too few arguments\n");
		exit(1);
	}
	
	typelib = fopen(argv[1], "r");
	if (typelib == NULL)
	{
		fprintf(stderr, "failed to open %s\n", argv[1]);
		exit(1);
	}
	magic = malloc(16);
	deps = malloc(8192);
	if ((magic == NULL) || (deps == NULL))
	{
		fprintf(stderr, "failed to allocate memory\n");
		exit(1);
	}
	if (fread(magic, 16, 1, typelib) < 1)
	{
		fprintf(stderr, "failed to read magic from typelib\n");
		exit(1);
	}
	if (strcmp(magic, G_IR_MAGIC))
	{
		fprintf(stderr, "magic mismatch, not a typelib?\n");
		exit(1);
	}

	fseek(typelib, 36, SEEK_SET);
	if (fread(&deps_offset, 4, 1, typelib) < 1)
	{
		fprintf(stderr, "failed to read deps offset from typelib\n");
		exit(1);
	}
	fseek(typelib, deps_offset, SEEK_SET);
	if (fscanf(typelib, "%8191s", deps) < 1)
	{
		fprintf(stderr, "failed to read deps from typelib\n");
		exit(1);
	}

	free(deps);
	free(magic);
	fclose(typelib);

	printf("%s\n", deps);
	return 0;
}