Ok i have a homework that is supposed to do what ls -al command does. I have most of the program done except from the date. Here is my code so far:
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
main(int argc, char *argv[])
{
DIR *dirp;
struct dirent *dentry;
struct stat buffer;
int errors = 0;
char *dirname = ".";
const char* type;
if (argc > 1)
dirname = argv[1];
//change to specified directory and open for reading
if (chdir(dirname) == -1 || (dirp = opendir(".")) == (DIR *)NULL)
{
fprintf(stderr, "%s: cannot access requested directory\n",
argv[0]);
exit(1);
}
printf("Name \tType \tSize \t Access Date\n");
// iterate through directory contents
while ((dentry = readdir(dirp)) != (struct dirent *)NULL)
{
/* fetch inode information */
if (stat(dentry->d_name, &buffer) == (-1))
{
fprintf(stderr, "%s: cannot access %s\n",
argv[0], dentry->d_name);
errors++;
continue;
}
if(S_ISDIR (buffer.st_mode))
type = "<DIR>";
else
type = " ";
printf("%-25s\t%s\t%d\t%d\n", dentry->d_name, type,
buffer.st_size, buffer.st_atime);
}
closedir(dirp);
exit(errors);
}
The problem with this is it shows the time in seconds and i need to display the date. my teacher wants this printed out to screen:
FileName FileType Size Day Date
writer <DIR> 288 Mon Dec 17 10:20:32 2001
and this is my output:
mail <DIR> 512 1082356227
public_html <DIR> 512 1082356227
thanks in advance...