Re: Stat mod time

From: Jeremy Elson (jelson@blaze.cs.jhu.edu)
Date: 09/07/94


> 
> I'm pretty sure this was just a typo, but shouldn't the code read:
> 
> struct stat statbuf;
> time-t modified time;
> 
> stat(MOTD_FILE, &statbuf);  /* I missed the & error the first time :) */
> modified_time = statbuf->st_mtime;
> 

No, it wasn't a typo.  The code above won't compile; the '->' should be a '.'
as I'd originally said.

> Since you mentioned it before, I am assuming the last line was just a typo. 

What I meant was that the version of the code which declared statbuf as a
'struct stat *' (note * at the end) should have used '->' instead of '.'.
But, that had the additional problem of not setting the statbuf pointer to
actually point at anything.

My version of the code does not declare statbuf to be a pointer and thus
does not require a CREATE, and requires a '.' instead of '->'.

To clear up any confusion, I'll give two versions of the code.  Both work,
but the first is better for obvious reasons.

Method 1:

struct stat statbuf;
time_t modified time;

stat(MOTD_FILE, &statbuf);
modified_time = statbuf.st_mtime;



Method 2:

struct stat *statbuf;
time_t modified time;

CREATE(statbuf, struct stat, 1);
stat(MOTD_FILE, statbuf);
modified_time = statbuf->st_mtime;
free(statbuf);



Jeremy



This archive was generated by hypermail 2b30 : 12/07/00 PST