OK, so I was thinking of a program that displays my folder's or file's last modified time; I succeeded doing that, but I passed only the path as argument.
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
int main(int count , char *args[]){
char buffer[50];
struct stat attr;
if(count < 2){
printf("No parameters passed");
}
else{
strcpy(buffer,args[1]);
stat(buffer,&attr);
printf("Last modiffied date time : % s" , ctime(&attr.st_mtime));
}
return 0;
}
Which works! But I was wondering if I could do that using a DIR instead a char path. Something like:
.........
DIR *mydir;
struct dirent *dir;
dir = opendir(args[1]);
NOTES: However I found out that the stat function that I used earlier (path as argument) has these parameters:
int stat(const char*restrict path, struct stat *restrict buf);
Which took me to this question:
How can I really show the stat of the folder (in my case last modified date), if I can't use this function?
EDIT
My try so far using dirent:
..........
DIR *mydir;
struct stat attr;
mydir = opendir(args[1]);
stat(mydir,&attr);
printf("last modified is... %s" , ctime(&attr.st_mtime));
return 0;
I get this warning:
warning: passing argument 1 of ‘stat’ from incompatible pointer type /usr/include/sys/stat.h:211: note: expected ‘const char * __restrict__’ but argument is of type ‘struct DIR *’
The answer is no. You can't use a DIR
with stat
.
What's wrong with passing the name of the directory?