Don_k 0 Newbie Poster

Hi, I would like to using POSIX API system calls in C, and via command line arguments know how to go about extracting from an archive file.(in Linux).

I have done the archiving process it archives the files I enter into the command line , and the last but one file mentioned is the archived file.

e.g. ./tar file1 archivedfile

    Contents of file1 = abc.

    archivedfile

    now looks like:

    archivedfile(this is a header)

    abc.

How would I by using the filename (the header in archive file) extract the archive file out.

HERE is my pseudo code of how I want it to work:

if(argv[i]==0){printf("extracting...");} /*//this means if the file entered 
//on command line is archivefile you want to extract.then go onto extract process...
while(buffer[count]!='\n')     //loop to get filename.
buffer[count]='\0';
{//add characters to array e.g. filestringarray
filename=buffer[count];
count++;
set pointer using lseek to poin at if fname=file1 then lseek to point to 6 until reach start of next file.
lseek(count+1SEEK_SET);

  another loop to get body of file:while(..){}
}
createopen a file from filename,read filename, read contents.
need to use strtok to find \n token for filename...*/

The above is how I would like my code to work if anyone could help me on any of the parts mentioned above It would be greatly appreciated. For example even the first line argvi==0 I'm not sure that even makes sense so, what I wanted the line to do is if the filename entered by the user is an archive file then continue with the extraction process of the archived file.

Thank you all for any help.

Dani AI

Generated

— The basic idea (archive = header + file bytes) is fine, but the parser and archive format need a couple of concrete rules to make extraction reliable.

An archive must record where each file ends. The two safe options are: (1) include the file size in the header, or (2) use fixed-size headers with a known layout. Relying on a newline-only separator for filenames will break on binary files or filenames that contain newlines. Add a small magic string at the archive start to detect the format.

A robust extraction loop (POSIX) looks like this in steps:

  • open the archive with open(archive_path, O_RDONLY).
  • verify the magic bytes if present.
  • read a header (e.g., filename + ASCII size + newline).
  • parse the size with strtoull into an off_t.
  • create the destination file with open(destname, O_CREAT|O_TRUNC|O_WRONLY, mode) (consider O_NOFOLLOW or mkstemp for safety).
  • read exactly size bytes from the archive in a loop (handle partial reads and EINTR) and write them to the destination file (handle partial writes).
  • repeat until EOF.

A minimal helper to read N bytes reliably is recommended; never assume a single read() returns all requested bytes. For large files, stream in fixed-size chunks rather than allocating one big buffer. Use lseek() only to skip known lengths; sequential read() is simpler for extraction.

Troubleshooting notes: check all syscall return values and errno, enforce a maximum filename length (e.g., PATH_MAX), sanitize filenames (avoid directory traversal like ../), and preserve file permissions if needed. For reference on POSIX calls used here see open(2), read(2), write(2), and lseek(2):

open(2)
read(2)
write(2)
lseek(2)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.