inoobdotcom
inoobdotcom

Reputation: 139

printing only filename not permissions and date

I am reading in an archive file from Unix and I want to read the files in and print what files are inside the archive and just the filename. For an example, I need hello.c not hello.c 123124123 001004002 1000 0.

Here is the I have right now

input_fd = open(archive_name, O_RDONLY);


while( (read(input_fd, buf, BUF_SIZE)) > 0) {
    printf("%s\n", buf);
}
close(input_fd);

Upvotes: 2

Views: 89

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754780

How portable does your code need to be? By far the simplest way to achieve the effect is to use the ar command itself. To get the output you desire:

ar t archive

There have been many archive formats in existence, and there are still a number of variants in common use (AIX in particular is rather different from 'the rest'). There are different ways of handling long file names. To read the archive accurately, you will need to be able to read the header, possibly the string list, skip over the pseudo-file related to library symbols that are defined, and then process each file header and skip over the file itself, repeating until the end of the archive. When you read the header, you will need to split it appropriately to isolate just the name.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182694

What you are doing seems weird to me but if you have a C-string (you're printing buf in the code and it produces sensible output) you can try to NUL-terminate it:

char *p;

if ((p = strchr(buf, ' '))
    *p = 0;
printf("%s\n", buf);

Upvotes: 2

Related Questions