Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
27
Task/File-modification-time/C/file-modification-time-1.c
Normal file
27
Task/File-modification-time/C/file-modification-time-1.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <utime.h>
|
||||
|
||||
const char *filename = "input.txt";
|
||||
|
||||
int main() {
|
||||
struct stat foo;
|
||||
time_t mtime;
|
||||
struct utimbuf new_times;
|
||||
|
||||
if (stat(filename, &foo) < 0) {
|
||||
perror(filename);
|
||||
return 1;
|
||||
}
|
||||
mtime = foo.st_mtime; /* seconds since the epoch */
|
||||
|
||||
new_times.actime = foo.st_atime; /* keep atime unchanged */
|
||||
new_times.modtime = time(NULL); /* set mtime to current time */
|
||||
if (utime(filename, &new_times) < 0) {
|
||||
perror(filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
24
Task/File-modification-time/C/file-modification-time-2.c
Normal file
24
Task/File-modification-time/C/file-modification-time-2.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <err.h>
|
||||
|
||||
const char *filename = "input.txt";
|
||||
|
||||
int main() {
|
||||
struct stat foo;
|
||||
struct timeval new_times[2];
|
||||
|
||||
if (stat(filename, &foo) < 0)
|
||||
err(1, "%s", filename);
|
||||
|
||||
/* keep atime unchanged */
|
||||
TIMESPEC_TO_TIMEVAL(&new_times[0], &foo.st_atim);
|
||||
|
||||
/* set mtime to current time */
|
||||
gettimeofday(&new_times[1], NULL);
|
||||
|
||||
if (utimes(filename, new_times) < 0)
|
||||
err(1, "%s", filename);
|
||||
|
||||
return 0;
|
||||
}
|
||||
30
Task/File-modification-time/C/file-modification-time-3.c
Normal file
30
Task/File-modification-time/C/file-modification-time-3.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const char *filename = "input.txt";
|
||||
|
||||
int main() {
|
||||
struct stat foo;
|
||||
struct timespec new_times[2];
|
||||
|
||||
if (stat(filename, &foo) < 0) {
|
||||
perror(filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* keep atime unchanged */
|
||||
new_times[0] = foo.st_atim;
|
||||
|
||||
/* set mtime to current time */
|
||||
clock_gettime(CLOCK_REALTIME, &new_times[1]);
|
||||
|
||||
if (utimensat(AT_FDCWD, filename, new_times, 0) < 0) {
|
||||
perror(filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
107
Task/File-modification-time/C/file-modification-time-4.c
Normal file
107
Task/File-modification-time/C/file-modification-time-4.c
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/* Print "message: last Win32 error" to stderr. */
|
||||
void
|
||||
oops(const wchar_t *message)
|
||||
{
|
||||
wchar_t *buf;
|
||||
DWORD error;
|
||||
|
||||
buf = NULL;
|
||||
error = GetLastError();
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL, error, 0, (wchar_t *)&buf, 0, NULL);
|
||||
|
||||
if (buf) {
|
||||
fwprintf(stderr, L"%ls: %ls", message, buf);
|
||||
LocalFree(buf);
|
||||
} else {
|
||||
/* FormatMessageW failed. */
|
||||
fwprintf(stderr, L"%ls: unknown error 0x%x\n",
|
||||
message, error);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
setmodtime(const wchar_t *path)
|
||||
{
|
||||
FILETIME modtime;
|
||||
SYSTEMTIME st;
|
||||
HANDLE fh;
|
||||
wchar_t date[80], time[80];
|
||||
|
||||
fh = CreateFileW(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
|
||||
0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (fh == INVALID_HANDLE_VALUE) {
|
||||
oops(path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Use GetFileTime() to get the file modification time.
|
||||
*/
|
||||
if (GetFileTime(fh, NULL, NULL, &modtime) == 0)
|
||||
goto fail;
|
||||
FileTimeToSystemTime(&modtime, &st);
|
||||
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
|
||||
date, sizeof date / sizeof date[0]) == 0 ||
|
||||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
|
||||
time, sizeof time / sizeof time[0]) == 0)
|
||||
goto fail;
|
||||
wprintf(L"%ls: Last modified at %s at %s (UTC).\n",
|
||||
path, date, time);
|
||||
|
||||
/*
|
||||
* Use SetFileTime() to change the file modification time
|
||||
* to the current time.
|
||||
*/
|
||||
GetSystemTime(&st);
|
||||
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
|
||||
date, sizeof date / sizeof date[0]) == 0 ||
|
||||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL,
|
||||
time, sizeof time / sizeof time[0]) == 0)
|
||||
goto fail;
|
||||
SystemTimeToFileTime(&st, &modtime);
|
||||
if (SetFileTime(fh, NULL, NULL, &modtime) == 0)
|
||||
goto fail;
|
||||
wprintf(L"%ls: Changed to %s at %s (UTC).\n", path, date, time);
|
||||
|
||||
CloseHandle(fh);
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
oops(path);
|
||||
CloseHandle(fh);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Show the file modification time, and change it to the current time.
|
||||
*/
|
||||
int
|
||||
main()
|
||||
{
|
||||
int argc, i, r;
|
||||
wchar_t **argv;
|
||||
|
||||
/* MinGW never provides wmain(argc, argv). */
|
||||
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
||||
if (argv == NULL) {
|
||||
oops(L"CommandLineToArgvW");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (argc < 2) {
|
||||
fwprintf(stderr, L"usage: %ls file...\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
r = 0;
|
||||
for (i = 1; argv[i] != NULL; i++)
|
||||
if (setmodtime(argv[i])) r = 1;
|
||||
return r;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue