6
6
#define OP_CUSTOM (1 << 0)
7
7
#define OP_SILENT (1 << 31)
8
8
9
- _Noreturn void __usage_error (char * msg , char * argv_0 )
9
+ static int operation = 0 ;
10
+ static char * argv_0 ;
11
+
12
+ _Noreturn void __usage_error (char * msg )
10
13
{
11
14
fprintf (stderr ,
12
15
"Error message: %s.\n"
13
16
"\n"
14
- "Usage: %s <path/to/file> [path/to/other/files] [ <optional arguments> ]\n"
17
+ "Usage: \n"
18
+ " %s <path/to/file> [path/to/other/files] [ <optional arguments> ]\n"
15
19
"\n"
16
20
"positional arguments:\n"
17
21
" path/to/payload\n"
@@ -22,19 +26,20 @@ _Noreturn void __usage_error(char* msg, char* argv_0)
22
26
" -s,--silent\tsuppress the standard output\n"
23
27
"\n"
24
28
"example:\n"
25
- " %s test.txt -c \"19990101000000\""
29
+ " %s test.txt -c \"19990101000000\"\n "
26
30
, msg , argv_0 , argv_0 );
27
31
exit (-1 );
28
32
}
29
33
30
- int parse_command_line (int argc , char * * argv )
34
+ char * parse_command_line (int argc , char * * argv )
31
35
{
32
- int operation = 0 ;
36
+
37
+ char * datetime = (char * )calloc (14 , sizeof (char ));
33
38
char buffer [32 ];
34
39
35
40
if (argc < 2 )
36
41
{
37
- __usage_error (argv [ 0 ], "No arguments supplied" );
42
+ __usage_error ("No arguments supplied" );
38
43
}
39
44
40
45
for (int i = 1 ; i < argc ; i ++ )
@@ -46,9 +51,22 @@ int parse_command_line(int argc, char** argv)
46
51
switch (argv [i ][1 ])
47
52
{
48
53
case 'c' :
49
- case 'C' :
50
54
{
51
- operation |= OP_CUSTOM ;
55
+ if (i + 1 >= argc )
56
+ {
57
+ __usage_error ("No format specified" );
58
+ }
59
+ else
60
+ {
61
+ operation |= OP_CUSTOM ;
62
+ strncpy (datetime , argv [i + 1 ], sizeof ("YYYYMMDDHHMMSS" ));
63
+ }
64
+
65
+ break ;
66
+ }
67
+ case 's' :
68
+ {
69
+ operation |= OP_SILENT ;
52
70
break ;
53
71
}
54
72
case '-' :
@@ -60,20 +78,83 @@ int parse_command_line(int argc, char** argv)
60
78
}
61
79
else if (!strncmp ("custom" , buffer , sizeof ("custom" )))
62
80
{
63
- operation |= OP_CUSTOM ;
81
+ if (i + 1 >= argc )
82
+ {
83
+ __usage_error ("No format specified" );
84
+ }
85
+ else
86
+ {
87
+ operation |= OP_CUSTOM ;
88
+ strncpy (datetime , argv [i + 1 ], sizeof ("YYYYMMDDHHMMSS" ));
89
+ }
64
90
}
65
91
else
66
92
{
67
- __usage_error (argv [ 0 ], "unknown option supplied" );
93
+ __usage_error ("unknown option supplied" );
68
94
}
69
95
break ;
70
96
}
97
+ default :
98
+ {
99
+ __usage_error ("unknown option supplied" );
100
+ }
71
101
}
72
102
}
73
103
}
74
104
}
75
105
76
- return operation ;
106
+ return datetime ;
107
+ }
108
+
109
+ void SetDefaultTime (LPSYSTEMTIME pSystemTime )
110
+ {
111
+ GetLocalTime (pSystemTime );
112
+ pSystemTime -> wYear = 1999 ;
113
+ pSystemTime -> wMonth = 12 ;
114
+ pSystemTime -> wDay = 31 ;
115
+ pSystemTime -> wDayOfWeek = 5 ;
116
+ pSystemTime -> wHour = 0 ;
117
+ pSystemTime -> wMinute = 0 ;
118
+ pSystemTime -> wSecond = 0 ;
119
+ pSystemTime -> wMilliseconds = 0 ;
120
+ }
121
+
122
+ void SetCustomTime (LPSYSTEMTIME pSystemTime , LPCSTR fmt )
123
+ {
124
+ if (strlen (fmt ) != 14 )
125
+ {
126
+ __usage_error ("Invalid datetime format" );
127
+ }
128
+
129
+ for (int i = 0 ; i < 14 ; i ++ )
130
+ {
131
+ if ((fmt [i ] < '0' ) || (fmt [i ] > '9' ))
132
+ {
133
+ __usage_error ("Invalid character detected" );
134
+ }
135
+ }
136
+
137
+ char buffer_wYear [5 ] = { 0 };
138
+ char buffer_wMonth [3 ] = { 0 };
139
+ char buffer_wDay [3 ] = { 0 };
140
+ char buffer_wHour [3 ] = { 0 };
141
+ char buffer_wMinute [3 ] = { 0 };
142
+ char buffer_wSecond [3 ] = { 0 };
143
+
144
+ memcpy (buffer_wYear , fmt + 0 , sizeof (buffer_wYear )- 1 );
145
+ memcpy (buffer_wMonth , fmt + 4 , sizeof (buffer_wMonth )- 1 );
146
+ memcpy (buffer_wDay , fmt + 6 , sizeof (buffer_wDay )- 1 );
147
+ memcpy (buffer_wHour , fmt + 8 , sizeof (buffer_wHour )- 1 );
148
+ memcpy (buffer_wMinute , fmt + 10 , sizeof (buffer_wMinute )- 1 );
149
+ memcpy (buffer_wSecond , fmt + 12 , sizeof (buffer_wSecond )- 1 );
150
+
151
+ sscanf (buffer_wYear , "%hu" , & (pSystemTime -> wYear ));
152
+ sscanf (buffer_wMonth , "%hu" , & (pSystemTime -> wMonth ));
153
+ sscanf (buffer_wDay , "%hu" , & (pSystemTime -> wDay ));
154
+ sscanf (buffer_wHour , "%hu" , & (pSystemTime -> wHour ));
155
+ sscanf (buffer_wMinute , "%hu" , & (pSystemTime -> wMinute ));
156
+ sscanf (buffer_wSecond , "%hu" , & (pSystemTime -> wSecond ));
157
+ sscanf (buffer_wYear , "%hu" , & (pSystemTime -> wYear ));
77
158
}
78
159
79
160
int main (int argc , char * * argv )
@@ -83,10 +164,10 @@ int main(int argc, char** argv)
83
164
SYSTEMTIME system_time ;
84
165
FILETIME file_time ;
85
166
LPCSTR file_name ;
86
- LPCSTR datetime ;
87
- BOOL operation ;
167
+ LPSTR datetime ;
88
168
89
- operation = parse_command_line (argc , argv );
169
+ argv_0 = argv [0 ];
170
+ datetime = parse_command_line (argc , argv );
90
171
91
172
for (int i = 1 ; i < argc ; i ++ )
92
173
{
@@ -101,16 +182,14 @@ int main(int argc, char** argv)
101
182
fclose (fp );
102
183
}
103
184
104
- GetLocalTime (& system_time );
105
-
106
- system_time .wYear = 1999 ;
107
- system_time .wMonth = 12 ;
108
- system_time .wDay = 31 ;
109
- system_time .wDayOfWeek = 5 ;
110
- system_time .wMilliseconds = 0 ;
111
- system_time .wMinute = 0 ;
112
- system_time .wHour = 0 ;
113
- system_time .wSecond = 0 ;
185
+ if (operation & 1 )
186
+ {
187
+ SetCustomTime (& system_time , datetime );
188
+ }
189
+ else
190
+ {
191
+ SetDefaultTime (& system_time );
192
+ }
114
193
115
194
SystemTimeToFileTime (& system_time , & file_time );
116
195
hFile = CreateFile (file_name , // File name
@@ -125,12 +204,12 @@ int main(int argc, char** argv)
125
204
CloseHandle (hFile );
126
205
127
206
datetime = datetime ? datetime : "default" ;
128
- if (! (operation & OP_SILENT ) )
207
+ if ((operation >> 31 ) & 1 )
129
208
{
130
209
fprintf (stdout , "'%s' successfully processed to date-time: %s" , argv [i ], datetime );
131
210
}
132
211
}
133
-
134
212
213
+ free (datetime );
135
214
return 0 ;
136
215
}
0 commit comments