Subject: Re: lseek() continued! DOH!
To: None <netbsd-help@NetBSD.ORG>
From: None <tooleym@douglas.bc.ca>
List: netbsd-help
Date: 02/04/1998 14:06:46
> : > It still sounds like you're treating the offset parameter and return
> : > value of lseek() as 32-bit values... they're 64-bit values. When you
> : > printf lseek's return value, you have to use a %qd, not %ld or %d. And
> : > make sure you #include <unistd.h>
> 
> Let me give you a shot in the dark rewrite.
> 
> You'll note four changes:
> 
> - status is an off_t (that's the return value of lseek(), though
>   conceivably `long' would work if the rest of the code worked ;).
> - the removal of `L' from constants assigned to pos (if `pos' is long,
>   that's overkill, it's just a cleanup).
> - a sizeof(record) is changed to a sizeof(struct record), at the `^^^^^'.
>   Perhaps this was a typo, or you `typedef struct record record'?
> - casting to off_t is done after all calculations EXCEPT NEGATING is done.
>   (From memory:) When you evaluate a sizeof expression, its type is
>   `unsigned int'.  Negating it keeps it an `unsigned int', even though you
>   negated it properly in 32-bit math.  If you cast first, it becomes off_t
>   (`signed long long'), which should work. 
> 
> The last change is what I think will fix this code as per the problem you
> have.

Yes! It was the off_t promotion I needed to make the program work
correctly. I had eliminated all the -Wall errors, I even started running
it through lint (hee hee what a funny program) on whatever the maximum
annoyance level is, and I had rewritten all the references to the offset
values to include the (off_t) casting, however it wasn't until I put the
negative sign in the correct place that everything just started magically
to work! Thank you so much for your help.. sometimes I think I
deliberately force myself to solve a problem in a particular way simply
because I know the logic is correct and I'm stubborn as heck--and though
rewriting it into new code would have saved me a lot of time, the
satisfaction that I can finally nail this one down (and can learn about
how to properly cast at the same time) is great.

Thanks Todd! You da man!

Marc Tooley
tooleym@douglas.bc.ca

p.s. ktrace() is indeed my friend--as long as I cast things correctly. :)