This is almost half my program. I'm currently working on it in small parts. The immediate problem is about grabbing file info using lstat and converting the time modified to local time.
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>
int main( int argc, char** argv)
{
for ( int i = 1; i < argc; i++)
{
struct stat buffer;
int stat = lstat(argv[i], &buffer);
if ( stat != 0 )
{
printf("Cannot open file %s.\n", argv[i]);
continue;
}
/***********************************************************/
char buf[1024];
ssize_t len;
if ((len = readlink(argv[i], buf, sizeof(buf)-1)) != -1)
buf[len] = '\0';
//printf("%zu\n", len);
/***********************************************************/
struct tm *tmz;
time_t result = time(NULL);
tmz = localtime(&buffer.st_mtime);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&result)), (uintmax_t)result);
printf("year: %d mon: %d day: %d\n",
tmz->tm_year, tmz->tm_mon, tmz->tm_mday);
}
return 0;
}
Here is the output:
Code:
./l9stat ../loglog.big-o.gif //<- some input file
Sun Nov 27 15:43:24 2011
1322437404 secs since the Epoch
year: 111 mon: 10 day: 27
The first line is the current time which is what I want. The second line is there for debugging. The last line isn't giving me what I want. It's supposed to print info about the input file I give it.