Question: Problem 1 We are going to develop our own tar and untar tools to copy / extract files into / from an archive. The first

Problem 1
We are going to develop our own tar and untar tools to copy/extract files into/from an
archive.
The first thing is to figure out the format of the archive. In addition to storing the file content,
the archive file must also store the original filename so that it can be recreated on extraction.
The following is a suggested archive file format:
mytar
[filename-1]
[filesize-1]
[filedata-1][filename-2]
[filesize-2]
[filedata-2]...
A couple comments on the archive format:
The file begins with the magic word mytar
for identification purposes. This is a
common technique for application to identify if the file is of the right type (instead of
relying on file extension) and if not, abort the operation.
Files are stored in the archive one after the other, prefixed by the filename and the
file size. The file size is essential for determining where the files data ends and the
next file begins.
There is no end-of-archive marker its just the end of the file.
Filename and file size are stored as ASCII text, followed by a newline char
.
Both text and binary files can be archived.
a) Develop mytar.c which will copy source files specified in the command-line
arguments into the archive file specified.
You are highly recommended to test your program in a separate directory so
that any bugs will not destroy your C source files!
To facilitate debugging, try archiving short text files first (e.g., C source files).
The resultant archive file can then be inspected in gedit directly.
Example: (No argument given, show usage.)
$ ./mytar
Usage: ./mytar outputfile infile1[infile2]...
Example: (Archive files. Note the wildcard *.c will expand (by the shell) into a list of matching
filenames and be passed to main()s argc/argv[] parameters.)
$ ./mytar myfiles.mt *.c
Creating archive file ...
Adding file mytar.c to archive ...
Adding file myuntar.c to archive ...
Total 2 files and 8887 bytes archived successfully.
Example: (Archive files. Overwrite handling.)
$ ./mytar myfiles.mt *.c
Output file myfiles.mt already exists, overwrite? (y/N): n
Aborted!
b) Develop myuntar.c which will extract files from an archive file to an optional output
path.
You are highly recommended to test your program in a separate directory so that any
bugs will not destroy your C source files!
The output path, if not specified, is the current working path. Otherwise, the output
path will be prepended to the original filenames. See below for examples.
If the output file already exists then it will be truncated and overwritten.
Example: (No argument given, show usage.)
$ ./myuntar
Usage: ./myuntar inputfile [output_path]
Example: (Extract files to an output path out. Note the output path must exist or it will
abort.)
$ ./myuntar myfiles.mt nodir
Reading archive file ...
Failed to open destination file nodir/mytar.c, reason: No such file or
directory
$ mkdir outdir
$ ./myuntar myfiles.mt outdir
Reading archive file ...
Extracting file outdir/mytar.c ...
Extracting file outdir/myuntar.c ...
Total 2 files and 8887 bytes extracted successfully.
c) Does your mytar.c work correctly if one of the input files is a directory? Try the
following and see what happens. Add the logic to detect directories and skip them.
Put your solution into mytar2.c.
Page 2/5
Example: (Extract files to an output path out. Note the output path must exist or it will
abort.)
$ ./mytar myfiles.mt *
Creating archive file ...
Adding file mytar to archive ...
Adding file mytar.c to archive ...
Adding file myuntar to archive ...
Adding file myuntar.c to archive ...
Input file outdir is a directory, skipped..
Total 4 files and 35407 bytes archived successfully.
d) Another annoyance is when you already have the archive file and you want to
overwrite it. But if your input wildcard is * then the archive file will be included as
well which is not correct behavior. Add the logic to detect this and put your solution
into mytar3.c (copied from mytar2.c).
Example: (Automatically skip the archive file from the list of source files.)
$ ./mytar myfiles.mt *
Output file myfiles.mt already exists, overwrite? (y/N): y
Creating archive file ...
Input file same as output file, skipped.
Adding file mytar to archive ...
Adding file mytar.c to archive ...
Adding file myuntar to archive ...
Adding file myuntar.c to archive ...
Input file outdir is a directory, skipped..
Total 4 files and 35407 bytes archived successfully.
Problem 1 We are going to develop our own tar and

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!