Repository navigation
Expand file tree
/
Copy pathdir.c
More file actions
1998 lines (1789 loc) · 67.5 KB
/
Copy pathdir.c
File metadata and controls
1998 lines (1789 loc) · 67.5 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DIR.C - dir internal command.
*
*
* History:
*
* 01/29/97 (Tim Norman)
* started.
*
* 06/13/97 (Tim Norman)
* Fixed code.
*
* 07/12/97 (Tim Norman)
* Fixed bug that caused the root directory to be unlistable
*
* 07/12/97 (Marc Desrochers)
* Changed to use maxx, maxy instead of findxy()
*
* 06/08/98 (Rob Lake)
* Added compatibility for /w in dir
*
* 06/09/98 (Rob Lake)
* Compatibility for dir/s started
* Tested that program finds directories off root fine
*
* 06/10/98 (Rob Lake)
* do_recurse saves the cwd and also stores it in Root
* build_tree adds the cwd to the beginning of its' entries
* Program runs fine, added print_tree -- works fine.. as EXE,
* program won't work properly as COM.
*
* 06/11/98 (Rob Lake)
* Found problem that caused COM not to work
*
* 06/12/98 (Rob Lake)
* debugged...
* added free mem routine
*
* 06/13/98 (Rob Lake)
* debugged the free mem routine
* debugged whole thing some more
* Notes:
* ReadDir stores Root name and _Read_Dir does the hard work
* PrintDir prints Root and _Print_Dir does the hard work
* KillDir kills Root _after_ _Kill_Dir does the hard work
* Integrated program into DIR.C(this file) and made some same
* changes throughout
*
* 06/14/98 (Rob Lake)
* Cleaned up code a bit, added comments
*
* 06/16/98 (Rob Lake)
* Added error checking to my previously added routines
*
* 06/17/98 (Rob Lake)
* Rewrote recursive functions, again! Most other recursive
* functions are now obsolete -- ReadDir, PrintDir, _Print_Dir,
* KillDir and _Kill_Dir. do_recurse does what PrintDir did
* and _Read_Dir did what it did before along with what _Print_Dir
* did. Makes /s a lot faster!
* Reports 2 more files/dirs that MS-DOS actually reports
* when used in root directory(is this because dir defaults
* to look for read only files?)
* Added support for /b, /a and /l
* Made error message similar to DOS error messages
* Added help screen
*
* 06/20/98 (Rob Lake)
* Added check for /-(switch) to turn off previously defined
* switches.
* Added ability to check for DIRCMD in environment and
* process it
*
* 06/21/98 (Rob Lake)
* Fixed up /B
* Now can dir *.ext/X, no spaces!
*
* 06/29/98 (Rob Lake)
* error message now found in command.h
*
* 07/08/1998 (John P. Price)
* removed extra returns; closer to MSDOS
* fixed wide display so that an extra return is not displayed
* when there is five filenames in the last line.
*
* 07/12/98 (Rob Lake)
* Changed error messages
*
* 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
* added config.h include
*
*
* 04-Dec-1998 (Eric Kohl)
* Converted source code to Win32, except recursive dir ("dir /s").
*
* 10-Dec-1998 (Eric Kohl)
* Fixed recursive dir ("dir /s").
*
* 14-Dec-1998 (Eric Kohl)
* Converted to Win32 directory functions and
* fixed some output bugs. There are still some more ;)
*
* 10-Jan-1999 (Eric Kohl)
* Added "/N" and "/4" options, "/O" is a dummy.
* Added locale support.
*
* 20-Jan-1999 (Eric Kohl)
* Redirection safe!
*
* 01-Mar-1999 (Eric Kohl)
* Replaced all runtime io functions by their Win32 counterparts.
*
* 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
* dir /s now works in deeper trees
*
* 28-Jan-2004 (Michael Fritscher <michael@fritscher.net>)
* Fix for /p, so it is working under Windows in GUI-mode, too.
*
* 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
* Fix /w to print long names.
*
* 27-Feb-2005 (Konstantinos Paliouras <squarious@gmail.com>)
* Implemented all the switches that were missing, and made
* the ROS dir very similar to windows dir. Major part of
* the code is rewritten. /p is removed, to be rewritten in
* the main cmd code.
*
* 1-Jul-2004 (Brandon Turner <turnerb7@msu.edu>)
* Added /p back in using ConOutPrintfPaging
*
* 3-Feb-2007 (Paolo Devoti devotip at gmail)
* Removed variables formerly in use to handle pagination
* Pagination belongs to ConOutPrintfPaging
* Removed already commented out code of old pagination
*
* 25-Aug-2015 (Pierre Schweitzer)
* Implemented /R switch
*
* 6-Aug-2018 (Hermes Belusca-Maito and Katayama Hirofumi MZ)
* Fix handling of patterns containing trailing dots.
*/
#include "precomp.h"
#include <cjkcode.h>
#ifdef INCLUDE_CMD_DIR
/* Time Field enumeration */
enum ETimeField
{
TF_CREATIONDATE = 0,
TF_MODIFIEDDATE = 1,
TF_LASTACCESSEDDATE = 2
};
/* Ordered by enumeration */
enum EOrderBy
{
ORDER_NAME = 0,
ORDER_SIZE = 1,
ORDER_DIRECTORY = 2,
ORDER_EXTENSION = 3,
ORDER_TIME = 4
};
/* The struct for holding the switches */
typedef struct _DirSwitchesFlags
{
BOOL bBareFormat; /* Bare Format */
BOOL bTSeparator; /* Thousands separator */
BOOL bWideList; /* Wide list format */
BOOL bWideListColSort; /* Wide list format but sorted by column */
BOOL bLowerCase; /* Uses lower case */
BOOL bNewLongList; /* New long list */
BOOL bPause; /* Pause per page */
BOOL bUser; /* Displays the owner of file */
BOOL bRecursive; /* Displays files in specified directory and all sub */
BOOL bShortName; /* Displays the sort name of files if exist */
BOOL b4Digit; /* Four digit year */
BOOL bDataStreams; /* Displays alternate data streams */
struct
{
DWORD dwAttribVal; /* The desired state of attribute */
DWORD dwAttribMask; /* Which attributes to check */
} stAttribs; /* Displays files with this attributes only */
struct
{
enum EOrderBy eCriteria[3]; /* Criterias used to order by */
BOOL bCriteriaRev[3]; /* If the criteria is in reversed order */
short sCriteriaCount; /* The quantity of criterias */
} stOrderBy; /* Ordered by criterias */
struct
{
enum ETimeField eTimeField; /* The time field that will be used for */
} stTimeField; /* The time field to display or use for sorting */
} DIRSWITCHFLAGS, *LPDIRSWITCHFLAGS;
typedef struct _DIRFINDSTREAMNODE
{
WIN32_FIND_STREAM_DATA stStreamInfo;
struct _DIRFINDSTREAMNODE *ptrNext;
} DIRFINDSTREAMNODE, *PDIRFINDSTREAMNODE;
typedef struct _DIRFINDINFO
{
WIN32_FIND_DATA stFindInfo;
PDIRFINDSTREAMNODE ptrHead;
} DIRFINDINFO, *PDIRFINDINFO;
typedef struct _DIRFINDLISTNODE
{
DIRFINDINFO stInfo;
struct _DIRFINDLISTNODE *ptrNext;
} DIRFINDLISTNODE, *PDIRFINDLISTNODE;
typedef BOOL
(WINAPI *PGETFREEDISKSPACEEX)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
/* Globally save the # of dirs, files and bytes,
* probably later pass them to functions. Rob Lake */
static ULONG recurse_dir_cnt;
static ULONG recurse_file_cnt;
static ULONGLONG recurse_bytes;
/*
* help
*
* displays help screen for dir
* Rob Lake
*/
static VOID
DirHelp(VOID)
{
ConOutResPaging(TRUE, STRING_DIR_HELP1);
}
/* Check whether this is a dot-directory "." or "..", speed-optimized */
FORCEINLINE
BOOL
IsDotDirectory(
IN LPCTSTR pszPath)
{
return ( pszPath[0] == _T('.') &&
( pszPath[1] == 0 || /* pszPath[1] == _T('\\') || */
(pszPath[1] == _T('.') && (pszPath[2] == 0 /* || pszPath[2] == _T('\\') */))
) );
}
FORCEINLINE
BOOL
IsDotDirectoryN(
IN const TCHAR* pPath,
IN SIZE_T Length)
{
return ((Length == 1 && pPath[0] == _T('.')) ||
(Length == 2 && pPath[0] == _T('.') && pPath[1] == _T('.')));
}
/*
* DirReadParameters
*
* Parse the parameters and switches of the command line and exports them
*/
static BOOL
DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
LPTSTR** params, /* [OUT] The parameters after parsing */
LPINT entries, /* [OUT] The number of parameters after parsing */
LPDIRSWITCHFLAGS lpFlags) /* [IN/OUT] The flags after calculating switches */
{
TCHAR cCurSwitch; /* The current switch */
TCHAR cCurChar; /* Current examined character */
TCHAR cCurUChar; /* Current upper examined character */
BOOL bNegative; /* Negative switch */
BOOL bPNegative; /* Negative switch parameter */
BOOL bIntoQuotes; /* A flag showing if we are in quotes (") */
LPTSTR ptrStart; /* A pointer to the first character of a parameter */
LPTSTR ptrEnd; /* A pointer to the last character of a parameter */
BOOL bOrderByNoPar; /* A flag to indicate /O with no switch parameter */
LPTSTR temp;
/* Initialize parameter array */
*params = NULL;
*entries = 0;
/* Initialize variables; */
cCurSwitch = _T(' ');
bNegative = FALSE;
bPNegative = FALSE;
/* We suppose that switch parameters
were given to avoid setting them to default
if the switch was not given */
bOrderByNoPar = FALSE;
/* Main Loop (see README_DIR.txt) */
/* scan the command line char per char, and we process its char */
while (*Line)
{
/* we save current character as it is and its upper case */
cCurChar = *Line;
cCurUChar = _totupper(*Line);
/* 1st section (see README_DIR.txt) */
/* When a switch is expecting */
if (cCurSwitch == _T('/'))
{
while (_istspace(*Line))
Line++;
bNegative = (*Line == _T('-'));
Line += bNegative;
cCurChar = *Line;
cCurUChar = _totupper(*Line);
if ((cCurUChar == _T('A')) ||(cCurUChar == _T('T')) || (cCurUChar == _T('O')))
{
/* If positive, prepare for parameters... if negative, reset to defaults */
switch (cCurUChar)
{
case _T('A'):
lpFlags->stAttribs.dwAttribVal = 0L;
lpFlags->stAttribs.dwAttribMask = 0L;
if (bNegative)
lpFlags->stAttribs.dwAttribMask = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
break;
case _T('T'):
if (bNegative)
lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE;
break;
case _T('O'):
bOrderByNoPar = !bNegative;
lpFlags->stOrderBy.sCriteriaCount = 0;
break;
}
if (!bNegative)
{
/* Positive switch, so it can take parameters. */
cCurSwitch = cCurUChar;
Line++;
/* Skip optional leading colon */
if (*Line == _T(':'))
Line++;
continue;
}
}
else if (cCurUChar == _T('L'))
lpFlags->bLowerCase = ! bNegative;
else if (cCurUChar == _T('B'))
lpFlags->bBareFormat = ! bNegative;
else if (cCurUChar == _T('C'))
lpFlags->bTSeparator = ! bNegative;
else if (cCurUChar == _T('W'))
lpFlags->bWideList = ! bNegative;
else if (cCurUChar == _T('D'))
lpFlags->bWideListColSort = ! bNegative;
else if (cCurUChar == _T('N'))
lpFlags->bNewLongList = ! bNegative;
else if (cCurUChar == _T('P'))
lpFlags->bPause = ! bNegative;
else if (cCurUChar == _T('Q'))
lpFlags->bUser = ! bNegative;
else if (cCurUChar == _T('S'))
lpFlags->bRecursive = ! bNegative;
else if (cCurUChar == _T('X'))
lpFlags->bShortName = ! bNegative;
else if (cCurUChar == _T('R'))
lpFlags->bDataStreams = ! bNegative;
else if (cCurChar == _T('4'))
lpFlags->b4Digit = ! bNegative;
else if (cCurChar == _T('?'))
{
DirHelp();
return FALSE;
}
else
{
error_invalid_switch ((TCHAR)_totupper(*Line));
return FALSE;
}
/* Make sure there's no extra characters at the end of the switch */
if (Line[1] && Line[1] != _T('/') && !_istspace(Line[1]))
{
error_parameter_format(Line[1]);
return FALSE;
}
cCurSwitch = _T(' ');
}
else if (cCurSwitch == _T(' '))
{
/* 2nd section (see README_DIR.txt) */
/* We are expecting parameter or the unknown */
if (cCurChar == _T('/'))
cCurSwitch = _T('/');
else if (_istspace(cCurChar))
/* do nothing */;
else
{
/* This is a file/directory name parameter. Find its end */
ptrStart = Line;
bIntoQuotes = FALSE;
while (*Line)
{
if (!bIntoQuotes && (*Line == _T('/') || _istspace(*Line)))
break;
bIntoQuotes ^= (*Line == _T('"'));
Line++;
}
ptrEnd = Line;
/* Copy it to the entries list */
temp = cmd_alloc((ptrEnd - ptrStart + 1) * sizeof(TCHAR));
if (!temp)
return FALSE;
memcpy(temp, ptrStart, (ptrEnd - ptrStart) * sizeof(TCHAR));
temp[ptrEnd - ptrStart] = _T('\0');
StripQuotes(temp);
if (!add_entry(entries, params, temp))
{
cmd_free(temp);
freep(*params);
return FALSE;
}
cmd_free(temp);
continue;
}
}
else
{
/* 3rd section (see README_DIR.txt) */
/* We are waiting for switch parameters */
/* Check if there are no more switch parameters */
if ((cCurChar == _T('/')) || _istspace(cCurChar))
{
/* Wrong decision path, reprocess current character */
cCurSwitch = _T(' ');
continue;
}
/* Process parameter switch */
switch (cCurSwitch)
{
case _T('A'): /* Switch parameters for /A (attributes filter) */
if (cCurChar == _T('-'))
bPNegative = TRUE;
else if (cCurUChar == _T('D'))
{
lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_DIRECTORY;
if (bPNegative)
lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_DIRECTORY;
else
lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_DIRECTORY;
}
else if (cCurUChar == _T('R'))
{
lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_READONLY;
if (bPNegative)
lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_READONLY;
else
lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_READONLY;
}
else if (cCurUChar == _T('H'))
{
lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_HIDDEN;
if (bPNegative)
lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_HIDDEN;
else
lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_HIDDEN;
}
else if (cCurUChar == _T('A'))
{
lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_ARCHIVE;
if (bPNegative)
lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_ARCHIVE;
else
lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_ARCHIVE;
}
else if (cCurUChar == _T('S'))
{
lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_SYSTEM;
if (bPNegative)
lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_SYSTEM;
else
lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_SYSTEM;
}
else
{
error_parameter_format((TCHAR)_totupper (*Line));
return FALSE;
}
break;
case _T('T'): /* Switch parameters for /T (time field) */
if (cCurUChar == _T('C'))
lpFlags->stTimeField.eTimeField= TF_CREATIONDATE ;
else if (cCurUChar == _T('A'))
lpFlags->stTimeField.eTimeField= TF_LASTACCESSEDDATE ;
else if (cCurUChar == _T('W'))
lpFlags->stTimeField.eTimeField= TF_MODIFIEDDATE ;
else
{
error_parameter_format((TCHAR)_totupper (*Line));
return FALSE;
}
break;
case _T('O'): /* Switch parameters for /O (order) */
/* Ok a switch parameter was given */
bOrderByNoPar = FALSE;
if (cCurChar == _T('-'))
bPNegative = TRUE;
else if (cCurUChar == _T('N'))
{
if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_NAME;
}
else if (cCurUChar == _T('S'))
{
if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_SIZE;
}
else if (cCurUChar == _T('G'))
{
if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_DIRECTORY;
}
else if (cCurUChar == _T('E'))
{
if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_EXTENSION;
}
else if (cCurUChar == _T('D'))
{
if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_TIME;
}
else
{
error_parameter_format((TCHAR)_totupper (*Line));
return FALSE;
}
}
/* We check if we calculated the negative value and release the flag */
if ((cCurChar != _T('-')) && bPNegative)
bPNegative = FALSE;
}
Line++;
}
/* /O with no switch parameters acts like /O:GN */
if (bOrderByNoPar)
{
lpFlags->stOrderBy.sCriteriaCount = 2;
lpFlags->stOrderBy.eCriteria[0] = ORDER_DIRECTORY;
lpFlags->stOrderBy.bCriteriaRev[0] = FALSE;
lpFlags->stOrderBy.eCriteria[1] = ORDER_NAME;
lpFlags->stOrderBy.bCriteriaRev[1] = FALSE;
}
return TRUE;
}
/* Print either with or without paging, depending on /P switch */
static BOOL
DirPrintf(LPDIRSWITCHFLAGS lpFlags, LPTSTR szFormat, ...)
{
BOOL Done = TRUE;
va_list arg_ptr;
va_start(arg_ptr, szFormat);
if (lpFlags->bPause)
Done = ConPrintfVPaging(&StdOutPager, FALSE, szFormat, arg_ptr);
else
ConPrintfV(StdOut, szFormat, arg_ptr);
va_end(arg_ptr);
return Done;
}
/*
* PrintDirectoryHeader
*
* print the header for the dir command
*/
static BOOL
PrintDirectoryHeader(LPCTSTR szPath, LPDIRSWITCHFLAGS lpFlags)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
LPCTSTR szFullDir;
TCHAR szRootName[MAX_PATH];
TCHAR szVolName[80];
DWORD dwSerialNr;
if (lpFlags->bBareFormat)
return TRUE;
szFullDir = szPath;
/* Get the media ID of the drive */
if (!GetVolumePathName(szFullDir, szRootName, ARRAYSIZE(szRootName)) ||
!GetVolumeInformation(szRootName, szVolName, ARRAYSIZE(szVolName),
&dwSerialNr, NULL, NULL, NULL, 0))
{
return TRUE;
}
/* Print drive info */
if (szVolName[0] != _T('\0'))
{
LoadString(CMD_ModuleHandle, STRING_DIR_HELP2, szMsg, ARRAYSIZE(szMsg));
DirPrintf(lpFlags, szMsg, _totupper(szRootName[0]), szVolName);
}
else
{
LoadString(CMD_ModuleHandle, STRING_DIR_HELP3, szMsg, ARRAYSIZE(szMsg));
DirPrintf(lpFlags, szMsg, _totupper(szRootName[0]));
}
/* Print the volume serial number if the return was successful */
LoadString(CMD_ModuleHandle, STRING_DIR_HELP4, szMsg, ARRAYSIZE(szMsg));
DirPrintf(lpFlags, szMsg, HIWORD(dwSerialNr), LOWORD(dwSerialNr));
return TRUE;
}
static VOID
DirPrintFileDateTime(TCHAR *lpDate,
TCHAR *lpTime,
LPWIN32_FIND_DATA lpFile,
LPDIRSWITCHFLAGS lpFlags)
{
FILETIME ft;
SYSTEMTIME dt;
/* Select the right time field */
switch (lpFlags->stTimeField.eTimeField)
{
case TF_CREATIONDATE:
if (!FileTimeToLocalFileTime(&lpFile->ftCreationTime, &ft))
return;
FileTimeToSystemTime(&ft, &dt);
break;
case TF_LASTACCESSEDDATE :
if (!FileTimeToLocalFileTime(&lpFile->ftLastAccessTime, &ft))
return;
FileTimeToSystemTime(&ft, &dt);
break;
case TF_MODIFIEDDATE:
if (!FileTimeToLocalFileTime(&lpFile->ftLastWriteTime, &ft))
return;
FileTimeToSystemTime(&ft, &dt);
break;
}
FormatDate(lpDate, &dt, lpFlags->b4Digit);
FormatTime(lpTime, &dt);
}
INT
FormatDate(TCHAR *lpDate, LPSYSTEMTIME dt, BOOL b4Digit)
{
/* Format date */
WORD wYear = b4Digit ? dt->wYear : dt->wYear%100;
switch (nDateFormat)
{
case 0: /* mmddyy */
default:
return _stprintf(lpDate, _T("%02d%c%02d%c%0*d"),
dt->wMonth, cDateSeparator,
dt->wDay, cDateSeparator,
b4Digit?4:2, wYear);
break;
case 1: /* ddmmyy */
return _stprintf(lpDate, _T("%02d%c%02d%c%0*d"),
dt->wDay, cDateSeparator, dt->wMonth,
cDateSeparator, b4Digit?4:2, wYear);
break;
case 2: /* yymmdd */
return _stprintf(lpDate, _T("%0*d%c%02d%c%02d"),
b4Digit?4:2, wYear, cDateSeparator,
dt->wMonth, cDateSeparator, dt->wDay);
break;
}
}
INT
FormatTime(TCHAR *lpTime, LPSYSTEMTIME dt)
{
/* Format Time */
switch (nTimeFormat)
{
case 0: /* 12 hour format */
default:
return _stprintf(lpTime,_T("%02d%c%02u %cM"),
(dt->wHour == 0 ? 12 : (dt->wHour <= 12 ? dt->wHour : dt->wHour - 12)),
cTimeSeparator,
dt->wMinute, (dt->wHour <= 11 ? _T('A') : _T('P')));
break;
case 1: /* 24 hour format */
return _stprintf(lpTime, _T("%02d%c%02u"),
dt->wHour, cTimeSeparator, dt->wMinute);
break;
}
}
static VOID
GetUserDiskFreeSpace(LPCTSTR lpRoot,
PULARGE_INTEGER lpFreeSpace)
{
PGETFREEDISKSPACEEX pGetFreeDiskSpaceEx;
HINSTANCE hInstance;
DWORD dwSecPerCl;
DWORD dwBytPerSec;
DWORD dwFreeCl;
DWORD dwTotCl;
ULARGE_INTEGER TotalNumberOfBytes, TotalNumberOfFreeBytes;
lpFreeSpace->QuadPart = 0;
hInstance = GetModuleHandle(_T("KERNEL32"));
if (hInstance != NULL)
{
pGetFreeDiskSpaceEx = (PGETFREEDISKSPACEEX)GetProcAddress(hInstance,
#ifdef _UNICODE
"GetDiskFreeSpaceExW");
#else
"GetDiskFreeSpaceExA");
#endif
if (pGetFreeDiskSpaceEx != NULL)
{
if (pGetFreeDiskSpaceEx(lpRoot, lpFreeSpace, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) != FALSE)
return;
}
}
GetDiskFreeSpace(lpRoot,
&dwSecPerCl,
&dwBytPerSec,
&dwFreeCl,
&dwTotCl);
lpFreeSpace->QuadPart = dwSecPerCl * dwBytPerSec * dwFreeCl;
}
/*
* print_summary: prints dir summary
* Added by Rob Lake 06/17/98 to compact code
* Just copied Tim's Code and patched it a bit
*/
static INT
PrintSummary(LPCTSTR szPath,
ULONG ulFiles,
ULONG ulDirs,
ULONGLONG u64Bytes,
LPDIRSWITCHFLAGS lpFlags,
BOOL TotalSummary)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
TCHAR szBuffer[64];
ULARGE_INTEGER uliFree;
/* Here we check if we didn't find anything */
if (!(ulFiles + ulDirs))
{
if (!lpFlags->bRecursive || (TotalSummary && lpFlags->bRecursive))
error_file_not_found();
return 1;
}
/* In bare format we don't print results */
if (lpFlags->bBareFormat)
return 0;
/* Print recursive specific results */
/* Take this code offline to fix /S does not print double info */
if (TotalSummary && lpFlags->bRecursive)
{
ConvertULargeInteger(u64Bytes, szBuffer, ARRAYSIZE(szBuffer), lpFlags->bTSeparator);
LoadString(CMD_ModuleHandle, STRING_DIR_HELP5, szMsg, ARRAYSIZE(szMsg));
DirPrintf(lpFlags, szMsg, ulFiles, szBuffer);
}
else
{
/* Print File Summary */
/* Condition to print summary is:
If we are not in bare format and if we have results! */
ConvertULargeInteger(u64Bytes, szBuffer, ARRAYSIZE(szBuffer), lpFlags->bTSeparator);
LoadString(CMD_ModuleHandle, STRING_DIR_HELP8, szMsg, ARRAYSIZE(szMsg));
DirPrintf(lpFlags, szMsg, ulFiles, szBuffer);
}
/* Print total directories and free space */
if (!lpFlags->bRecursive || TotalSummary)
{
GetUserDiskFreeSpace(szPath, &uliFree);
ConvertULargeInteger(uliFree.QuadPart, szBuffer, ARRAYSIZE(szBuffer), lpFlags->bTSeparator);
LoadString(CMD_ModuleHandle, STRING_DIR_HELP6, szMsg, ARRAYSIZE(szMsg));
DirPrintf(lpFlags, szMsg, ulDirs, szBuffer);
}
return 0;
}
/*
* getExt
*
* Get the extension of a filename
*/
TCHAR* getExt(const TCHAR* file)
{
static TCHAR *NoExt = _T("");
TCHAR* lastdot = _tcsrchr(file, _T('.'));
return (lastdot != NULL ? lastdot + 1 : NoExt);
}
/*
* getName
*
* Get the name of the file without extension
*/
static LPTSTR
getName(const TCHAR* file, TCHAR * dest)
{
INT_PTR iLen;
LPTSTR end;
/* Check for dot-directories "." and ".." */
if (IsDotDirectory(file))
{
_tcscpy(dest, file);
return dest;
}
end = _tcsrchr(file, _T('.'));
if (!end)
iLen = _tcslen(file);
else
iLen = (end - file);
_tcsncpy(dest, file, iLen);
*(dest + iLen) = _T('\0');
return dest;
}
/*
* DirPrintNewList
*
* The function that prints in new style
*/
static VOID
DirPrintNewList(PDIRFINDINFO ptrFiles[], /* [IN]Files' Info */
DWORD dwCount, /* [IN] The quantity of files */
LPCTSTR szCurPath, /* [IN] Full path of current directory */
LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
{
DWORD i;
TCHAR szSize[30];
TCHAR szShortName[15];
TCHAR szDate[20];
TCHAR szTime[20];
INT iSizeFormat;
ULARGE_INTEGER u64FileSize;
PDIRFINDSTREAMNODE ptrCurStream;
for (i = 0; i < dwCount && !CheckCtrlBreak(BREAK_INPUT); i++)
{
/* Calculate size */
if (ptrFiles[i]->stFindInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
/* Junction */
iSizeFormat = -14;
_tcscpy(szSize, _T("<JUNCTION>"));
}
else if (ptrFiles[i]->stFindInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
/* Directory */
iSizeFormat = -14;
_tcscpy(szSize, _T("<DIR>"));
}
else
{
/* File */
iSizeFormat = 14;
u64FileSize.HighPart = ptrFiles[i]->stFindInfo.nFileSizeHigh;
u64FileSize.LowPart = ptrFiles[i]->stFindInfo.nFileSizeLow;
ConvertULargeInteger(u64FileSize.QuadPart, szSize, 20, lpFlags->bTSeparator);
}
/* Calculate short name */
szShortName[0] = _T('\0');
if (lpFlags->bShortName)
_stprintf(szShortName, _T(" %-12s"), ptrFiles[i]->stFindInfo.cAlternateFileName);
/* Format date and time */
DirPrintFileDateTime(szDate, szTime, &ptrFiles[i]->stFindInfo, lpFlags);
/* Print the line */
DirPrintf(lpFlags, _T("%10s %-6s %*s%s %s\n"),
szDate,
szTime,
iSizeFormat,
szSize,
szShortName,
ptrFiles[i]->stFindInfo.cFileName);
/* Now, loop on the streams */
ptrCurStream = ptrFiles[i]->ptrHead;
while (ptrCurStream)
{
ConvertULargeInteger(ptrCurStream->stStreamInfo.StreamSize.QuadPart, szSize, 20, lpFlags->bTSeparator);
/* Print the line */
DirPrintf(lpFlags, _T("%10s %-6s %*s%s %s%s\n"),
L"",
L"",
16,
szSize,
L"",
ptrFiles[i]->stFindInfo.cFileName,
ptrCurStream->stStreamInfo.cStreamName);
ptrCurStream = ptrCurStream->ptrNext;
}
}
}
/*
* DirPrintWideList
*
* The function that prints in wide list
*/
static VOID
DirPrintWideList(PDIRFINDINFO ptrFiles[], /* [IN] Files' Info */
DWORD dwCount, /* [IN] The quantity of files */
LPCTSTR szCurPath, /* [IN] Full path of current directory */
LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
{
SHORT iScreenWidth;
USHORT iColumns;
USHORT iLines;
UINT_PTR iLongestName;
TCHAR szTempFname[MAX_PATH];
DWORD i;
DWORD j;
DWORD temp;
BOOL bCJK = IsCJKCodePage(OutputCodePage);
SIZE_T cxWidth;
/* Calculate longest name */
iLongestName = 1;
for (i = 0; i < dwCount; i++)
{
cxWidth = ConGetTextWidth(ptrFiles[i]->stFindInfo.cFileName);
/* Directories need 2 additional characters for brackets */
if (ptrFiles[i]->stFindInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
cxWidth += 2;
iLongestName = max(iLongestName, cxWidth);
}
/* Count the highest number of columns */
GetScreenSize(&iScreenWidth, NULL);
iColumns = (USHORT)(iScreenWidth / iLongestName);
/* Check if there is enough space for spaces between names */
if (((iLongestName * iColumns) + iColumns) >= (UINT)iScreenWidth)
iColumns --;
/* A last check at iColumns to avoid division by zero */
if (!iColumns) iColumns = 1;
/* Calculate the lines that will be printed */
iLines = (USHORT)((dwCount + iColumns - 1) / iColumns);
for (i = 0; i < iLines && !CheckCtrlBreak(BREAK_INPUT); i++)
{