I wrote some code to display the last modified time and file names. My code compiles, but I need to change the format of the timestamp
What I want is:
Jul 17 12:12 2013 2-s.txt
Jul 17 12:12 2013 3-s.txt
What I'm getting now is:
Wed Jul 17 12:24:48 2013
2-s.txt
Wed Jul 17 12:24:48 2013
3-s.txt
Can someone take a look at my code below and give me some suggestions on how to fix it? Thank you!!!
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/utsname.h>
#include <ctype.h>
#include <string.h>
#include <ar.h>
#include <getopt.h>
#include <time.h>
#include <utime.h>
int main (int argc, char **argv)
{
FILE *fp;
size_t readNum;
long long tot_file_size, cur_file_size;
struct stat fileStat;
struct ar_hdr my_ar;
struct tm fileTime;
struct utimbuf *fileTime2;
//open the archive file (e.g., hw.a)
fp = fopen(argv[1], "r");
if (fp == NULL)
{
perror("Error opening the file\n");
exit(-1);
}
//size of the archive file
fseek(fp, 0, SEEK_END);
tot_file_size =ftell(fp);
rewind(fp);
//read data into struct
fseek(fp, strlen(ARMAG), SEEK_SET); //skip the magic string
while (ftell(fp) < tot_file_size - 1)
{
readNum = fread(&my_ar, sizeof(my_ar), 1, fp);
if (stat(argv[1], &fileStat) == -1) {
perror("stat");
exit(EXIT_FAILURE); //change from EXIT_SUCCESS to EXIT_FAILURE
}
if ((fileStat.st_mode & S_IFMT) == S_IFREG)
{
printf("%s", ctime(&fileStat.st_mtime));
printf("%.*s", 15, my_ar.ar_name);
}
cur_file_size = atoll(my_ar.ar_size);
if (fseek(fp, cur_file_size, SEEK_CUR) != 0)
{
perror("You have an error.\n");
exit(-1);
}
}
fclose(fp);
return 0;
}