forked from amdf/NativeShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.c
398 lines (346 loc) · 9.72 KB
/
file.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Copyright (c) 2011 amdf.
THIS CODE AND INFORMATION IS PROVIDED UNDER THE LESSER GNU PUBLIC LICENSE.
PLEASE READ THE FILE "COPYING" IN THE TOP LEVEL DIRECTORY.
Module Name:
file.c
Abstract:
The Native Command Line Interface (NCLI) is the command shell for the
TinyKRNL OS.
This module implements commands for dealing with files and directories.
Environment:
Native
Revision History:
Alex Ionescu - Started Implementation - 23-Mar-06
amdf - changed RtlCliSetCurrentDirectory - 20-Feb-11
--*/
#include "precomp.h"
/*++
* @name RtlCliGetCurrentDirectory
*
* The RtlCliGetCurrentDirectory routine provides a way to get the current
* directory.
*
* @param CurrentDirectory
* The current directory.
*
* @return ULONG
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
ULONG
RtlCliGetCurrentDirectory(IN OUT PWSTR CurrentDirectory)
{
//
// Get the current directory into our buffer
//
return RtlGetCurrentDirectory_U(MAX_PATH * sizeof(WCHAR),
CurrentDirectory);
}
/*++
* @name RtlCliSetCurrentDirectory
*
* The RtlCliSetCurrentDirectory routine provides a way to change the current
* directory.
*
* @param Directory
* The directory to change to.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliSetCurrentDirectory(PCHAR Directory)
{
WCHAR buf[MAX_PATH];
UNICODE_STRING us;
if (NULL == Directory)
{
return STATUS_UNSUCCESSFUL;
}
// Full path contains at least two symbols, the second is ':'
if (strnlen(Directory, MAX_PATH) >= 2 && Directory[1] == ':')
{
RtlCreateUnicodeStringFromAsciiz(&us, Directory);
RtlSetCurrentDirectory_U(&us);
RtlFreeUnicodeString(&us);
return STATUS_SUCCESS;
}
GetFullPath(Directory, buf, TRUE);
RtlInitUnicodeString(&us, buf);
RtlSetCurrentDirectory_U(&us);
RtlFreeUnicodeString(&us);
return STATUS_SUCCESS;
}
/*++
* @name RtlCliDumpFileInfo
*
* The RtlCliDumpFileInfo routine FILLMEIN
*
* @param DirInfo
* FILLMEIN
*
* @return None.
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
VOID
RtlCliDumpFileInfo(PFILE_BOTH_DIR_INFORMATION DirInfo)
{
PWCHAR Null;
WCHAR Save;
TIME_FIELDS Time;
CHAR SizeString[16];
WCHAR ShortString[12+1];
WCHAR FileString[MAX_PATH+1];
WCHAR FileStringSize[100];
WCHAR ShortStringSize[100];
UINT file_size = 0;
UINT short_size = 0;
//
// The filename isn't null-terminated, and the next structure follows
// right after it. So, we save the next char (which ends up being the
// NextEntryOffset of the next structure), then temporarly clear it so
// that the RtlCliDisplayString can treat it as a null-terminated string
//
Null = (PWCHAR)((PBYTE)DirInfo->FileName + DirInfo->FileNameLength);
Save = *Null;
*Null = 0;
//
// Get the last access time
//
RtlSystemTimeToLocalTime(&DirInfo->CreationTime, &DirInfo->CreationTime);
RtlTimeToTimeFields(&DirInfo->CreationTime, &Time);
//
// Don't display sizes for directories
//
if (!(DirInfo->FileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
sprintf(SizeString, "%d", DirInfo->AllocationSize.LowPart);
}
else
{
sprintf(SizeString, " ", DirInfo->AllocationSize.LowPart);
}
//
// Display this entry
//
// 75 symbols.
file_size = DirInfo->FileNameLength / sizeof(WCHAR);
short_size = DirInfo->ShortNameLength / sizeof(WCHAR);
swprintf(ShortStringSize, L"%d", short_size);
swprintf(FileStringSize, L"%d", file_size);
if (DirInfo->ShortNameLength)
{
memset(ShortString, 0x00, (12+1)*sizeof(WCHAR));
wcsncpy(ShortString, DirInfo->ShortName, short_size);
}
else
{
swprintf(ShortString, L" ");
}
if (DirInfo->FileNameLength)
{
memset(FileString, 0x00, (MAX_PATH+1)*sizeof(WCHAR));
wcsncpy(FileString, DirInfo->FileName, file_size);
} else
{
swprintf(FileString, L" ");
}
RtlCliDisplayString("%02d.%02d.%04d %02d:%02d %s %9s %-28S %12S\n",
Time.Day,
Time.Month,
Time.Year,
Time.Hour,
Time.Minute,
DirInfo->FileAttributes & FILE_ATTRIBUTE_DIRECTORY ?
"<DIR>" : " ",
SizeString,
FileString,
ShortString);
//
// Restore the character that was here before
//
*Null = Save;
}
/*++
* @name RtlCliListDirectory
*
* The RtlCliListDirectory routine lists the current directory contents.
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliListDirectory(VOID)
{
UNICODE_STRING DirectoryString;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE DirectoryHandle;
NTSTATUS Status;
IO_STATUS_BLOCK IoStatusBlock;
BOOLEAN FirstQuery = TRUE;
WCHAR CurrentDirectory[MAX_PATH];
PFILE_BOTH_DIR_INFORMATION DirectoryInfo, Entry;
HANDLE EventHandle;
CHAR i, c;
//
// For now, we only support the current directory (ie: you can't dir e:\
// without CDing into it first
//
RtlCliGetCurrentDirectory(CurrentDirectory);
//
// Convert it to NT Format
//
if (!RtlDosPathNameToNtPathName_U(CurrentDirectory,
&DirectoryString,
NULL,
NULL))
{
//
// Fail
//
return STATUS_UNSUCCESSFUL;
}
//
// Initialize the object attributes
//
RtlCliDisplayString(" Directory of %S\n\n", CurrentDirectory);
InitializeObjectAttributes(&ObjectAttributes,
&DirectoryString,
OBJ_CASE_INSENSITIVE,
NULL,
NULL);
//
// Open the directory
//
Status = ZwCreateFile(&DirectoryHandle,
FILE_LIST_DIRECTORY,
&ObjectAttributes,
&IoStatusBlock,
NULL,
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
FILE_OPEN,
FILE_DIRECTORY_FILE,
NULL,
0);
if (!NT_SUCCESS(Status))
{
return Status;
}
//
// Allocate space for directory entry information
//
DirectoryInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, 4096);
if (!DirectoryInfo)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
//
// Create the event to wait on
//
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
Status = NtCreateEvent(&EventHandle,
EVENT_ALL_ACCESS,
&ObjectAttributes,
SynchronizationEvent,
FALSE);
if (!NT_SUCCESS(Status))
{
return Status;
}
//
// Start loop
//
i = 0;
for (;;)
{
//
// Get the contents of the directory, adding up the size as we go
//
Status = ZwQueryDirectoryFile(DirectoryHandle,
EventHandle,
NULL,
0,
&IoStatusBlock,
DirectoryInfo,
4096,
FileBothDirectoryInformation,
FALSE,
NULL,
FirstQuery);
if (Status == STATUS_PENDING)
{
//
// Wait on the event
//
NtWaitForSingleObject(EventHandle, FALSE, NULL);
Status = IoStatusBlock.Status;
}
//
// Check for success
//
if (!NT_SUCCESS(Status))
{
//
// Nothing left to enumerate. Close handles and free memory
//
ZwClose(DirectoryHandle);
RtlFreeHeap(RtlGetProcessHeap(), 0, DirectoryInfo);
return STATUS_SUCCESS;
}
//
// Loop every directory
//
Entry = DirectoryInfo;
while(Entry)
{
//
// List the file
//
RtlCliDumpFileInfo(Entry);
if (++i > 20)
{
i = 0;
RtlCliDisplayString("Continue listing (Y/N):");
while (TRUE)
{
c = RtlCliGetChar(hKeyboard);
if (c == 'n' || c == 'N')
{
RtlCliDisplayString("\n");
return STATUS_SUCCESS;
}
if (c == 'y' || c == 'Y')
{
break;
}
}
RtlCliDisplayString("\n");
}
//
// Make sure we still have a file
//
if (!Entry->NextEntryOffset) break;
//
// Move to the next one
//
Entry = (PFILE_BOTH_DIR_INFORMATION)((ULONG_PTR)Entry +
Entry->NextEntryOffset);
}
//
// This isn't the first scan anymore
//
FirstQuery = FALSE;
}
}