Welcome, Guest. Please login or register.

Author Topic: How do I check size/date of a file in ANSI C?  (Read 3994 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline ChaosLordTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
How do I check size/date of a file in ANSI C?
« on: February 16, 2006, 04:51:05 AM »
How do I check size and date of a file in ANSI C?
Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline Castellen

Re: How do I check size/date of a file in ANSI C?
« Reply #1 on: February 16, 2006, 05:11:38 AM »
I usually use stat() or lstat() though these might be more Unix specific???

More details in this thread.
 

Offline adolescent

  • Hero Member
  • *****
  • Join Date: Sep 2003
  • Posts: 3056
    • Show only replies by adolescent
Re: How do I check size/date of a file in ANSI C?
« Reply #2 on: February 16, 2006, 07:29:04 AM »
There's no function to get file date in pure ANSI C.  But, look for stat() like Castellen mentioned.
Time to move on.  Bye Amiga.org.  :(
 

Offline ChaosLordTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: How do I check size/date of a file in ANSI C?
« Reply #3 on: February 16, 2006, 07:58:15 AM »
I am already using stat() but the problem is that stat() in SASC triggers a read of a nonexistent environment variable TZ which is lame and slow.  I am doing thousands of stat() in a loop.

Accessing an environment variable for no reason every time you check the existence of a file sounds like something linux would do. :insane:  So that is why I was searching for a cleaner way that is also multiplatform.

Does stat() in all compilers trigger this lame environment variable access?  Or is it just some stupid thing in SASC?  Its easy to run SnoopDos to check.

How can I do surgery on my SASC to amputate the offending code? :hammer:

p.s. what the heck is the difference between stat() and lstat() ? :huh:

Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline chris

Re: How do I check size/date of a file in ANSI C?
« Reply #4 on: February 16, 2006, 10:02:00 AM »
Why not #ifndef around the block and add AmigaOS-specific replacement code?

You can use Lock() and Examine() or Open() and ExamineFH() to get a FileInfoBlock, which will tell you the date and size.

Chris
"Miracles we do at once, the impossible takes a little longer" - AJS on Hyperion
Avatar picture is Tabitha by Eric W Schwartz
 

Offline ChaosLordTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: How do I check size/date of a file in ANSI C?
« Reply #5 on: February 16, 2006, 10:09:17 AM »
@chris

The deal is that I could do your idea but
I would have to do it in many different places
and the code is already around 200,000 lines
and adding a bunch of giant blocks of #ifdef AMIGA
code would really messify things...

Do you know any secret trick to do it super elegantly and simply?  :-)
Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: How do I check size/date of a file in ANSI C?
« Reply #6 on: February 16, 2006, 10:35:00 AM »
Why not simply write a wrapper function that does the job you need and simply have the implementation of that function #ifdef AMIGA ?
int p; // A
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: How do I check size/date of a file in ANSI C?
« Reply #7 on: February 16, 2006, 10:37:02 AM »
Quote
ChaosLord wrote:
I am already using stat() but the problem is that stat() in SASC triggers a read of a nonexistent environment variable TZ which is lame and slow.  I am doing thousands of stat() in a loop.

Either that, or change to a different c.lib, or use the #define-method, or write your own stat()-code which gets linked before c.lib. The last method is probably the best solution, as your portable fallthrough solution would be limited to one maintainable place in your code.

Quote
Accessing an environment variable for no reason every time you check the existence of a file sounds like something linux would do. :insane:  So that is why I was searching for a cleaner way that is also multiplatform.

Since TZ is 'time zone', I am not at all impressed by this line of reasoning. That people do not use time zones and instead just force the clock of their computer to the right time is not SAS/C's fault---instead, it is following proper Unix programming guidelines.

Quote
p.s. what the heck is the difference between stat() and lstat() ? :huh:

On the Amiga, not really important, if memory serves me. The calls return different results when fed a file or a link, either hard or soft. One gives the results for the link itself, the other follows it through to the file pointed at. Links never worked properly under FFS. Or they did, but then only the softlink. Or was it the hardlink?
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline ChaosLordTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: How do I check size/date of a file in ANSI C?
« Reply #8 on: February 16, 2006, 11:07:38 AM »
Quote

Cymric wrote:
Quote
ChaosLord wrote:
I am already using stat() but the problem is that stat() in SASC triggers a read of a nonexistent environment variable TZ which is lame and slow.  I am doing thousands of stat() in a loop.

Either that, or change to a different c.lib, or use the #define-method, or write your own stat()-code which gets linked before c.lib. The last method is probably the best solution, as your portable fallthrough solution would be limited to one maintainable place in your code.

Okey dokey so I can make my own stat() function
that uses AmigaOS way of doing things that will fill in
a unix struct stat so all the time compare code will work without change.

BUT... how do I convert from AmigaDOS time format to Unix time_t format?


Quote

Quote
Accessing an environment variable for no reason every time you check the existence of a file sounds like something linux would do. :insane:  So that is why I was searching for a cleaner way that is also multiplatform.

Since TZ is 'time zone', I am not at all impressed by this line of reasoning. That people do not use time zones and instead just force the clock of their computer to the right time is not SAS/C's fault---instead, it is following proper Unix programming guidelines.

But the timezone of my computer has nothing to do with the
time that some file was created many years ago on someone elses computer...  Does it?

I guess you are saying that linux constantly converts all filedates to/from GMT?

So all filedates in linux are stored on the HD in GMT?

And since AmigaOS does not store filedates in GMT but
instead stores the actual date where it was created,
the stat() function in SASC should be rewritten
to NOT check TZ since adding in the user's TimeZone
under AmigaOS would then give the wrong date.

Correct?
Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline ChaosLordTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2003
  • Posts: 2608
    • Show only replies by ChaosLord
    • http://totalchaoseng.dbv.pl/news.php
Re: How do I check size/date of a file in ANSI C?
« Reply #9 on: February 16, 2006, 11:10:34 AM »
Quote

Karlos wrote:
Why not simply write a wrapper function that does the job you need and simply have the implementation of that function #ifdef AMIGA ?


Yes I was thinking the same thing  :-)

But I donno how to convert from Amiga time to Unix time_t time. :-?
Wanna try a wonderfull strategy game with lots of handdrawn anims,
Magic Spells and Monsters, Incredible playability and lastability,
English speech, etc. Total Chaos AGA
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: How do I check size/date of a file in ANSI C?
« Reply #10 on: February 16, 2006, 11:17:26 AM »
Don't both systems measure time (32-bit integer) as the number of seconds since a particular epoch date?

If so, provided you know the difference between them. If memory serves, the Unix epoch is 00:00:00 Jan 01 1970 and the Amiga epoch is 00:00:00 Jan 01 1978

I would assume, if you evaluate the amiga epoch date as a unix datestamp you have a displacement you can use to convert an amiga 32-bit datestamp to a unix one and vice versa.
int p; // A