[comp.sys.amiga.tech] Wanted: Filelength

a665@mindlink.UUCP (Anthon Pang) (07/30/90)

I don't think there's a function that returns the filelength of a file, with
AmigaDOS.  But I need an equivalent...how do you determine the length of a file
given only the file handle (eg from an Open)?  There must be a way to find the
associated file lock, as one is created when the file is opened (shared /
exclusive)...right?  Then you can Examine the FileInfoBlock and get the file's
length from the fibSize field.

    ie    size_t  filelength(int handle)

I want a function that doesn't require knowing the filename...or do I need 2.0?

Thanks.

markv@kuhub.cc.ukans.edu (08/01/90)

> I don't think there's a function that returns the filelength of a file, with
> AmigaDOS.But I need an equivalent...how do you determine the length of a file
> given onlythe file handle (eg from an Open)?  There must be a way to find the
> associated file lock, as one is created when the file is opened (shared /
> exclusive)...right? Then you can Examine the FileInfoBlock and get the file's
> length from the fibSize field.
> 
>     ie    size_t  filelength(int handle)
> 
>Iwant a function that doesn't require knowing the filename...or do I need 2.0?
> 
> Thanks.

If you use C stream handles (not Amiga ones) just try the following function:

#include <stdio.h>

size_t filelength(FILE *FooFile)
{
    pos_t	save_pos;
    size_t	temp;

    /*
    ** fseek() and ftell() do all the work.  fgetpos() and fsetpos() insure
    ** that we leave the file the way we got it.
    */

    save_pos = fgetpos(FooFile);
    fseek(FooFile, 0, SEEK_END);
    temp = ftell(FooFile);
    fsetpos(FooFile, save_pos);
    return(temp);
}
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mark Gooderum			Only...		\    Good Cheer !!!
Academic Computing Services	       ///	  \___________________________
University of Kansas		     ///  /|         __    _
Bix:	  markgood	      \\\  ///  /__| |\/| | | _   /_\  makes it
Bitnet:   MARKV@UKANVAX		\/\/  /    | |  | | |__| /   \ possible...
Internet: markv@kuhub.cc.ukans.edu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

forgeas@swinjm.UUCP (Jean-Michel Forgeas) (08/01/90)

In article <2671@mindlink.UUCP> a665@mindlink.UUCP (Anthon Pang) writes:
>I don't think there's a function that returns the filelength of a file, with
>AmigaDOS.  But I need an equivalent...how do you determine the length of a file
>given only the file handle (eg from an Open)?  There must be a way to find the
>[...]
>Thanks.

Alexander Livshits showed me this little trick, here it is:

int filelength( fh )
{
    Seek( fh, 0, OFFSET_END );
    return( Seek( fh, 0, OFFSET_BEGINNING ) );
}

--
                                     \___/
Jean-Michel Forgeas                   \-/
cbmvax!cbmehq!cbmfra!swinjm!forgeas    |    The Software Winery
                                      -^-
                           And, where is the universe ?

caw@miroc.Chi.IL.US (Christopher A. Wichura) (08/02/90)

In article <2671@mindlink.UUCP> a665@mindlink.UUCP (Anthon Pang) writes:
>I don't think there's a function that returns the filelength of a file, with
>AmigaDOS.  But I need an equivalent...how do you determine the length of a file
>given only the file handle (eg from an Open)?  There must be a way to find the

This is easy to do.  After opening the file, do a Seek() to an offset of zero
from the end of the file.  Then do another Seek() to offset zero from
current and the return value is the size of the file.  Granted, this is a
little more overhead than having a Lock() and doing an Examine(), but it is
a lot easier.

-=> CAW

/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                          Christopher A. Wichura

               caw@miroc.chi.il.us      (my amiga)         
               u12401@uicvm.uic.edu     (my school account)

Please! Do not send mail to my school account unless mail to miroc bounces.
    I often do not check uicvm.uic.edu for periods in excess of a week.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\//////////////////////////////////////

brians@hpcljms.HP.COM (Brian Sullivan) (08/02/90)

> I don't think there's a function that returns the filelength of a file, with
> AmigaDOS.  But I need an equivalent...how do you determine the length of a file
> given only the file handle (eg from an Open)?  There must be a way to find the
> associated file lock, as one is created when the file is opened (shared /
> exclusive)...right?  Then you can Examine the FileInfoBlock and get the file's
> length from the fibSize field.
> 
>     ie    size_t  filelength(int handle)
> 
> I want a function that doesn't require knowing the filename...or do I need 2.0?
> 
> Thanks.

   I needed such a function once before, when porting the UNIX compress
utility to the Amiga.   I you are using Lattice C you can you the lseek(2)
library call like this:

        off_t  sz;
        ...
        sz = lseek( fd, 0, SEEK_END);


        

jesup@cbmvax.commodore.com (Randell Jesup) (08/02/90)

In article <2671@mindlink.UUCP> a665@mindlink.UUCP (Anthon Pang) writes:
>I don't think there's a function that returns the filelength of a file, with
>AmigaDOS.  But I need an equivalent...how do you determine the length of a file
>given only the file handle (eg from an Open)?  There must be a way to find the
>associated file lock, as one is created when the file is opened (shared /
>exclusive)...right?  Then you can Examine the FileInfoBlock and get the file's
>length from the fibSize field.
>
>    ie    size_t  filelength(int handle)
>
>I want a function that doesn't require knowing the filename...or do I need 2.0?

	Well, to do it simply requires 2.0 (which has ExamineFH).  Under
< 2.0, use this:

	...
	oldpos = Seek(fh,0L,OFFSET_END);
	size = Seek(fh,oldpos,OFFSET_BEGINNING);
	...

	In other words, seek to the end and see where that is.

-- 
Randell Jesup, Keeper of AmigaDos, Commodore Engineering.
{uunet|rutgers}!cbmvax!jesup, jesup@cbmvax.cbm.commodore.com  BIX: rjesup  
Common phrase heard at Amiga Devcon '89: "It's in there!"

peter@sugar.hackercorp.com (Peter da Silva) (08/02/90)

Best save and restore the current position, as well.

> int filelength( fh )
> {
+     int old_off;
!     old_off = Seek( fh, 0, OFFSET_END );
!     return( Seek( fh, old_off, OFFSET_BEGINNING ) );
> }

Might also want to make things "long" just in case someone compiles it under
old Manx. Or to be really minimalist:

#define filelength(fh) Seek(fh, Seek(fh, 0L, OFFSET_END), OFFSET_BEGINNING)

(oddly, my old DOS manual defines both OFFSET_END and OFFSET_BEGINNING as 1)
-- 
Peter da Silva.   `-_-'
<peter@sugar.hackercorp.com>.