/
pdump.c
87 lines (71 loc) · 1.72 KB
/
pdump.c
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
/* pdump.c
* Copyright: (When this is determined...it will go here)
* CVS Info
* $Id$
* Overview:
* A program to dump pack files to human readable form.
* Data Structure and Algorithms:
* History:
* Notes:
* References:
*/
#include "parrot/packfile.h"
int
main(int argc, char **argv)
{
struct stat file_stat;
int fd;
opcode_t *packed;
size_t packed_size;
struct PackFile *pf;
struct Parrot_Interp *interpreter = make_interpreter(NO_FLAGS);
if (argc != 2) {
fprintf(stderr, "pdump: usage: pdump FILE\n");
return 1;
}
if (stat(argv[1], &file_stat)) {
printf("can't stat %s, code %i\n", argv[1], errno);
return 1;
}
fd = open(argv[1], O_RDONLY | O_BINARY);
if (!fd) {
printf("Can't open, error %i\n", errno);
return 1;
}
init_world();
interpreter = make_interpreter(NO_FLAGS);
packed_size = file_stat.st_size;
#ifndef HAS_HEADER_SYSMMAN
packed = (opcode_t *)mem_sys_allocate(packed_size);
if (!packed) {
printf("Can't allocate, code %i\n", errno);
return 1;
}
read(fd, (void *)packed, packed_size);
#else
packed =
(opcode_t *)mmap(0, packed_size, PROT_READ, MAP_SHARED, fd, (off_t)0);
if (!packed) {
printf("Can't mmap, code %i\n", errno);
return 1;
}
#endif
pf = PackFile_new();
if (!PackFile_unpack(interpreter, pf, (opcode_t *)packed, packed_size)) {
printf("Can't unpack.\n");
return 1;
}
PackFile_dump(pf);
PackFile_DELETE(pf);
pf = NULL;
return 0;
}
/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/