ctar/dumparchiveheader.c

81 lines
1.8 KiB
C
Executable File

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "tar.h"
int main(int argc, char *argv[])
{
char *name = argv[1]; // name of archive
int numberoffiles = argc - 3;
char buffer[300]; // character buffer for write
int fp, r, i, p;
if (argc != 2)
{
fprintf(stderr, "%s: Usage: dah filename \n", strerror(5));
exit(EXIT_FAILURE);
}
fp = open(name, O_RDONLY, 0644); // opens file; creates it if it does not exist;
if (fp < 0)
{
fprintf(stderr, "cannot open %s because of %s\n", name, strerror(5));
exit(EXIT_FAILURE);
}
write(1, "\n", 1);
hdr header;
while (true)
{
r = read(fp, &header, sizeof(hdr));
if (r != sizeof(hdr))
{
fprintf(stderr, "%s: file is not the size of a header object\n", strerror(5));
exit(EXIT_FAILURE);
}
else if (header.magic != 0x63746172)
{
fprintf(stderr, "%s: header does not include the magic number [magic number is: %x]\n", strerror(5), header.magic);
exit(EXIT_FAILURE);
}
printf("magic number is: %x\n", header.magic);
printf("The end of file pointer is at: %i\n", header.eop);
printf("number of used blocks are: %i\n", header.block_count);
for (i = 0; i < 8; i++)
{
printf("The filesize of block [%i] is %i\n", i, header.file_size[i]);
}
for (i = 0; i < 8; i++)
{
printf("Is block [%i] deleted? (1=true, 0=false): %i\n", i, header.deleted[i]);
}
for (i = 0; i < 8; i++)
{
printf("The filename in block [%i] is %i\n", i, header.file_name[i]);
}
printf("The next header is at: %i\n", header.next);
if (header.next < 0)
{
printf("End of headers\n");
exit(EXIT_SUCCESS);
}
else
{
p = header.next;
lseek(fp, header.next, SEEK_SET);
}
}
close(r);
close(fp);
return EXIT_SUCCESS;
}