lkml.org 
[lkml]   [2005]   [Nov]   [12]   [last100]   RSS Feed
Views: [more markup]  [less markup]  [headers]  [forward] 
 
Messages in this thread
/
DateSat, 12 Nov 2005 08:41:36 -0500
FromBill Davidsen <>
SubjectRe: Compatible fstat()
Parag Warudkar wrote:

>
> On Nov 8, 2005, at 5:57 PM, Bill Davidsen wrote:
>
>> Parag Warudkar wrote:
>>
>>> On Nov 8, 2005, at 2:39 PM, Bob Copeland wrote:
>>>
>>>> Isn't this just because the device size is > 2**32?  What if you   
>>>> use fseeko(3)
>>>> and #define _FILE_OFFSET_BITS 64?
>>>>
>>> Yep. I got it to return the correct hard disk size (17Gb) using   
>>> lseek64 and
>>> #define _LARGEFILE64_SOURCE
>>> #define _FILE_OFFSET_BITS 64
>>> Here is what I did
>>> -------------------------------------------------
>>> #define _LARGEFILE64_SOURCE
>>> #define _FILE_OFFSET_BITS 64
>>> #include <stdio.h>
>>> #include <unistd.h>
>>> #include <fcntl.h>
>>> int main()
>>> {
>>>         int f;
>>>         off64_t off=0;
>>
>> Why is this initialized?
>>
>>>         f = open("/dev/hda", O_RDONLY );
>>>         if(f <= 0){
>>>                 perror("open");
>>>                 exit(0);
>>>         }
>>>         off = lseek64(f, 0, SEEK_SET);
>>
>> Why do this? it always returns zero.
>>
>>>         off = lseek64(f, 0, SEEK_END);
>>>         perror("llseek");
>>>         printf ("Size %lld\n", off);
>>>         close(f);
>>>         return 0;
>>> }
>>
>> RETURN VALUE
>>   Upon successful completion, lseek returns the resulting offset
>>   location as measured in bytes from the beginning of the  file.
>>   Otherwise,  a  value  of  (off_t)-1 is returned and errno is
>>   set to indicate the error.
>>
>
> You took it a little too seriously! It was from the scribble-and- 
> shuffle-till-it-works department ;) Sole purpose was to figure out a  
> way to print the disk size from the device - some how that is!
>
> But thanks for pointing out anyway - it doesn't hurt to be correct no  
> matter what the purpose is. 

You could look at how blockdev does it, the --getss (sector size) and 
--getsize (size in sectors) seem to work, based on trying it on a total 
of two machines ;-) Even gives correct size for software RAID arrays!

-- 
bill davidsen <davidsen@tmr.com>
  CTO TMR Associates, Inc
  Doing interesting things with small computers since 1979

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-11-12 13:35    [from the cache]
©2003-2008