-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtar.c
66 lines (50 loc) · 1.17 KB
/
tar.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
#include <sigma0/sigma0.h>
#include <sigma0/tar.h>
#include <c4rt/c4rt.h>
#include <stdbool.h>
static inline bool str_equal( const char *a, const char *b ){
for ( ; *a && *b; a++, b++ ){
if ( *a != *b ){
return false;
}
}
return *a == *b;
}
static inline unsigned octal_num( const char *num ){
unsigned ret = 0;
for ( ; *num; num++ ){
ret *= 8;
ret += *num - '0';
}
return ret;
}
tar_header_t *tar_lookup( tar_header_t *archive, const char *name ){
tar_header_t *ret = NULL;
tar_header_t *temp = archive;
while ( temp && *temp->filename ){
if ( str_equal( temp->filename, name )){
ret = temp;
break;
}
temp = tar_next( temp );
}
return ret;
}
tar_header_t *tar_next( tar_header_t *archive ){
tar_header_t *ret = archive;
if ( ret && *ret->filename ){
unsigned size = octal_num( ret->size );
ret += size / sizeof( tar_header_t ) + 1;
ret += size % sizeof( tar_header_t ) > 0;
}
return ret;
}
void *tar_data( tar_header_t *archive ){
return (void *)(archive + 1);
}
unsigned tar_data_size( tar_header_t *archive ){
return octal_num( archive->size );
}
bool tar_end( tar_header_t *archive ){
return !archive || !*archive->filename;
}