Implement Timestamps() for UNIX et al.

This commit is contained in:
2019-04-28 22:37:42 +01:00
parent 0ef6ef82c0
commit 781e5a6b63
2 changed files with 286 additions and 1 deletions

View File

@@ -48,6 +48,7 @@ Copyright (C) 2011-2018 Natalia Portillo
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <utime.h>
void GetOsInfo()
{
@@ -519,7 +520,279 @@ void Filenames(const char *path)
void Timestamps(const char *path)
{
// TODO: Implement
char message[300];
int ret;
time_t ftLastAccessTime;
time_t ftLastWriteTime;
FILE * h;
int rc, wRc, cRc, tRc;
struct utimbuf times;
ret = chdir(path);
if(ret)
{
printf("Error %d changing to specified path.\n", errno);
return;
}
ret = mkdir("TIMES", 0755);
if(ret)
{
printf("Error %d creating working directory.\n", errno);
return;
}
ret = chdir("TIMES");
if(ret)
{
printf("Error %d changing to working directory.\n", errno);
return;
}
printf("Creating timestamped files.\n");
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MAXATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, MAXDATETIME, "access");
times.actime = MAXTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MAXATIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MAXATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MAXMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, MAXDATETIME, "modification");
times.modtime = MAXTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MAXMTIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MAXMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MINATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, MINDATETIME, "access");
times.actime = MINTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MINATIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MINATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("MINMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, MINDATETIME, "modification");
times.modtime = MINTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("MINMTIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "MINMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y1KATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, Y1KDATETIME, "access");
times.actime = Y1KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y1KATIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y1KATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y1KMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, Y1KDATETIME, "modification");
times.modtime = Y1KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y1KMTIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y1KMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y2KATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, Y2KDATETIME, "access");
times.actime = Y2KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y2KATIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y2KATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("Y2KMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, Y1KDATETIME, "modification");
times.modtime = Y2KTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("Y2KMTIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "Y2KMTIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("LESSATIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, LESSDATETIME, "access");
times.actime = LESSTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("LESSATIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "LESSATIME", rc, wRc, cRc, tRc);
memset(&times, 0, sizeof(struct utimbuf));
h = fopen("LESSMTIME", "w+");
rc = 0;
wRc = 0;
cRc = 0;
tRc = 0;
if(h == NULL) { rc = errno; }
else
{
memset(&message, 0, 300);
sprintf((char *)message, DATETIME_FORMAT, LESSDATETIME, "modification");
times.modtime = LESSTIMESTAMP;
ret = fprintf(h, message);
if(ret < 0) { wRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
ret = utime("LESSMTIME", &times);
if(ret) { tRc = errno; }
}
printf("\tFile name = \"%s\", rc = %d, wRc = %d, cRc = %d, tRc = %d\n", "LESSMTIME", rc, wRc, cRc, tRc);
}
void DirectoryDepth(const char *path)

View File

@@ -375,6 +375,18 @@
#define ST_NODIRATIME 2048
#endif
#define DATETIME_FORMAT "This file is dated %s for %s\n"
#define LESSDATETIME "2106/02/07 06:28:15 or unknown"
#define MAXDATETIME "2038/01/19 03:14:07"
#define MINDATETIME "1970/01/01 00:00:00"
#define Y2KDATETIME "2000/01/01 00:00:00"
#define Y1KDATETIME "1999/12/31 23:59:59"
#define LESSTIMESTAMP 0xFFFFFFFF
#define MAXTIMESTAMP 0x7FFFFFFF
#define MINTIMESTAMP 0x00000000
#define Y2KTIMESTAMP 0x386D4380
#define Y1KTIMESTAMP 0x386D437F
#endif
#endif // SETTER_UNIX_H