|
From: Giovanni C. <val...@li...> - 2000-12-23 16:50:35
|
Simo Sorce wrote:
>
> > /* support functions */
> > int check_file (char* file)
> > {
> > union block block;
> > char* magic;
> > int retval = 0;
> > FILE* fp = fopen(file, "rb");
>
> I think you should use standard file descriptors (open, write, read,
> seek) and not file streams as it is more portable across systems.
I prefer open() calls too, but I found that POSIX-style calls have
nothing like ftell() which I think may be useful, esp. in extracting or
deleting files.
Should my guess happen to be wrong, I'd happily switch to open() calls.
>
> >
> > if (!fp)
> > return -1;
> >
> > if (fread(&block, sizeof(union block), 1, fp) != 1)
> > {
> > fclose(fp);
> > return -1;
> > }
> >
> > magic = block.header.magic;
> >
> > if (memcmp(magic, TMAGIC, sizeof(char)*TMAGLEN))
> > retval = 1; /* posix archive */
> > if (memcmp(magic, OLDGNU_MAGIC, sizeof(char)* 8))
> > retval = 2; /* GNU archive */
>
> Using sizeof(char) in this contest is a bug, file format declares magic
> beeing long 8 bytes!
> This is indipendent from how many bytes a char use on the platform.
> On platforms with chars of greater than 8bit size this would break as
> they will copy more bytes causing possible memory leaks.
I could have used strcmp() as well, but AFAIK memcmp() is more efficient
than strcmp(), even when comparing strings. So I prefered to force a
string comparison by use of memcmp(), but I had to be sure that it would
have compared 8 chars, not 8 bytes.
Do you think I should use strcmp(), instead of this hack?
|