Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/cmd/directory.c
8541 views
1
/*
2
* CMD - Wine-compatible command line interface - Directory functions.
3
*
4
* Copyright (C) 1999 D A Pickles
5
* Copyright (C) 2007 J Edmeades
6
*
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
11
*
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
16
*
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
*/
21
22
#define WIN32_LEAN_AND_MEAN
23
24
#include "wcmd.h"
25
#define WIN32_NO_STATUS
26
#include <pathcch.h>
27
#include <winioctl.h>
28
#include <ddk/ntifs.h>
29
#include "wine/debug.h"
30
31
WINE_DEFAULT_DEBUG_CHANNEL(cmd);
32
33
typedef enum _DISPLAYTIME
34
{
35
Creation = 0,
36
Access,
37
Written
38
} DISPLAYTIME;
39
40
typedef enum _DISPLAYORDER
41
{
42
Unspecified = 0,
43
Name,
44
Extension,
45
Size,
46
Date
47
} DISPLAYORDER;
48
49
#define MAX_DATETIME_FORMAT 80
50
51
static WCHAR date_format[MAX_DATETIME_FORMAT * 2];
52
static WCHAR time_format[MAX_DATETIME_FORMAT * 2];
53
static int file_total, dir_total, max_width;
54
static ULONGLONG byte_total;
55
static DISPLAYTIME dirTime;
56
static DISPLAYORDER dirOrder;
57
static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
58
static BOOL paged_mode, recurse, wide, bare, lower, shortname, usernames, separator;
59
static ULONG showattrs, attrsbits;
60
61
/*****************************************************************************
62
* WCMD_filesize64
63
*
64
* Convert a 64-bit number into a WCHARacter string, with commas every three digits.
65
* Result is returned in a static string overwritten with each call.
66
* FIXME: There must be a better algorithm!
67
*/
68
static WCHAR * WCMD_filesize64 (ULONGLONG n) {
69
70
ULONGLONG q;
71
unsigned int r, i;
72
WCHAR *p;
73
static WCHAR buff[32];
74
75
p = buff;
76
i = -3;
77
do {
78
if (separator && ((++i)%3 == 1)) *p++ = ',';
79
q = n / 10;
80
r = n - (q * 10);
81
*p++ = r + '0';
82
*p = '\0';
83
n = q;
84
} while (n != 0);
85
wcsrev(buff);
86
return buff;
87
}
88
89
/*****************************************************************************
90
* WCMD_dir_sort
91
*
92
* Sort based on the /O options supplied on the command line
93
*/
94
static int __cdecl WCMD_dir_sort (const void *a, const void *b)
95
{
96
const WIN32_FIND_DATAW *filea = (const WIN32_FIND_DATAW *)a;
97
const WIN32_FIND_DATAW *fileb = (const WIN32_FIND_DATAW *)b;
98
BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
99
BOOL bDir = fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
100
int result = 0;
101
102
if (orderGroupDirs && dirOrder == Unspecified) {
103
/* Special case: If ordering groups and not sorting by other criteria, "." and ".." always go first. */
104
if (aDir && !lstrcmpW(filea->cFileName, L".")) {
105
result = -1;
106
} else if (bDir && !lstrcmpW(fileb->cFileName, L".")) {
107
result = 1;
108
} else if (aDir && !lstrcmpW(filea->cFileName, L"..")) {
109
result = -1;
110
} else if (bDir && !lstrcmpW(fileb->cFileName, L"..")) {
111
result = 1;
112
}
113
114
if (result) {
115
if (orderGroupDirsReverse) result = -result;
116
return result;
117
}
118
}
119
120
/* If /OG or /O-G supplied, dirs go at the top or bottom, also sorted
121
if requested sort order is by name. */
122
if (orderGroupDirs && (aDir || bDir)) {
123
if (aDir && bDir && dirOrder == Name) {
124
result = lstrcmpiW(filea->cFileName, fileb->cFileName);
125
if (orderReverse) result = -result;
126
} else if (aDir) {
127
result = -1;
128
}
129
else result = 1;
130
if (orderGroupDirsReverse) result = -result;
131
return result;
132
133
/* Order by Name: */
134
} else if (dirOrder == Name) {
135
result = lstrcmpiW(filea->cFileName, fileb->cFileName);
136
137
/* Order by Size: */
138
} else if (dirOrder == Size) {
139
ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
140
ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
141
if( sizea < sizeb ) result = -1;
142
else if( sizea == sizeb ) result = 0;
143
else result = 1;
144
145
/* Order by Date: (Takes into account which date (/T option) */
146
} else if (dirOrder == Date) {
147
148
const FILETIME *ft;
149
ULONG64 timea, timeb;
150
151
if (dirTime == Written) {
152
ft = &filea->ftLastWriteTime;
153
timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
154
ft = &fileb->ftLastWriteTime;
155
timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
156
} else if (dirTime == Access) {
157
ft = &filea->ftLastAccessTime;
158
timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
159
ft = &fileb->ftLastAccessTime;
160
timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
161
} else {
162
ft = &filea->ftCreationTime;
163
timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
164
ft = &fileb->ftCreationTime;
165
timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
166
}
167
if( timea < timeb ) result = -1;
168
else if( timea == timeb ) result = 0;
169
else result = 1;
170
171
/* Order by Extension: (Takes into account which date (/T option) */
172
} else if (dirOrder == Extension) {
173
WCHAR drive[10];
174
WCHAR dir[MAX_PATH];
175
WCHAR fname[MAX_PATH];
176
WCHAR extA[MAX_PATH];
177
WCHAR extB[MAX_PATH];
178
179
/* Split into components */
180
_wsplitpath(filea->cFileName, drive, dir, fname, extA);
181
_wsplitpath(fileb->cFileName, drive, dir, fname, extB);
182
result = lstrcmpiW(extA, extB);
183
}
184
185
if (orderReverse) result = -result;
186
return result;
187
}
188
189
/*****************************************************************************
190
* WCMD_getfileowner
191
*/
192
static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) {
193
194
ULONG sizeNeeded = 0;
195
DWORD rc;
196
WCHAR name[MAXSTRING];
197
WCHAR domain[MAXSTRING];
198
199
/* In case of error, return empty string */
200
*owner = 0x00;
201
202
/* Find out how much space we need for the owner security descriptor */
203
GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
204
rc = GetLastError();
205
206
if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
207
208
LPBYTE secBuffer;
209
PSID pSID = NULL;
210
BOOL defaulted = FALSE;
211
ULONG nameLen = MAXSTRING;
212
ULONG domainLen = MAXSTRING;
213
SID_NAME_USE nameuse;
214
215
secBuffer = xalloc(sizeNeeded * sizeof(BYTE));
216
217
/* Get the owners security descriptor */
218
if(!GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, secBuffer,
219
sizeNeeded, &sizeNeeded)) {
220
free(secBuffer);
221
return;
222
}
223
224
/* Get the SID from the SD */
225
if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
226
free(secBuffer);
227
return;
228
}
229
230
/* Convert to a username */
231
if (LookupAccountSidW(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
232
swprintf(owner, ownerlen, L"%s%c%s", domain, '\\', name);
233
}
234
free(secBuffer);
235
}
236
return;
237
}
238
239
static void output_reparse_target(const WCHAR *dir, const WCHAR *filename)
240
{
241
char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
242
REPARSE_DATA_BUFFER *data = (void *)buffer;
243
HANDLE file;
244
WCHAR *path;
245
DWORD size;
246
247
PathAllocCombine(dir, filename, PATHCCH_ALLOW_LONG_PATHS, &path);
248
file = CreateFileW(path, GENERIC_READ, 0, NULL, OPEN_EXISTING,
249
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
250
if (file == INVALID_HANDLE_VALUE)
251
{
252
WARN("failed to open %s, error %lu\n", debugstr_w(path), GetLastError());
253
LocalFree(path);
254
return;
255
}
256
257
if (DeviceIoControl(file, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, sizeof(buffer), &size, NULL))
258
{
259
if (data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
260
{
261
size_t offset = data->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR);
262
WCMD_output(L" [%1]", data->MountPointReparseBuffer.PathBuffer + offset);
263
}
264
else if (data->ReparseTag == IO_REPARSE_TAG_SYMLINK)
265
{
266
size_t offset = data->SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(WCHAR);
267
WCMD_output(L" [%1]", data->SymbolicLinkReparseBuffer.PathBuffer + offset);
268
}
269
}
270
else
271
{
272
WARN("failed to get reparse point from %s, error %lu\n", debugstr_w(path), GetLastError());
273
}
274
275
LocalFree(path);
276
CloseHandle(file);
277
}
278
279
/*****************************************************************************
280
* WCMD_list_directory
281
*
282
* List a single file directory. This function (and those below it) can be called
283
* recursively when the /S switch is used.
284
*/
285
286
static RETURN_CODE WCMD_list_directory (DIRECTORY_STACK *inputparms, int level, DIRECTORY_STACK **outputparms) {
287
288
WCHAR string[1024], datestring[32], timestring[32];
289
WCHAR real_path[MAX_PATH];
290
WIN32_FIND_DATAW *fd;
291
FILETIME ft;
292
SYSTEMTIME st;
293
HANDLE hff;
294
int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
295
int numCols, numRows;
296
int rows, cols;
297
ULARGE_INTEGER byte_count, file_size;
298
DIRECTORY_STACK *parms;
299
int concurrentDirs = 0;
300
BOOL done_header = FALSE;
301
RETURN_CODE return_code = NO_ERROR;
302
303
dir_count = 0;
304
file_count = 0;
305
entry_count = 0;
306
byte_count.QuadPart = 0;
307
widest = 0;
308
cur_width = 0;
309
310
/* Loop merging all the files from consecutive parms which relate to the
311
same directory. Note issuing a directory header with no contents
312
mirrors what windows does */
313
parms = inputparms;
314
fd = xalloc(sizeof(WIN32_FIND_DATAW));
315
while (parms && lstrcmpW(inputparms->dirName, parms->dirName) == 0) {
316
317
if ((lstrlenW(parms->dirName) + lstrlenW(parms->fileName) + 1) > ARRAY_SIZE(real_path)) {
318
WINE_TRACE("Path gets too long: %s %s\n", wine_dbgstr_w(parms->dirName), wine_dbgstr_w(parms->fileName));
319
/* Move to next parm */
320
parms = parms->next;
321
continue;
322
}
323
324
concurrentDirs++;
325
326
/* Work out the full path + filename */
327
lstrcpyW(real_path, parms->dirName);
328
lstrcatW(real_path, parms->fileName);
329
330
/* Load all files into an in memory structure */
331
WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
332
hff = FindFirstFileW(real_path, &fd[entry_count]);
333
if (hff != INVALID_HANDLE_VALUE) {
334
do {
335
/* Skip any which are filtered out by attribute */
336
if ((fd[entry_count].dwFileAttributes & attrsbits) != showattrs) continue;
337
338
entry_count++;
339
340
/* Keep running track of longest filename for wide output */
341
if (wide || orderByCol) {
342
int tmpLen = lstrlenW(fd[entry_count-1].cFileName) + 3;
343
if (fd[entry_count-1].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
344
if (tmpLen > widest) widest = tmpLen;
345
}
346
347
fd = xrealloc(fd, (entry_count + 1) * sizeof(WIN32_FIND_DATAW));
348
} while (FindNextFileW(hff, &fd[entry_count]) != 0);
349
FindClose (hff);
350
}
351
352
/* Work out the actual current directory name without a trailing \ */
353
lstrcpyW(real_path, parms->dirName);
354
real_path[lstrlenW(parms->dirName)-1] = 0x00;
355
356
/* Output the results */
357
if (!bare) {
358
if (level != 0 && (entry_count > 0)) return_code = WCMD_output_asis(L"\r\n");
359
if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
360
WCMD_output (L"Directory of %1\n\n", real_path);
361
done_header = TRUE;
362
}
363
}
364
365
/* Move to next parm */
366
parms = parms->next;
367
}
368
369
/* Handle case where everything is filtered out */
370
if (entry_count > 0) {
371
372
/* Sort the list of files */
373
qsort (fd, entry_count, sizeof(WIN32_FIND_DATAW), WCMD_dir_sort);
374
375
/* Work out the number of columns */
376
WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
377
if (wide || orderByCol) {
378
numCols = max(1, max_width / widest);
379
numRows = entry_count / numCols;
380
if (entry_count % numCols) numRows++;
381
} else {
382
numCols = 1;
383
numRows = entry_count;
384
}
385
WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
386
387
for (rows=0; rows<numRows && return_code == NO_ERROR; rows++) {
388
BOOL addNewLine = TRUE;
389
for (cols=0; cols<numCols; cols++) {
390
WCHAR username[24];
391
392
/* Work out the index of the entry being pointed to */
393
if (orderByCol) {
394
i = (cols * numRows) + rows;
395
if (i >= entry_count) continue;
396
} else {
397
i = (rows * numCols) + cols;
398
if (i >= entry_count) continue;
399
}
400
401
/* /L convers all names to lower case */
402
if (lower) wcslwr( fd[i].cFileName );
403
404
/* /Q gets file ownership information */
405
if (usernames) {
406
lstrcpyW (string, inputparms->dirName);
407
lstrcatW (string, fd[i].cFileName);
408
WCMD_getfileowner(string, username, ARRAY_SIZE(username));
409
}
410
411
if (dirTime == Written) {
412
FileTimeToLocalFileTime (&fd[i].ftLastWriteTime, &ft);
413
} else if (dirTime == Access) {
414
FileTimeToLocalFileTime (&fd[i].ftLastAccessTime, &ft);
415
} else {
416
FileTimeToLocalFileTime (&fd[i].ftCreationTime, &ft);
417
}
418
FileTimeToSystemTime (&ft, &st);
419
GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, date_format,
420
datestring, ARRAY_SIZE(datestring));
421
GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, time_format,
422
timestring, ARRAY_SIZE(timestring));
423
424
if (wide) {
425
426
tmp_width = cur_width;
427
if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
428
WCMD_output (L"[%1]", fd[i].cFileName);
429
dir_count++;
430
tmp_width = tmp_width + lstrlenW(fd[i].cFileName) + 2;
431
} else {
432
WCMD_output (L"%1", fd[i].cFileName);
433
tmp_width = tmp_width + lstrlenW(fd[i].cFileName) ;
434
file_count++;
435
file_size.u.LowPart = fd[i].nFileSizeLow;
436
file_size.u.HighPart = fd[i].nFileSizeHigh;
437
byte_count.QuadPart += file_size.QuadPart;
438
}
439
cur_width = cur_width + widest;
440
441
if ((cur_width + widest) > max_width) {
442
cur_width = 0;
443
} else {
444
WCMD_output(L"%1!*s!", cur_width - tmp_width, L"");
445
}
446
447
} else if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
448
dir_count++;
449
450
if (!bare) {
451
if ((fd[i].dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
452
&& (fd[i].dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT))
453
WCMD_output(L"%1 %2 <JUNCTION> ", datestring, timestring);
454
else if ((fd[i].dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
455
&& (fd[i].dwReserved0 == IO_REPARSE_TAG_SYMLINK))
456
WCMD_output(L"%1 %2 <SYMLINKD> ", datestring, timestring);
457
else
458
WCMD_output(L"%1 %2 <DIR> ", datestring, timestring);
459
if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName);
460
if (usernames) WCMD_output(L"%1!-23s!", username);
461
WCMD_output(L"%1",fd[i].cFileName);
462
if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
463
output_reparse_target(inputparms->dirName, fd[i].cFileName);
464
} else {
465
if (!((lstrcmpW(fd[i].cFileName, L".") == 0) ||
466
(lstrcmpW(fd[i].cFileName, L"..") == 0))) {
467
WCMD_output(L"%1%2", recurse ? inputparms->dirName : L"", fd[i].cFileName);
468
} else {
469
addNewLine = FALSE;
470
}
471
}
472
}
473
else {
474
file_count++;
475
file_size.u.LowPart = fd[i].nFileSizeLow;
476
file_size.u.HighPart = fd[i].nFileSizeHigh;
477
byte_count.QuadPart += file_size.QuadPart;
478
if (!bare) {
479
if ((fd[i].dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
480
&& (fd[i].dwReserved0 == IO_REPARSE_TAG_SYMLINK))
481
WCMD_output(L"%1 %2 <SYMLINK> ", datestring, timestring);
482
else
483
WCMD_output(L"%1 %2 %3!14s! ", datestring, timestring,
484
WCMD_filesize64(file_size.QuadPart));
485
if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName);
486
if (usernames) WCMD_output(L"%1!-23s!", username);
487
WCMD_output(L"%1",fd[i].cFileName);
488
if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
489
output_reparse_target(inputparms->dirName, fd[i].cFileName);
490
} else {
491
WCMD_output(L"%1%2", recurse ? inputparms->dirName : L"", fd[i].cFileName);
492
}
493
}
494
}
495
if (addNewLine) return_code = WCMD_output_asis(L"\r\n");
496
cur_width = 0;
497
498
/* Allow command to be aborted if user presses Ctrl-C.
499
* Don't overwrite any existing error code.
500
*/
501
if (return_code == NO_ERROR) {
502
return_code = WCMD_ctrlc_status();
503
}
504
}
505
506
if (!bare && return_code == NO_ERROR) {
507
if (file_count == 1) {
508
WCMD_output (L" 1 file %1!25s! bytes\n", WCMD_filesize64 (byte_count.QuadPart));
509
}
510
else {
511
WCMD_output (L"%1!8d! files %2!24s! bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
512
}
513
}
514
byte_total = byte_total + byte_count.QuadPart;
515
file_total = file_total + file_count;
516
dir_total = dir_total + dir_count;
517
518
if (!bare && !recurse && return_code == NO_ERROR) {
519
if (dir_count == 1) {
520
WCMD_output (L"%1!8d! directory ", 1);
521
} else {
522
WCMD_output (L"%1!8d! directories", dir_count);
523
}
524
}
525
}
526
free(fd);
527
528
/* When recursing, look in all subdirectories for matches */
529
if (recurse && return_code == NO_ERROR) {
530
DIRECTORY_STACK *dirStack = NULL;
531
DIRECTORY_STACK *lastEntry = NULL;
532
WIN32_FIND_DATAW finddata;
533
534
/* Build path to search */
535
lstrcpyW(string, inputparms->dirName);
536
lstrcatW(string, L"*");
537
538
WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
539
hff = FindFirstFileW(string, &finddata);
540
if (hff != INVALID_HANDLE_VALUE) {
541
do {
542
if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
543
(lstrcmpW(finddata.cFileName, L"..") != 0) &&
544
(lstrcmpW(finddata.cFileName, L".") != 0)) {
545
546
DIRECTORY_STACK *thisDir;
547
int dirsToCopy = concurrentDirs;
548
549
/* Loop creating list of subdirs for all concurrent entries */
550
parms = inputparms;
551
while (dirsToCopy > 0) {
552
dirsToCopy--;
553
554
/* Work out search parameter in sub dir */
555
lstrcpyW (string, inputparms->dirName);
556
lstrcatW (string, finddata.cFileName);
557
lstrcatW(string, L"\\");
558
WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
559
560
/* Allocate memory, add to list */
561
thisDir = xalloc(sizeof(DIRECTORY_STACK));
562
if (dirStack == NULL) dirStack = thisDir;
563
if (lastEntry != NULL) lastEntry->next = thisDir;
564
lastEntry = thisDir;
565
thisDir->next = NULL;
566
thisDir->dirName = xstrdupW(string);
567
thisDir->fileName = xstrdupW(parms->fileName);
568
parms = parms->next;
569
}
570
}
571
} while (FindNextFileW(hff, &finddata) != 0);
572
FindClose (hff);
573
574
while (dirStack != NULL && return_code == NO_ERROR) {
575
DIRECTORY_STACK *thisDir = dirStack;
576
return_code = WCMD_list_directory (thisDir, 1, &dirStack);
577
if (return_code != NO_ERROR) dirStack = NULL;
578
while (thisDir != dirStack) {
579
DIRECTORY_STACK *tempDir = thisDir->next;
580
free(thisDir->dirName);
581
free(thisDir->fileName);
582
free(thisDir);
583
thisDir = tempDir;
584
}
585
}
586
}
587
}
588
589
/* Handle case where everything is filtered out */
590
if ((file_total + dir_total == 0) && (level == 0)) {
591
return_code = ERROR_FILE_NOT_FOUND;
592
SetLastError (return_code);
593
WCMD_print_error ();
594
}
595
596
*outputparms = parms;
597
return return_code;
598
}
599
600
/*****************************************************************************
601
* WCMD_dir_trailer
602
*
603
* Print out the trailer for the supplied path
604
*/
605
static void WCMD_dir_trailer(const WCHAR *path) {
606
ULARGE_INTEGER freebytes;
607
BOOL status;
608
609
status = GetDiskFreeSpaceExW(path, NULL, NULL, &freebytes);
610
WINE_TRACE("Writing trailer for '%s' gave %d(%ld)\n", wine_dbgstr_w(path),
611
status, GetLastError());
612
613
if (!bare) {
614
if (recurse) {
615
WCMD_output (L"\n Total files listed:\n%1!8d! files%2!25s! bytes\n", file_total, WCMD_filesize64 (byte_total));
616
WCMD_output (L"%1!8d! directories %2!18s! bytes free\n\n", dir_total, WCMD_filesize64 (freebytes.QuadPart));
617
} else {
618
WCMD_output (L" %1!18s! bytes free\n\n", WCMD_filesize64 (freebytes.QuadPart));
619
}
620
}
621
}
622
623
/* Get the length of a date/time formatting pattern */
624
/* copied from dlls/kernelbase/locale.c */
625
static int get_pattern_len( const WCHAR *pattern, const WCHAR *accept )
626
{
627
int i;
628
629
if (*pattern == '\'')
630
{
631
for (i = 1; pattern[i]; i++)
632
{
633
if (pattern[i] != '\'') continue;
634
if (pattern[++i] != '\'') return i;
635
}
636
return i;
637
}
638
if (!wcschr( accept, *pattern )) return 1;
639
for (i = 1; pattern[i]; i++) if (pattern[i] != pattern[0]) break;
640
return i;
641
}
642
643
/* Initialize date format to use abbreviated one with leading zeros */
644
static void init_date_format(void)
645
{
646
WCHAR sys_format[MAX_DATETIME_FORMAT];
647
int src_pat_len, dst_pat_len;
648
const WCHAR *src;
649
WCHAR *dst = date_format;
650
651
GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sys_format, ARRAY_SIZE(sys_format));
652
653
for (src = sys_format; *src; src += src_pat_len, dst += dst_pat_len) {
654
src_pat_len = dst_pat_len = get_pattern_len(src, L"yMd");
655
656
switch (*src)
657
{
658
case '\'':
659
wmemcpy(dst, src, src_pat_len);
660
break;
661
662
case 'd':
663
case 'M':
664
if (src_pat_len == 4) /* full name */
665
dst_pat_len--; /* -> use abbreviated one */
666
/* fallthrough */
667
case 'y':
668
if (src_pat_len == 1) /* without leading zeros */
669
dst_pat_len++; /* -> with leading zeros */
670
wmemset(dst, *src, dst_pat_len);
671
break;
672
673
default:
674
*dst = *src;
675
break;
676
}
677
}
678
*dst = '\0';
679
680
TRACE("date format: %s\n", wine_dbgstr_w(date_format));
681
}
682
683
/* Initialize time format to use leading zeros */
684
static void init_time_format(void)
685
{
686
WCHAR sys_format[MAX_DATETIME_FORMAT];
687
int src_pat_len, dst_pat_len;
688
const WCHAR *src;
689
WCHAR *dst = time_format;
690
691
GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, sys_format, ARRAY_SIZE(sys_format));
692
693
for (src = sys_format; *src; src += src_pat_len, dst += dst_pat_len) {
694
src_pat_len = dst_pat_len = get_pattern_len(src, L"Hhmst");
695
696
switch (*src)
697
{
698
case '\'':
699
wmemcpy(dst, src, src_pat_len);
700
break;
701
702
case 'H':
703
case 'h':
704
case 'm':
705
case 's':
706
if (src_pat_len == 1) /* without leading zeros */
707
dst_pat_len++; /* -> with leading zeros */
708
/* fallthrough */
709
case 't':
710
wmemset(dst, *src, dst_pat_len);
711
break;
712
713
default:
714
*dst = *src;
715
break;
716
}
717
}
718
*dst = '\0';
719
720
/* seconds portion will be dropped by TIME_NOSECONDS */
721
TRACE("time format: %s\n", wine_dbgstr_w(time_format));
722
}
723
724
/*****************************************************************************
725
* WCMD_directory
726
*
727
* List a file directory.
728
*
729
*/
730
731
RETURN_CODE WCMD_directory(WCHAR *args)
732
{
733
WCHAR path[MAX_PATH], cwd[MAX_PATH];
734
DWORD status;
735
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
736
WCHAR *p;
737
WCHAR string[MAXSTRING];
738
int argno = 0;
739
WCHAR *argN = args;
740
WCHAR lastDrive;
741
BOOL trailerReqd = FALSE;
742
DIRECTORY_STACK *fullParms = NULL;
743
DIRECTORY_STACK *prevEntry = NULL;
744
DIRECTORY_STACK *thisEntry = NULL;
745
WCHAR drive[10];
746
WCHAR dir[MAX_PATH];
747
WCHAR fname[MAX_PATH];
748
WCHAR ext[MAX_PATH];
749
unsigned num_empty = 0, num_with_data = 0;
750
RETURN_CODE return_code = NO_ERROR;
751
752
/* Prefill quals with (uppercased) DIRCMD env var */
753
if (GetEnvironmentVariableW(L"DIRCMD", string, ARRAY_SIZE(string))) {
754
wcsupr( string );
755
lstrcatW(string,quals);
756
lstrcpyW(quals, string);
757
}
758
759
byte_total = 0;
760
file_total = dir_total = 0;
761
762
/* Initialize all flags to their defaults as if no DIRCMD or quals */
763
paged_mode = FALSE;
764
recurse = FALSE;
765
wide = FALSE;
766
bare = FALSE;
767
lower = FALSE;
768
shortname = FALSE;
769
usernames = FALSE;
770
orderByCol = FALSE;
771
separator = TRUE;
772
dirTime = Written;
773
dirOrder = Unspecified;
774
orderReverse = FALSE;
775
orderGroupDirs = FALSE;
776
orderGroupDirsReverse = FALSE;
777
showattrs = 0;
778
attrsbits = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
779
780
/* Handle args - Loop through so right most is the effective one */
781
/* Note: /- appears to be a negate rather than an off, eg. dir
782
/-W is wide, or dir /w /-w /-w is also wide */
783
p = quals;
784
while (*p && (*p=='/' || *p==' ')) {
785
BOOL negate = FALSE;
786
if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
787
788
if (*p=='-') {
789
negate = TRUE;
790
p++;
791
}
792
793
WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
794
switch (*p) {
795
case 'P': if (negate) paged_mode = !paged_mode;
796
else paged_mode = TRUE;
797
break;
798
case 'S': if (negate) recurse = !recurse;
799
else recurse = TRUE;
800
break;
801
case 'W': if (negate) wide = !wide;
802
else wide = TRUE;
803
break;
804
case 'B': if (negate) bare = !bare;
805
else bare = TRUE;
806
break;
807
case 'L': if (negate) lower = !lower;
808
else lower = TRUE;
809
break;
810
case 'X': if (negate) shortname = !shortname;
811
else shortname = TRUE;
812
break;
813
case 'Q': if (negate) usernames = !usernames;
814
else usernames = TRUE;
815
break;
816
case 'D': if (negate) orderByCol = !orderByCol;
817
else orderByCol = TRUE;
818
break;
819
case 'C': if (negate) separator = !separator;
820
else separator = TRUE;
821
break;
822
case 'T': p = p + 1;
823
if (*p==':') p++; /* Skip optional : */
824
825
if (*p == 'A') dirTime = Access;
826
else if (*p == 'C') dirTime = Creation;
827
else if (*p == 'W') dirTime = Written;
828
829
/* Support /T and /T: with no parms, default to written */
830
else if (*p == 0x00 || *p == '/') {
831
dirTime = Written;
832
p = p - 1; /* So when step on, move to '/' */
833
} else {
834
SetLastError(return_code = ERROR_INVALID_PARAMETER);
835
WCMD_print_error();
836
goto exit;
837
}
838
break;
839
case 'O': /* Reset order state for each occurrence of /O, i.e. if DIRCMD contains /O and user
840
also specified /O on the command line. */
841
dirOrder = Unspecified;
842
orderGroupDirs = FALSE;
843
orderReverse = FALSE;
844
orderGroupDirsReverse = FALSE;
845
p = p + 1;
846
if (*p==':') p++; /* Skip optional : */
847
while (*p && *p != '/') {
848
WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
849
/* Options N,E,S,D are mutually-exclusive, first encountered takes precedence. */
850
switch (*p) {
851
case 'N': if (dirOrder == Unspecified) dirOrder = Name; break;
852
case 'E': if (dirOrder == Unspecified) dirOrder = Extension; break;
853
case 'S': if (dirOrder == Unspecified) dirOrder = Size; break;
854
case 'D': if (dirOrder == Unspecified) dirOrder = Date; break;
855
case '-': if (dirOrder == Unspecified) {
856
if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
857
else if (*(p+1)=='N'||*(p+1)=='E'||*(p+1)=='S'||*(p+1)=='D') orderReverse = TRUE;
858
}
859
break;
860
case 'G': orderGroupDirs = TRUE; break;
861
default:
862
SetLastError(return_code = ERROR_INVALID_PARAMETER);
863
WCMD_print_error();
864
goto exit;
865
}
866
p++;
867
}
868
/* Handle default case of /O specified by itself, with no specific options.
869
This is equivalent to /O:GN. */
870
if (dirOrder == Unspecified && !orderGroupDirs) {
871
orderGroupDirs = TRUE;
872
orderGroupDirsReverse = FALSE;
873
dirOrder = Name;
874
}
875
p = p - 1; /* So when step on, move to '/' */
876
break;
877
case 'A': p = p + 1;
878
showattrs = 0;
879
attrsbits = 0;
880
if (*p==':') p++; /* Skip optional : */
881
while (*p && *p != '/') {
882
BOOL anegate = FALSE;
883
ULONG mask;
884
885
/* Note /A: - options are 'offs' not toggles */
886
if (*p=='-') {
887
anegate = TRUE;
888
p++;
889
}
890
891
WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
892
switch (*p) {
893
case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
894
case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
895
case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
896
case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
897
case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
898
default:
899
SetLastError(return_code = ERROR_INVALID_PARAMETER);
900
WCMD_print_error();
901
goto exit;
902
}
903
904
/* Keep running list of bits we care about */
905
attrsbits |= mask;
906
907
/* Mask shows what MUST be in the bits we care about */
908
if (anegate) showattrs = showattrs & ~mask;
909
else showattrs |= mask;
910
911
p++;
912
}
913
p = p - 1; /* So when step on, move to '/' */
914
WINE_TRACE("Result: showattrs %lx, bits %lx\n", showattrs, attrsbits);
915
break;
916
default:
917
SetLastError(return_code = ERROR_INVALID_PARAMETER);
918
WCMD_print_error();
919
goto exit;
920
}
921
p = p + 1;
922
}
923
924
/* Handle conflicting args and initialization */
925
if (bare || shortname) wide = FALSE;
926
if (bare) shortname = FALSE;
927
if (wide) usernames = FALSE;
928
if (orderByCol) wide = TRUE;
929
930
if (wide) {
931
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
932
max_width = consoleInfo.dwSize.X;
933
else
934
max_width = 80;
935
}
936
if (paged_mode) {
937
WCMD_enter_paged_mode(NULL);
938
}
939
940
init_date_format();
941
init_time_format();
942
943
argno = 0;
944
argN = args;
945
GetCurrentDirectoryW(MAX_PATH, cwd);
946
lstrcatW(cwd, L"\\");
947
948
/* Loop through all args, calculating full effective directory */
949
fullParms = NULL;
950
prevEntry = NULL;
951
while (argN) {
952
WCHAR fullname[MAXSTRING];
953
WCHAR *thisArg = WCMD_parameter(args, argno++, &argN, FALSE, FALSE);
954
if (argN && argN[0] != '/') {
955
956
WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
957
if (thisArg[1] == ':' && thisArg[2] == '\\') {
958
lstrcpyW(fullname, thisArg);
959
} else if (thisArg[1] == ':' && thisArg[2] != '\\') {
960
WCHAR envvar[4];
961
wsprintfW(envvar, L"=%c:", thisArg[0]);
962
if (!GetEnvironmentVariableW(envvar, fullname, MAX_PATH)) {
963
wsprintfW(fullname, L"%c:", thisArg[0]);
964
}
965
lstrcatW(fullname, L"\\");
966
lstrcatW(fullname, &thisArg[2]);
967
} else if (thisArg[0] == '\\') {
968
memcpy(fullname, cwd, 2 * sizeof(WCHAR));
969
lstrcpyW(fullname+2, thisArg);
970
} else {
971
lstrcpyW(fullname, cwd);
972
lstrcatW(fullname, thisArg);
973
}
974
WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
975
976
if (!WCMD_get_fullpath(fullname, ARRAY_SIZE(path), path, NULL)) continue;
977
978
/*
979
* If the path supplied does not include a wildcard, and the endpoint of the
980
* path references a directory, we need to list the *contents* of that
981
* directory not the directory file itself.
982
*/
983
if ((wcschr(path, '*') == NULL) && (wcschr(path, '%') == NULL)) {
984
status = GetFileAttributesW(path);
985
if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
986
if (!ends_with_backslash(path)) lstrcatW(path, L"\\");
987
lstrcatW(path, L"*");
988
}
989
} else {
990
/* Special case wildcard search with no extension (ie parameters ending in '.') as
991
GetFullPathName strips off the additional '.' */
992
if (fullname[0] && fullname[lstrlenW(fullname)-1] == '.') lstrcatW(path, L".");
993
}
994
995
WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
996
thisEntry = xalloc(sizeof(DIRECTORY_STACK));
997
if (fullParms == NULL) fullParms = thisEntry;
998
if (prevEntry != NULL) prevEntry->next = thisEntry;
999
prevEntry = thisEntry;
1000
thisEntry->next = NULL;
1001
1002
/* Split into components */
1003
_wsplitpath(path, drive, dir, fname, ext);
1004
WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
1005
wine_dbgstr_w(drive), wine_dbgstr_w(dir),
1006
wine_dbgstr_w(fname), wine_dbgstr_w(ext));
1007
1008
thisEntry->dirName = xalloc(sizeof(WCHAR) * (wcslen(drive) + wcslen(dir) + 1));
1009
lstrcpyW(thisEntry->dirName, drive);
1010
lstrcatW(thisEntry->dirName, dir);
1011
1012
thisEntry->fileName = xalloc(sizeof(WCHAR) * (wcslen(fname) + wcslen(ext) + 1));
1013
lstrcpyW(thisEntry->fileName, fname);
1014
lstrcatW(thisEntry->fileName, ext);
1015
1016
}
1017
}
1018
1019
/* If just 'dir' entered, a '*' parameter is assumed */
1020
if (fullParms == NULL) {
1021
WINE_TRACE("Inserting default '*'\n");
1022
fullParms = xalloc(sizeof(DIRECTORY_STACK));
1023
fullParms->next = NULL;
1024
fullParms->dirName = xstrdupW(cwd);
1025
fullParms->fileName = xstrdupW(L"*");
1026
}
1027
1028
lastDrive = '?';
1029
prevEntry = NULL;
1030
thisEntry = fullParms;
1031
trailerReqd = FALSE;
1032
1033
while (thisEntry != NULL && return_code == NO_ERROR) {
1034
1035
/* Output disk free (trailer) and volume information (header) if the drive
1036
letter changes */
1037
if (lastDrive != towupper(thisEntry->dirName[0])) {
1038
1039
/* Trailer Information */
1040
if (lastDrive != '?') {
1041
trailerReqd = FALSE;
1042
if (return_code == NO_ERROR)
1043
WCMD_dir_trailer(prevEntry->dirName);
1044
byte_total = file_total = dir_total = 0;
1045
}
1046
1047
lastDrive = towupper(thisEntry->dirName[0]);
1048
1049
if (!bare) {
1050
WCHAR drive[4];
1051
WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
1052
drive[0] = thisEntry->dirName[0];
1053
drive[1] = thisEntry->dirName[1];
1054
drive[2] = L'\\';
1055
drive[3] = L'\0';
1056
trailerReqd = TRUE;
1057
if (!WCMD_print_volume_information(drive)) {
1058
return_code = ERROR_INVALID_PARAMETER;
1059
goto exit;
1060
}
1061
}
1062
} else {
1063
if (!bare) WCMD_output_asis (L"\n\n");
1064
}
1065
1066
prevEntry = thisEntry;
1067
return_code = WCMD_list_directory (thisEntry, 0, &thisEntry);
1068
if (return_code == ERROR_FILE_NOT_FOUND)
1069
num_empty++;
1070
else
1071
num_with_data++;
1072
}
1073
1074
/* Trailer Information */
1075
if (trailerReqd && return_code == NO_ERROR) {
1076
WCMD_dir_trailer(prevEntry->dirName);
1077
}
1078
1079
exit:
1080
if (return_code == STATUS_CONTROL_C_EXIT)
1081
errorlevel = ERROR_INVALID_FUNCTION;
1082
else if (return_code != NO_ERROR || (num_empty && !num_with_data))
1083
return_code = errorlevel = ERROR_INVALID_FUNCTION;
1084
else
1085
errorlevel = NO_ERROR;
1086
1087
if (paged_mode) WCMD_leave_paged_mode();
1088
1089
/* Free storage allocated for parms */
1090
while (fullParms != NULL) {
1091
prevEntry = fullParms;
1092
fullParms = prevEntry->next;
1093
free(prevEntry->dirName);
1094
free(prevEntry->fileName);
1095
free(prevEntry);
1096
}
1097
1098
return return_code;
1099
}
1100
1101