Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/floppy/lstar.c
blob: 2a7d5417280be238efc69d349fde09a62f5de131 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <bsd/string.h>
#include <string.h>

static int octal_to_int( char *s, int size )
{
	int n = 0;
	char *c = s;
	while( size > 0 ) {
		n *= 8;
		n += *c - '0';
		c++;
		size--;
	}
	
	return n;
}

int main( int argc, char *argv[] )
{
	FILE *f = NULL;
	char buf[512];
	char filename[100];
	char size_octal[12];
	char cksum_octal[8];
	int size;
	unsigned int cksum;
	unsigned int computed_cksum;
	int i, n;
	int terminate = 0;

	if( argc != 2 ) {
		fprintf( stderr, "usage: %s <tarfile>\n", argv[0] );
		exit( EXIT_FAILURE );
	}
	
	f = fopen( argv[1], "r" );
	if( f == NULL ) {
		fprintf( stderr, "ERROR: failed to open file '%s'\n", argv[1] );
		exit( EXIT_FAILURE );
	}
	
	do {
		clearerr( f );
		n = fread( buf, 512, 1, f );
		if( n == 0 ) {
			if( ferror( f ) ) {
				fprintf( stderr, "ERR: read error\n" );
				terminate = 1;
			} else if( feof( f ) ) {
				terminate = 1;
				break;
			}
		}
		
		if( memcmp( &buf[257], "ustar", 5 ) == 0 ) {
			strlcpy( filename, buf, 100 );
			
			if( strcmp( filename, "" ) != 0 ) {

				printf( "file: %s\n", filename );
			
				strlcpy( size_octal, &buf[124], 12 );
				printf( "size (octal): %s\n", size_octal );

				size = octal_to_int( size_octal, 11 );
				printf( "size (decimal): %d\n", size );

				strlcpy( cksum_octal, &buf[148], 8 );
				printf( "cksum (octal): %s\n", cksum_octal );
				cksum = octal_to_int( cksum_octal, 6 );
				printf( "cksum (decimal): %d\n", cksum );
				
				for( i = 148; i < 148+8; i++ ) {
					buf[i] = ' ';
				}
				computed_cksum = 0;
				for( i = 0; i < 512; i++ ) {
					computed_cksum += (unsigned char)buf[i];
				}
				printf( "cksum (computed): %d\n", computed_cksum );
				if( cksum != computed_cksum ) {
					fprintf( stderr, "ERR: checksum mismatch in header!\n" );
				}

				while( size > 0 ) {
					fread( buf, 512, 1, f );
					size -= 512;
				}
			} else {
				fprintf( stderr, "INFO: the end\n" );
			}			
		} else {
			fprintf( stderr, "WARN: skipping non TAR header\n" );
		}
	} while( !terminate );
	
	fclose( f );
	
	exit( EXIT_SUCCESS );
}