-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.c
140 lines (128 loc) · 3.92 KB
/
date.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc == 2 || argc > 5)
{
printf("Invalid command\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
// checking for options
if (argv[2][0] == '-')
{
// checking for -R
if (strcmp(argv[2], "-R") == 0)
{
char *filename;
if (argc == 4)
filename = argv[3];
else
{
printf("Invalid command\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
struct stat fileTime;
if (stat(filename, &fileTime) == -1)
{
perror("Error");
return 1;
}
char rfcTime[64];
strftime(rfcTime, sizeof(rfcTime), "%a, %d %b %Y %H:%M:%S %z", gmtime(&fileTime.st_mtime));
// printing time
printf("%s\n", rfcTime);
}
// checking for -d
else if (strcmp(argv[2], "-d") == 0)
{
char *filename;
if (argc == 5)
filename = argv[4];
else
{
printf("Invalid command\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
// getting the file stats
struct stat fileTime;
if (stat(filename, &fileTime) == -1)
{
perror("Error");
return 1;
}
// to remove the single quotes '' from the argument
int length = strlen(argv[3]);
if (argv[3][0] == '\'' && argv[3][length - 1] == '\'')
{
memmove(argv[3], argv[3] + 1, length - 2);
argv[3][length - 2] = '\0';
}
else
{
printf("String not supported\n");
printf("supported STRINGS: 'tomorrow', 'yesterday'\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
// Checking is STRING is 'yesterday'
if (strcmp(argv[3], "yesterday") == 0)
{
time_t current_time = fileTime.st_mtime;
// struct for saving time
struct tm *modification_tm = localtime(¤t_time);
modification_tm->tm_mday--;
time_t yesterday_time = mktime(modification_tm);
printf("%s", ctime(&yesterday_time));
}
// checking for tomorrow
else if (strcmp(argv[3], "tomorrow") == 0)
{
time_t current_time = fileTime.st_mtime;
struct tm *modification_tm = localtime(¤t_time);
modification_tm->tm_mday++;
time_t tomorrow_time = mktime(modification_tm);
printf("%s", ctime(&tomorrow_time));
}
else
{
printf("String not supported\n");
printf("supported STRINGS: 'tomorrow', 'yesterday'\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
}
else
{
printf("Invalid Command\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
}
// if no arguments
else
{
char *filename;
if (argc == 3)
filename = argv[2];
else
{
printf("Invalid Command\n");
printf("Usage: date {-R|-d} <filename>\n");
exit(1);
}
struct stat fileTime;
if (stat(filename, &fileTime) == -1)
{
perror("Error getting file status");
return 1;
}
// printing files latest modified date
printf("%s", ctime(&fileTime.st_mtime));
}
}