ctar/utar.c

123 lines
2.4 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 fileexists(char *fname);
int main(int argc, char *argv[])
{
char *name = argv[1]; // name of archive
char buffer[300]; // character buffer for sprintf
int fp, r, i, j, p, w; // counters and pointers
if (argc != 2)
{
fprintf(stderr, "%s:\n Usage: utar filename \n", strerror(5));
exit(EXIT_FAILURE);
}
if (fileexists(name) == 0)
{
fprintf(stderr, "archive doesnt exist%s\n", strerror(2));
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", argv[2], strerror(5));
exit(EXIT_FAILURE);
}
hdr header;
int tempfp;
char byte;
char f_name[sizeof(short int)];
short int l;
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);
}
for (i = 0; i < header.block_count; i++)
{
if (header.deleted[i] == 1)
{
// skip it
}
else
{
lseek(fp, header.file_name[i], SEEK_SET);
read(fp, (char *)&l, sizeof(short int));
read(fp, f_name, l);
if (fileexists(f_name))
{
fprintf(stderr, "%s\n", strerror(17));
exit(EXIT_FAILURE);
}
tempfp = open(f_name, O_RDWR | O_CREAT, 0644); // creates the new file;
for (j = 0; j < header.file_size[i]; j++)
{
r = read(fp, &byte, sizeof(byte));
if (r < 0)
{
fprintf(stderr, "%s: file %s could not be read from archive\n", f_name, strerror(5));
exit(EXIT_FAILURE);
}
w = write(tempfp, &byte, sizeof(byte));
if (w < 0)
{
fprintf(stderr, "%s: file %s could not be written to file\n", f_name, strerror(5));
exit(EXIT_FAILURE);
}
}
}
}
if (header.next < 0)
{
printf("Untared Successfully\n");
exit(EXIT_SUCCESS);
}
else
{
p = header.next;
lseek(fp, header.next, SEEK_SET);
}
}
return EXIT_SUCCESS;
}
int fileexists(char *fname)
{
if (open(fname, O_RDONLY, 0644) < 0)
{
return false; // file doesn't exist
}
else
{
return true; // file exists
}
}