8 #include "shotgun/lib/shotgun.h"
9 #include "shotgun/lib/ar.h"
11 static int ar_validate(int fd
) {
17 bytes
= read(fd
, data
, 8);
19 if(bytes
!= 8 || strncmp(data
, "!<arch>\n", 8)) {
26 static char *ar_file_name(int fd
, char *name
, long *size
) {
30 if(strncmp(name
, "#1/", 3) != 0) {
31 ptr
= strchr(name
, ' ');
35 length
= atoi(name
+ 3);
37 ptr
= (char *)calloc(length
, sizeof(char));
38 if(ptr
== NULL
) return NULL
;
40 bytes
= read(fd
, ptr
, length
);
54 * Retrieves each file from from ar(5) archive +path+ and processes it with
57 int ar_each_file(machine m
, const char *path
, int(*callback
)(machine
, char *, uint8_t *, long)) {
60 char *name
, data_size
[11], name_data
[17];
68 fd
= open(path
, O_RDONLY
);
74 if(!ar_validate(fd
)) {
79 bytes
= read(fd
, name_data
, 16);
80 if(bytes
== 0) { /* end of file */
82 } else if(bytes
!= 16) {
87 err
= lseek(fd
, 32, SEEK_CUR
); /* mtime, uid, gid, mode */
93 bytes
= read(fd
, data_size
, 10);
99 size
= atoi(data_size
);
101 err
= lseek(fd
, 2, SEEK_CUR
);
107 name
= ar_file_name(fd
, name_data
, &size
);
109 data
= (uint8_t *)calloc(size
, sizeof(uint8_t));
115 bytes
= read(fd
, data
, size
);
122 ret
= callback(m
, name
, data
, bytes
);
131 lseek(fd
, 1, SEEK_CUR
);