1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
*/
#define _XOPEN_SOURCE /* WHY???? */
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <time.h>
/* Requirements in common.h */
#define REQ_PRINT_USAGE
#define REQ_ERRPRINT
#include "../common/common.h"
#define DESCRIPTION "Change file access and modification times."
#define OPERANDS "[-acm] [-t [[CC]YY]MMDDhhmm[.SS]] file..."
int main(int argc, char *argv[]) {
int file, i, argument;
struct timespec times[2];
struct tm time_tm = { 0 }; /* Used by strptime() */
char param[256], *argv0 = strdup(argv[0]), *datefmt, *date_string;
for (i = 0; i < 256 && (param[i] = 0); i++);
while ((argument = getopt(argc, argv, "acmt:")) != -1) {
if (argument == '?')
return print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
else if (argument == 't') {
switch (strlen(optarg)) {
case 8: /* MMDDhhmm */
datefmt = "%m%d%H%M";
break;
case 10: /* YYMMDDhhmm */
datefmt = "%y%m%d%H%M";
break;
case 12: /* CCYYMMDDhhmm */
datefmt = "%C%y%m%d%H%M";
break;
case 13: /* MMDDhhmm.SS */
datefmt = "%m%d%H%M.%S";
break;
case 15: /* YYMMDDhhmm.SS */
datefmt = "%y%m%d%H%M.%S";
break;
case 17: /* CCYYMMDDhhmm.SS */
datefmt = "%C%y%m%d%H%M.%S";
break;
default:
return print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
}
date_string = optarg;
}
param[(uint8_t)argument] = 1;
} argc -= optind; argv += optind;
if (argc < 1) return print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
if (param['t']) {
strptime(date_string, datefmt, &time_tm);
if (errno) return errprint(argv0, "strptime()", errno);
if (param['a'])
times[0].tv_sec = mktime(&time_tm);
if (param['m'])
times[1].tv_sec = mktime(&time_tm);
times[0].tv_nsec = 0; times[1].tv_nsec = 0;
} else {
times[0].tv_sec = time(NULL); times[1].tv_sec = time(NULL);
times[0].tv_nsec = 0; times[1].tv_nsec = 0;
}
for (i = 0; i < argc; i++) {
if (access(argv[i], F_OK) == -1 && errno == ENOENT && !param['c']) {
/* File does not exist */
file = creat(argv[i],
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
futimens(file, times);
} else if (access(argv[i], F_OK) != -1)
/* File exists */
utimensat(AT_FDCWD, argv[i], times, 0);
else if (errno != ENOENT) return errprint(argv0, argv[i], errno);
}
return errprint(argv0, NULL, errno);
}
|