I’m checking whether a file has been modified or not in order to refresh the content…
However, I need a couple improvements :
- How do I test the return value of stat to tell if it failed ?
- The returned errno numbers may change between glibc versions, right? In this case it is useless to do
errno==… How do I fix that?
int is_refreshing_needed (char * path)
{
int refresh_content;
struct stat file;
stat(path, &file);
//File does not exist or Permission Denied or the file has not been modified
if( errno == 2 || errno == 13 || file.st_atime > file.st_mtime ) {
refresh_content=0;
}
else {
refresh_content=1;
}
return refresh_content;
}
“man stat” is your friend. This is from the ‘return values” section of the man page:
“Upon successful completion, 0 is returned. Otherwise, -1 is returned and errno is set to indicate the error.”
The return values include:
EOI, EOVERFLOW, EACCESS, EFAULT
and there are others. These errors are errors from the stat() system call and will not change with gcc/glibc version.
to access the error information, you need to include
#include <errno.h>and you can use the perror() function to get the error text (you need to
#include <stdio.h>for the perror() declaration). see “man perror” for more information.also, please don’t use numbers like 2 (No such file or directory) and 13 (Permission denied). please use the #define’ed names to make the code easier to read.
good luck!