Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/floppy/lstar.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2022-09-02 09:18:52 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2022-09-02 09:18:52 +0200
commit52be99d8c0862ff87db9a4f9ccec1ac4b5f7caed (patch)
tree1a1a1c98090afd8459cc8f045f9de2d4a7cba5ab /floppy/lstar.c
parent15adaba9eaa6a98c8b55bc5c5f73c3a9e0e55e7a (diff)
added a quite unsorted first version of a floppy boot loader
Diffstat (limited to 'floppy/lstar.c')
-rw-r--r--floppy/lstar.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/floppy/lstar.c b/floppy/lstar.c
new file mode 100644
index 0000000..2a7d541
--- /dev/null
+++ b/floppy/lstar.c
@@ -0,0 +1,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 );
+}