e2437847351625889fd260de81eee2e2c6ee5eec
[reactos.git] / reactos / subsys / system / cmd / dir.c
1 /*
2 * DIR.C - dir internal command.
3 *
4 *
5 * History:
6 *
7 * 01/29/97 (Tim Norman)
8 * started.
9 *
10 * 06/13/97 (Tim Norman)
11 * Fixed code.
12 *
13 * 07/12/97 (Tim Norman)
14 * Fixed bug that caused the root directory to be unlistable
15 *
16 * 07/12/97 (Marc Desrochers)
17 * Changed to use maxx, maxy instead of findxy()
18 *
19 * 06/08/98 (Rob Lake)
20 * Added compatibility for /w in dir
21 *
22 * 06/09/98 (Rob Lake)
23 * Compatibility for dir/s started
24 * Tested that program finds directories off root fine
25 *
26 * 06/10/98 (Rob Lake)
27 * do_recurse saves the cwd and also stores it in Root
28 * build_tree adds the cwd to the beginning of its' entries
29 * Program runs fine, added print_tree -- works fine.. as EXE,
30 * program won't work properly as COM.
31 *
32 * 06/11/98 (Rob Lake)
33 * Found problem that caused COM not to work
34 *
35 * 06/12/98 (Rob Lake)
36 * debugged...
37 * added free mem routine
38 *
39 * 06/13/98 (Rob Lake)
40 * debugged the free mem routine
41 * debugged whole thing some more
42 * Notes:
43 * ReadDir stores Root name and _Read_Dir does the hard work
44 * PrintDir prints Root and _Print_Dir does the hard work
45 * KillDir kills Root _after_ _Kill_Dir does the hard work
46 * Integrated program into DIR.C(this file) and made some same
47 * changes throughout
48 *
49 * 06/14/98 (Rob Lake)
50 * Cleaned up code a bit, added comments
51 *
52 * 06/16/98 (Rob Lake)
53 * Added error checking to my previously added routines
54 *
55 * 06/17/98 (Rob Lake)
56 * Rewrote recursive functions, again! Most other recursive
57 * functions are now obsolete -- ReadDir, PrintDir, _Print_Dir,
58 * KillDir and _Kill_Dir. do_recurse does what PrintDir did
59 * and _Read_Dir did what it did before along with what _Print_Dir
60 * did. Makes /s a lot faster!
61 * Reports 2 more files/dirs that MS-DOS actually reports
62 * when used in root directory(is this because dir defaults
63 * to look for read only files?)
64 * Added support for /b, /a and /l
65 * Made error message similar to DOS error messages
66 * Added help screen
67 *
68 * 06/20/98 (Rob Lake)
69 * Added check for /-(switch) to turn off previously defined
70 * switches.
71 * Added ability to check for DIRCMD in environment and
72 * process it
73 *
74 * 06/21/98 (Rob Lake)
75 * Fixed up /B
76 * Now can dir *.ext/X, no spaces!
77 *
78 * 06/29/98 (Rob Lake)
79 * error message now found in command.h
80 *
81 * 07/08/1998 (John P. Price)
82 * removed extra returns; closer to MSDOS
83 * fixed wide display so that an extra return is not displayed
84 * when there is five filenames in the last line.
85 *
86 * 07/12/98 (Rob Lake)
87 * Changed error messages
88 *
89 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
90 * added config.h include
91 *
92 *
93 * 04-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
94 * Converted source code to Win32, except recursive dir ("dir /s").
95 *
96 * 10-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
97 * Fixed recursive dir ("dir /s").
98 *
99 * 14-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
100 * Converted to Win32 directory functions and
101 * fixed some output bugs. There are still some more ;)
102 *
103 * 10-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
104 * Added "/N" and "/4" options, "/O" is a dummy.
105 * Added locale support.
106 *
107 * 20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
108 * Redirection safe!
109 *
110 * 01-Mar-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
111 * Replaced all runtime io functions by their Win32 counterparts.
112 *
113 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
114 * dir /s now works in deeper trees
115 *
116 * 28-Jan-2004 (Michael Fritscher <michael@fritscher.net>)
117 * Fix for /p, so it is working under Windows in GUI-mode, too.
118 *
119 * 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
120 * Fix /w to print long names.
121 *
122 * 27-Feb-2005 (Konstantinos Paliouras <squarious@gmail.com>)
123 * Implemented all the switches that were missing, and made
124 * the ros dir very similar to windows dir. Major part of
125 * the code is rewritten. /p is removed, to be rewriten in
126 * the main cmd code.
127 *
128 * 1-Jul-2004 (Brandon Turner <turnerb7@msu.edu>)
129 * Added /p back in using ConOutPrintfPaging
130 */
131
132 #include <precomp.h>
133 #include "resource.h"
134
135 #ifdef INCLUDE_CMD_DIR
136
137
138
139 /* Time Field enumeration */
140 enum ETimeField
141 {
142 TF_CREATIONDATE = 0,
143 TF_MODIFIEDDATE = 1,
144 TF_LASTACCESSEDDATE = 2
145 };
146
147 /* Ordered by enumeration */
148 enum EOrderBy
149 {
150 ORDER_NAME = 0,
151 ORDER_SIZE = 1,
152 ORDER_DIRECTORY = 2,
153 ORDER_EXTENSION = 3,
154 ORDER_TIME = 4
155 };
156
157 /* The struct for holding the switches */
158 typedef struct _DirSwitchesFlags
159 {
160 BOOL bBareFormat; /* Bare Format */
161 BOOL bTSeperator; /* Thousands seperator */
162 BOOL bWideList; /* Wide list format */
163 BOOL bWideListColSort; /* Wide list format but sorted by column */
164 BOOL bLowerCase; /* Uses lower case */
165 BOOL bNewLongList; /* New long list */
166 BOOL bPause; /* Pause per page */
167 BOOL bUser; /* Displays the owner of file */
168 BOOL bRecursive; /* Displays files in specified directory and all sub */
169 BOOL bShortName; /* Displays the sort name of files if exist */
170 BOOL b4Digit; /* Four digit year */
171 struct
172 {
173 DWORD dwAttribVal; /* The desired state of attribute */
174 DWORD dwAttribMask; /* Which attributes to check */
175 BOOL bUnSet; /* A helper flag if "-" was given with the switch */
176 BOOL bParSetted; /* A helper flag if parameters of switch were given */
177 } stAttribs; /* Displays files with this attributes only */
178 struct
179 {
180 enum EOrderBy eCriteria[3]; /* Criterias used to order by */
181 BOOL bCriteriaRev[3]; /* If the criteria is in reversed order */
182 short sCriteriaCount; /* The quantity of criterias */
183 BOOL bUnSet; /* A helper flag if "-" was given with the switch */
184 BOOL bParSetted; /* A helper flag if parameters of switch were given */
185 } stOrderBy; /* Ordered by criterias */
186 struct
187 {
188 enum ETimeField eTimeField; /* The time field that will be used for */
189 BOOL bUnSet; /* A helper flag if "-" was given with the switch */
190 BOOL bParSetted; /* A helper flag if parameters of switch were given */
191 } stTimeField; /* The time field to display or use for sorting */
192 } DIRSWITCHFLAGS, *LPDIRSWITCHFLAGS;
193
194
195 typedef struct _DIRFINDLISTNODE
196 {
197 WIN32_FIND_DATA stFindInfo;
198 struct _DIRFINDLISTNODE *ptrNext;
199 } DIRFINDLISTNODE, *PDIRFINDLISTNODE;
200
201
202 typedef BOOL
203 (WINAPI *PGETFREEDISKSPACEEX)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
204
205
206 /* Globally save the # of dirs, files and bytes,
207 * probabaly later pass them to functions. Rob Lake */
208 static ULONG recurse_dir_cnt;
209 static ULONG recurse_file_cnt;
210 static ULARGE_INTEGER recurse_bytes;
211
212
213 /*
214 * help
215 *
216 * displays help screen for dir
217 * Rob Lake
218 */
219 static VOID
220 DirHelp(VOID)
221 {
222 ConOutResPaging(TRUE, STRING_DIR_HELP1);
223 }
224
225
226
227 /*
228 * DirReadParameters
229 *
230 * Parse the parameters and switches of the command line and exports them
231 */
232 static BOOL
233 DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
234 LPTSTR** params, /* [OUT] The parameters after parsing */
235 LPINT entries, /* [OUT] The number of parameters after parsing */
236 LPDIRSWITCHFLAGS lpFlags) /* [IN/OUT] The flags after calculating switches */
237 {
238 TCHAR cCurSwitch; /* The current switch */
239 TCHAR cCurChar; /* Current examing character */
240 TCHAR cCurUChar; /* Current upper examing character */
241 BOOL bNegative; /* Negative switch */
242 BOOL bPNegative; /* Negative switch parameter */
243 BOOL bIntoQuotes; /* A flag showing if we are in quotes (") */
244 LPTSTR ptrStart; /* A pointer to the first character of a parameter */
245 LPTSTR ptrEnd; /* A pointer to the last character of a parameter */
246 LPTSTR temp;
247
248 /* Initialize parameter array */
249 *params = malloc(sizeof(LPTSTR));
250 if(!params)
251 return FALSE;
252 *params = NULL;
253 *entries = 0;
254 ptrStart = NULL;
255 ptrEnd = NULL;
256
257 /* Initialize variables; */
258 cCurSwitch = _T(' ');
259 bNegative = FALSE;
260 bPNegative = FALSE;
261 bIntoQuotes = FALSE;
262
263 /* We suppose that switch parameters
264 were given to avoid setting them to default
265 if the switch was not given */
266 lpFlags->stAttribs.bParSetted = TRUE;
267 lpFlags->stOrderBy.bParSetted = TRUE;
268 lpFlags->stTimeField.bParSetted = TRUE;
269
270
271 /* Main Loop (see README_DIR.txt) */
272 /* scan the command line char per char, and we process its char */
273 while (*Line)
274 {
275 /* we save current character as it is and its upper case */
276 cCurChar = *Line;
277 cCurUChar = _totupper(*Line);
278
279 /* 1st section (see README_DIR.txt) */
280 /* When a switch is expecting */
281 if (cCurSwitch == _T('/'))
282 {
283 if ((cCurUChar == _T('A')) ||(cCurUChar == _T('T')) || (cCurUChar == _T('O')))
284 {
285 cCurSwitch = cCurUChar;
286 switch (cCurUChar)
287 {
288 case _T('A'):
289 lpFlags->stAttribs.bUnSet = bNegative;
290 lpFlags->stAttribs.bParSetted = FALSE;
291 break;
292 case _T('T'):
293 lpFlags->stTimeField.bUnSet = bNegative;
294 lpFlags->stTimeField.bParSetted = FALSE;
295 break;
296 case _T('O'):
297 lpFlags->stOrderBy.bUnSet = bNegative;
298 lpFlags->stOrderBy.bParSetted = FALSE;
299 break;
300 }
301 }
302 else if (cCurUChar == _T('L'))
303 lpFlags->bLowerCase = ! bNegative;
304 else if (cCurUChar == _T('B'))
305 lpFlags->bBareFormat = ! bNegative;
306 else if (cCurUChar == _T('C'))
307 lpFlags->bTSeperator = ! bNegative;
308 else if (cCurUChar == _T('W'))
309 lpFlags->bWideList = ! bNegative;
310 else if (cCurUChar == _T('D'))
311 lpFlags->bWideListColSort = ! bNegative;
312 else if (cCurUChar == _T('N'))
313 lpFlags->bNewLongList = ! bNegative;
314 else if (cCurUChar == _T('P'))
315 lpFlags->bPause = ! bNegative;
316 else if (cCurUChar == _T('Q'))
317 lpFlags->bUser = ! bNegative;
318 else if (cCurUChar == _T('S'))
319 lpFlags->bRecursive = ! bNegative;
320 else if (cCurUChar == _T('X'))
321 lpFlags->bShortName = ! bNegative;
322 else if (cCurChar == _T('4'))
323 lpFlags->b4Digit = ! bNegative;
324 else if (cCurChar == _T('?'))
325 {
326 DirHelp();
327 return FALSE;
328 }
329 else if (cCurChar == _T('-'))
330 {
331 bNegative = TRUE;
332 }
333 else
334 {
335 error_invalid_switch ((TCHAR)_totupper (*Line));
336 return FALSE;
337 }
338
339 /* We check if we calculated the negative value and realese the flag */
340 if ((cCurChar != _T('-')) && bNegative)
341 bNegative = FALSE;
342
343 /* if not a,o,t or - option then next parameter is not a switch */
344 if ((cCurSwitch == _T('/')) && (!bNegative))
345 cCurSwitch = _T(' ');
346
347 }
348 else if ((cCurSwitch == _T(' ')) || (cCurSwitch == _T('P')))
349 {
350 /* 2nd section (see README_DIR.txt) */
351 /* We are expecting parameter or the unknown */
352
353 if (cCurChar == _T('/'))
354 cCurSwitch = _T('/');
355
356 /* Process a spacer */
357 else if (cCurChar == _T(' '))
358 {
359 if (!bIntoQuotes)
360 {
361 cCurSwitch = _T(' ');
362 if(ptrStart && ptrEnd)
363 {
364 temp = malloc((ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
365 if(!temp)
366 return FALSE;
367 memcpy(temp, ptrStart, (ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
368 temp[(ptrEnd - ptrStart + 1)] = _T('\0');
369 if(!add_entry(entries, params, temp))
370 {
371 free(temp);
372 freep(*params);
373 return FALSE;
374 }
375
376 free(temp);
377
378 ptrStart = NULL;
379 ptrEnd = NULL;
380 }
381 }
382
383 }
384 else if (cCurChar == _T('\"'))
385 {
386 /* Process a quote */
387 bIntoQuotes = !bIntoQuotes;
388 if(!bIntoQuotes)
389 ptrEnd = Line;
390 }
391 else
392 {
393 /* Process a character for parameter */
394 if ((cCurSwitch == _T(' ')) && ptrStart && ptrEnd)
395 {
396 temp = malloc((ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
397 if(!temp)
398 return FALSE;
399 memcpy(temp, ptrStart, (ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
400 temp[(ptrEnd - ptrStart + 1)] = _T('\0');
401 if(!add_entry(entries, params, temp))
402 {
403 free(temp);
404 freep(*params);
405 return FALSE;
406 }
407
408 free(temp);
409
410 ptrStart = NULL;
411 ptrEnd = NULL;
412 }
413 cCurSwitch = _T('P');
414 if(!ptrStart)
415 ptrStart = ptrEnd = Line;
416 ptrEnd = Line;
417 }
418 }
419 else
420 {
421 /* 3rd section (see README_DIR.txt) */
422 /* We are waiting for switch parameters */
423
424 /* Check if there are no more switch parameters */
425 if ((cCurChar == _T('/')) || ( cCurChar == _T(' ')))
426 {
427 /* Wrong desicion path, reprocess current character */
428 cCurSwitch = cCurChar;
429 continue;
430 }
431 /* Process parameter switch */
432 switch(cCurSwitch)
433 {
434 case _T('A'): /* Switch parameters for /A (attributes filter) */
435 /* Ok a switch parameter was given */
436 lpFlags->stAttribs.bParSetted = TRUE;
437
438 if (cCurChar == _T(':'))
439 /* =V= dead command, used to make the "if" work */
440 cCurChar = cCurChar;
441 else if(cCurChar == _T('-'))
442 bPNegative = TRUE;
443 else if(cCurUChar == _T('D'))
444 {
445 lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_DIRECTORY;
446 if (bPNegative)
447 lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_DIRECTORY;
448 else
449 lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_DIRECTORY;
450 }
451 else if(cCurUChar == _T('R'))
452 {
453 lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_READONLY;
454 if (bPNegative)
455 lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_READONLY;
456 else
457 lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_READONLY;
458 }
459 else if(cCurUChar == _T('H'))
460 {
461 lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_HIDDEN;
462 if (bPNegative)
463 lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_HIDDEN;
464 else
465 lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_HIDDEN;
466 }
467 else if(cCurUChar == _T('A'))
468 {
469 lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_ARCHIVE;
470 if (bPNegative)
471 lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_ARCHIVE;
472 else
473 lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_ARCHIVE;
474 }
475 else if(cCurUChar == _T('S'))
476 {
477 lpFlags->stAttribs.dwAttribMask |= FILE_ATTRIBUTE_SYSTEM;
478 if (bPNegative)
479 lpFlags->stAttribs.dwAttribVal &= ~FILE_ATTRIBUTE_SYSTEM;
480 else
481 lpFlags->stAttribs.dwAttribVal |= FILE_ATTRIBUTE_SYSTEM;
482 }
483 else
484 {
485 error_parameter_format((TCHAR)_totupper (*Line));
486 return FALSE;
487 }
488 break;
489 case _T('T'): /* Switch parameters for /T (time field) */
490
491 /* Ok a switch parameter was given */
492 lpFlags->stTimeField.bParSetted = TRUE;
493
494 if (cCurChar == _T(':'))
495 /* =V= dead command, used to make the "if" work */
496 cCurChar = cCurChar;
497 else if(cCurUChar == _T('C'))
498 lpFlags->stTimeField.eTimeField= TF_CREATIONDATE ;
499 else if(cCurUChar == _T('A'))
500 lpFlags->stTimeField.eTimeField= TF_LASTACCESSEDDATE ;
501 else if(cCurUChar == _T('W'))
502 lpFlags->stTimeField.eTimeField= TF_MODIFIEDDATE ;
503 else
504 {
505 error_parameter_format((TCHAR)_totupper (*Line));
506 return FALSE;
507 }
508 break;
509 case _T('O'): /* Switch parameters for /O (order) */
510 /* Ok a switch parameter was given */
511 lpFlags->stOrderBy.bParSetted = TRUE;
512
513 if (cCurChar == _T(':'))
514 /* <== dead command, used to make the "if" work */
515 cCurChar = cCurChar;
516 else if(cCurChar == _T('-'))
517 bPNegative = TRUE;
518 else if(cCurUChar == _T('N'))
519 {
520 if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
521 lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
522 lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_NAME;
523 }
524 else if(cCurUChar == _T('S'))
525 {
526 if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
527 lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
528 lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_SIZE;
529 }
530 else if(cCurUChar == _T('G'))
531 {
532 if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
533 lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
534 lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_DIRECTORY;
535 }
536 else if(cCurUChar == _T('E'))
537 {
538 if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
539 lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
540 lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_EXTENSION;
541 }
542 else if(cCurUChar == _T('D'))
543 {
544 if (lpFlags->stOrderBy.sCriteriaCount < 3) lpFlags->stOrderBy.sCriteriaCount++;
545 lpFlags->stOrderBy.bCriteriaRev[lpFlags->stOrderBy.sCriteriaCount - 1] = bPNegative;
546 lpFlags->stOrderBy.eCriteria[lpFlags->stOrderBy.sCriteriaCount - 1] = ORDER_TIME;
547 }
548
549 else
550 {
551 error_parameter_format((TCHAR)_totupper (*Line));
552 return FALSE;
553 }
554
555
556 }
557 /* We check if we calculated the negative value and realese the flag */
558 if ((cCurChar != _T('-')) && bPNegative)
559 bPNegative = FALSE;
560 }
561
562 Line++;
563 }
564 /* Terminate the parameters */
565 if(ptrStart && ptrEnd)
566 {
567 temp = malloc((ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
568 if(!temp)
569 return FALSE;
570 memcpy(temp, ptrStart, (ptrEnd - ptrStart) + 2 * sizeof (TCHAR));
571 temp[(ptrEnd - ptrStart + 1)] = _T('\0');
572 if(!add_entry(entries, params, temp))
573 {
574 free(temp);
575 freep(*params);
576 return FALSE;
577 }
578
579 free(temp);
580
581 ptrStart = NULL;
582 ptrEnd = NULL;
583 }
584
585 /* Calculate the switches with no switch paramater */
586 if (!(lpFlags->stAttribs.bParSetted))
587 {
588 lpFlags->stAttribs.dwAttribVal = 0L;
589 lpFlags->stAttribs.dwAttribMask = lpFlags->stAttribs.dwAttribVal;
590 }
591 if (!(lpFlags->stOrderBy.bParSetted))
592 {
593 lpFlags->stOrderBy.sCriteriaCount = 1;
594 lpFlags->stOrderBy.eCriteria[0] = ORDER_NAME;
595 lpFlags->stOrderBy.bCriteriaRev[0] = FALSE;
596 }
597 if (!(lpFlags->stOrderBy.bParSetted))
598 lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE ;
599
600 /* Calculate the unsetted switches (the "-" prefixed)*/
601 if (lpFlags->stAttribs.bUnSet)
602 {
603 lpFlags->stAttribs.bUnSet = FALSE;
604 lpFlags->stAttribs.dwAttribVal = 0L;
605 lpFlags->stAttribs.dwAttribMask = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
606 }
607 if (lpFlags->stOrderBy.bUnSet)
608 {
609 lpFlags->stOrderBy.bUnSet = FALSE;
610 lpFlags->stOrderBy.sCriteriaCount = 0;
611 }
612 if (lpFlags->stTimeField.bUnSet )
613 {
614 lpFlags->stTimeField.bUnSet = FALSE;
615 lpFlags->stTimeField.eTimeField = TF_MODIFIEDDATE;
616 }
617 return TRUE;
618 }
619
620
621 /*
622 * ExtendFilespec
623 *
624 * extend the filespec, possibly adding wildcards
625 */
626 static VOID
627 ExtendFilespec (LPTSTR file)
628 {
629 INT len = 0;
630
631 if (!file)
632 return;
633
634
635 /* if no file spec, change to "*.*" */
636 if (*file == _T('\0'))
637 {
638 _tcscpy (file, _T("*.*"));
639 return;
640 }
641
642 // add support for *.
643 if ((file[0] == _T('*')) && (file[1] == _T('.') ))
644 {
645 return;
646 }
647
648 /* if starts with . add * in front */
649 if (*file == _T('.'))
650 {
651 memmove (&file[1], &file[0], (_tcslen (file) + 1) * sizeof(TCHAR));
652 file[0] = _T('*');
653 }
654
655 /* if no . add .* */
656 if (!_tcschr (file, _T('.')))
657 {
658 _tcscat (file, _T(".*"));
659 return;
660 }
661
662
663
664 /* if last character is '.' add '*' */
665 len = _tcslen (file);
666 if (file[len - 1] == _T('.'))
667 {
668 _tcscat (file, _T("*"));
669 return;
670 }
671 }
672
673
674 /*
675 * dir_parse_pathspec
676 *
677 * split the pathspec into drive, directory, and filespec
678 */
679 static INT
680 DirParsePathspec (LPTSTR szPathspec, LPTSTR szPath, LPTSTR szFilespec)
681 {
682 TCHAR szOrigPath[MAX_PATH];
683 LPTSTR start;
684 LPTSTR tmp;
685 INT i;
686 BOOL bWildcards = FALSE;
687
688 GetCurrentDirectory (MAX_PATH, szOrigPath);
689
690 /* get the drive and change to it */
691 if (szPathspec[1] == _T(':'))
692 {
693 TCHAR szRootPath[] = _T("A:");
694
695 szRootPath[0] = szPathspec[0];
696 start = szPathspec + 2;
697 if (!SetCurrentDirectory (szRootPath))
698 {
699 ErrorMessage (GetLastError(), NULL);
700 return 1;
701 }
702 }
703 else
704 {
705 start = szPathspec;
706 }
707
708
709 /* check for wildcards */
710 for (i = 0; szPathspec[i]; i++)
711 {
712 if (szPathspec[i] == _T('*') || szPathspec[i] == _T('?'))
713 bWildcards = TRUE;
714 }
715
716 /* check if this spec is a directory */
717 if (!bWildcards)
718 {
719 if (SetCurrentDirectory (szPathspec))
720 {
721 _tcscpy (szFilespec, _T("*.*"));
722
723 if (!GetCurrentDirectory (MAX_PATH, szPath))
724 {
725 szFilespec[0] = _T('\0');
726 SetCurrentDirectory (szOrigPath);
727 error_out_of_memory();
728 return 1;
729 }
730
731 SetCurrentDirectory (szOrigPath);
732 return 0;
733 }
734 }
735
736 /* find the file spec */
737 tmp = _tcsrchr (start, _T('\\'));
738
739 /* if no path is specified */
740 if (!tmp)
741 {
742 _tcscpy (szFilespec, start);
743 ExtendFilespec (szFilespec);
744 if (!GetCurrentDirectory (MAX_PATH, szPath))
745 {
746 szFilespec[0] = _T('\0');
747 SetCurrentDirectory (szOrigPath);
748 error_out_of_memory();
749 return 1;
750 }
751
752 SetCurrentDirectory (szOrigPath);
753 return 0;
754 }
755
756 /* get the filename */
757 _tcscpy (szFilespec, tmp+1);
758 ExtendFilespec (szFilespec);
759
760 if (tmp == start)
761 {
762 /* change to the root directory */
763 if (!SetCurrentDirectory (_T("\\")))
764 {
765 szFilespec[0] = _T('\0');
766 SetCurrentDirectory (szOrigPath);
767 error_path_not_found ();
768 return 1;
769 }
770 }
771 else
772 {
773 *tmp = _T('\0');
774
775 /* change to this directory */
776 if (!SetCurrentDirectory (start))
777 {
778 *tmp = _T('\\');
779 szFilespec[0] = _T('\0');
780 SetCurrentDirectory (szOrigPath);
781 error_path_not_found ();
782 return 1;
783 }
784 }
785
786 /* get the full name of the directory */
787 if (!GetCurrentDirectory (MAX_PATH, szPath))
788 {
789 *tmp = _T('\\');
790 szFilespec[0] = _T('\0');
791 SetCurrentDirectory (szOrigPath);
792 error_out_of_memory ();
793 return 1;
794 }
795
796 *tmp = _T('\\');
797
798 SetCurrentDirectory (szOrigPath);
799
800 return 0;
801 }
802
803
804 /*
805 * incline
806 *
807 * increment our line if paginating, display message at end of screen
808 */
809 #if 0
810 static BOOL
811 IncLine (LPINT pLine, LPDIRSWITCHFLAGS lpFlags)
812 {
813 BOOL bError;
814 CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo;
815 LONG WindowHeight;
816
817 bError = GetConsoleScreenBufferInfo(hConsole, &lpConsoleScreenBufferInfo);
818
819 WindowHeight = lpConsoleScreenBufferInfo.srWindow.Bottom - lpConsoleScreenBufferInfo.srWindow.Top;
820
821 /* That prevents bad behiour if WindowHeight could not be calculated */
822 if (!WindowHeight)
823 {
824 WindowHeight= 1000000;
825 }
826
827 if (!(lpFlags->bPause))
828 return FALSE;
829
830 (*pLine)++;
831
832 /*
833 * Because I don't know if WindowsHeight work in all cases,
834 * perhaps then maxy is the right value
835 */
836 if (*pLine >= (int)maxy - 2 || *pLine >= WindowHeight)
837 {
838 *pLine = 0;
839 return (PagePrompt () == PROMPT_BREAK);
840 }
841
842 return FALSE;
843 }
844 #endif
845
846 /*
847 * PrintDirectoryHeader
848 *
849 * print the header for the dir command
850 */
851 static BOOL
852 PrintDirectoryHeader(LPTSTR szPath, LPINT pLine, LPDIRSWITCHFLAGS lpFlags)
853 {
854 TCHAR szMsg[RC_STRING_MAX_SIZE];
855 TCHAR szRootName[MAX_PATH];
856 TCHAR szVolName[80];
857 DWORD dwSerialNr;
858 LPTSTR p;
859
860 if (lpFlags->bBareFormat)
861 return TRUE;
862
863 /* build usable root path */
864 if (szPath[1] == _T(':') && szPath[2] == _T('\\'))
865 {
866 /* normal path */
867 szRootName[0] = szPath[0];
868 szRootName[1] = _T(':');
869 szRootName[2] = _T('\\');
870 szRootName[3] = 0;
871 }
872 else if (szPath[0] == _T('\\') && szPath[1] == _T('\\'))
873 {
874 /* UNC path */
875 p = _tcschr(&szPath[2], _T('\\'));
876 if (p == NULL)
877 {
878 error_invalid_drive();
879 return(FALSE);
880 }
881 p = _tcschr(p+1, _T('\\'));
882 if (p == NULL)
883 {
884 _tcscpy(szRootName, szPath);
885 _tcscat(szRootName, _T("\\"));
886 }
887 else
888 {
889 *p = 0;
890 _tcscpy(szRootName, szPath);
891 _tcscat(szRootName, _T("\\"));
892 *p = _T('\\');
893 }
894 }
895 else
896 {
897 error_invalid_drive();
898 return(FALSE);
899 }
900
901 /* get the media ID of the drive */
902 if (!GetVolumeInformation(szRootName, szVolName, 80, &dwSerialNr,
903 NULL, NULL, NULL, 0))
904 {
905 error_invalid_drive();
906 return(FALSE);
907 }
908
909 /* print drive info */
910 if (szVolName[0] != _T('\0'))
911 {
912 LoadString(CMD_ModuleHandle, STRING_DIR_HELP2, szMsg, RC_STRING_MAX_SIZE);
913 //needs to have first paramter as TRUE because
914 //this is the first output and need to clear the static
915 if(lpFlags->bPause)
916 ConOutPrintfPaging(TRUE,szMsg, szRootName[0], szVolName);
917 else
918 ConOutPrintf(szMsg, szRootName[0], szVolName);
919
920 }
921 else
922 {
923 LoadString(CMD_ModuleHandle, STRING_DIR_HELP3, szMsg, RC_STRING_MAX_SIZE);
924 if(lpFlags->bPause)
925 ConOutPrintfPaging(TRUE,szMsg, szRootName[0]);
926 else
927 ConOutPrintf(szMsg, szRootName[0]);
928 }
929
930 /* print the volume serial number if the return was successful */
931 LoadString(CMD_ModuleHandle, STRING_DIR_HELP4, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
932 if(lpFlags->bPause)
933 ConOutPrintfPaging(FALSE,szMsg,
934 HIWORD(dwSerialNr),
935 LOWORD(dwSerialNr));
936 else
937 ConOutPrintf(szMsg,
938 HIWORD(dwSerialNr),
939 LOWORD(dwSerialNr));
940
941
942 return TRUE;
943 }
944
945
946 /*
947 * convert
948 *
949 * insert commas into a number
950 *
951 */
952 #if 0
953 static INT
954 ConvertULong (ULONG num, LPTSTR des, INT len)
955 {
956 TCHAR temp[32];
957 INT c = 0;
958 INT n = 0;
959
960 if (num == 0)
961 {
962 des[0] = _T('0');
963 des[1] = _T('\0');
964 n = 1;
965 }
966 else
967 {
968 temp[31] = 0;
969 while (num > 0)
970 {
971 if (((c + 1) % (nNumberGroups + 1)) == 0)
972 temp[30 - c++] = cThousandSeparator;
973 temp[30 - c++] = (TCHAR)(num % 10) + _T('0');
974 num /= 10;
975 }
976
977 for (n = 0; n <= c; n++)
978 des[n] = temp[31 - c + n];
979 }
980
981 return n;
982 }
983 #endif
984
985 static VOID
986 DirPrintFileDateTime(TCHAR *lpDate,
987 TCHAR *lpTime,
988 LPWIN32_FIND_DATA lpFile,
989 LPDIRSWITCHFLAGS lpFlags)
990 {
991 FILETIME ft;
992 SYSTEMTIME dt;
993 TCHAR szDate[30];
994 TCHAR szTime[30];
995 WORD wYear;
996
997 /* Select the right time field */
998 switch (lpFlags->stTimeField.eTimeField)
999 {
1000 case TF_CREATIONDATE:
1001 if (!FileTimeToLocalFileTime(&lpFile->ftCreationTime, &ft))
1002 return;
1003 FileTimeToSystemTime(&ft, &dt);
1004 break;
1005
1006 case TF_LASTACCESSEDDATE :
1007 if (!FileTimeToLocalFileTime(&lpFile->ftLastAccessTime, &ft))
1008 return;
1009 FileTimeToSystemTime(&ft, &dt);
1010 break;
1011
1012 case TF_MODIFIEDDATE:
1013 if (!FileTimeToLocalFileTime(&lpFile->ftLastWriteTime, &ft))
1014 return;
1015 FileTimeToSystemTime(&ft, &dt);
1016 break;
1017 }
1018
1019 /* Format date */
1020 wYear = (lpFlags->b4Digit) ? dt.wYear : dt.wYear%100;
1021 switch (nDateFormat)
1022 {
1023 case 0: /* mmddyy */
1024 default:
1025 _stprintf (szDate, _T("%02d%c%02d%c%0*d"),
1026 dt.wMonth, cDateSeparator,
1027 dt.wDay, cDateSeparator,
1028 lpFlags->b4Digit?4:2, wYear);
1029 break;
1030
1031 case 1: /* ddmmyy */
1032 _stprintf (szDate, _T("%02d%c%02d%c%0*d"),
1033 dt.wDay, cDateSeparator, dt.wMonth,
1034 cDateSeparator,lpFlags->b4Digit?4:2, wYear);
1035 break;
1036
1037 case 2: /* yymmdd */
1038 _stprintf (szDate, _T("%0*d%c%02d%c%02d"),
1039 lpFlags->b4Digit?4:2, wYear, cDateSeparator,
1040 dt.wMonth, cDateSeparator, dt.wDay);
1041 break;
1042 }
1043 /* Format Time */
1044 switch (nTimeFormat)
1045 {
1046 case 0: /* 12 hour format */
1047 default:
1048 _stprintf (szTime,_T(" %02d%c%02u%c"),
1049 (dt.wHour == 0 ? 12 : (dt.wHour <= 12 ? dt.wHour : dt.wHour - 12)),
1050 cTimeSeparator,
1051 dt.wMinute, (dt.wHour <= 11 ? _T('a') : _T('p')));
1052 break;
1053
1054 case 1: /* 24 hour format */
1055 _stprintf (szTime, _T(" %02d%c%02u"),
1056 dt.wHour, cTimeSeparator, dt.wMinute);
1057 break;
1058 }
1059 /* Copy results */
1060 _tcscpy(lpDate, szDate);
1061 _tcscpy(lpTime, szTime);
1062 }
1063
1064
1065 static VOID
1066 GetUserDiskFreeSpace(LPCTSTR lpRoot,
1067 PULARGE_INTEGER lpFreeSpace)
1068 {
1069 PGETFREEDISKSPACEEX pGetFreeDiskSpaceEx;
1070 HINSTANCE hInstance;
1071 DWORD dwSecPerCl;
1072 DWORD dwBytPerSec;
1073 DWORD dwFreeCl;
1074 DWORD dwTotCl;
1075 ULARGE_INTEGER TotalNumberOfBytes, TotalNumberOfFreeBytes;
1076
1077 lpFreeSpace->QuadPart = 0;
1078
1079 hInstance = LoadLibrary(_T("KERNEL32"));
1080 if (hInstance != NULL)
1081 {
1082 pGetFreeDiskSpaceEx = (PGETFREEDISKSPACEEX)GetProcAddress(hInstance,
1083 #ifdef _UNICODE
1084 "GetDiskFreeSpaceExW");
1085 #else
1086 "GetDiskFreeSpaceExA");
1087 #endif
1088 if (pGetFreeDiskSpaceEx != NULL)
1089 {
1090 if (pGetFreeDiskSpaceEx(lpRoot, lpFreeSpace, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == TRUE)
1091 return;
1092 }
1093 FreeLibrary(hInstance);
1094 }
1095
1096 GetDiskFreeSpace(lpRoot,
1097 &dwSecPerCl,
1098 &dwBytPerSec,
1099 &dwFreeCl,
1100 &dwTotCl);
1101
1102 lpFreeSpace->QuadPart = dwSecPerCl * dwBytPerSec * dwFreeCl;
1103 }
1104
1105
1106 /*
1107 * print_summary: prints dir summary
1108 * Added by Rob Lake 06/17/98 to compact code
1109 * Just copied Tim's Code and patched it a bit
1110 *
1111 */
1112 static INT
1113 PrintSummary(LPTSTR szPath,
1114 ULONG ulFiles,
1115 ULONG ulDirs,
1116 ULARGE_INTEGER u64Bytes,
1117 LPINT pLine,
1118 LPDIRSWITCHFLAGS lpFlags)
1119 {
1120 TCHAR szMsg[RC_STRING_MAX_SIZE];
1121 TCHAR szBuffer[64];
1122 ULARGE_INTEGER uliFree;
1123 TCHAR szRoot[] = _T("A:\\");
1124
1125
1126 /* Here we check if we didn't find anything */
1127 if (!(ulFiles + ulDirs))
1128 {
1129 error_file_not_found();
1130 return 1;
1131 }
1132
1133 /* In bare format we don't print results */
1134 if (lpFlags->bBareFormat)
1135 return 0;
1136
1137 /* Print recursive specific results */
1138 if (lpFlags->bRecursive)
1139 {
1140 ConvertULargeInteger(u64Bytes, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
1141
1142 LoadString(CMD_ModuleHandle, STRING_DIR_HELP5, szMsg, RC_STRING_MAX_SIZE);
1143 if(lpFlags->bPause)
1144 ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
1145 else
1146 ConOutPrintf(szMsg,ulFiles, szBuffer);
1147 }
1148
1149 /* Print File Summary */
1150 /* Condition to print summary is:
1151 If we are not in bare format and if we have results! */
1152 if (ulFiles > 0)
1153 {
1154 ConvertULargeInteger(u64Bytes, szBuffer, 20, lpFlags->bTSeperator);
1155 LoadString(CMD_ModuleHandle, STRING_DIR_HELP8, szMsg, RC_STRING_MAX_SIZE);
1156 if(lpFlags->bPause)
1157 ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
1158 else
1159 ConOutPrintf(szMsg,ulFiles, szBuffer);
1160
1161 }
1162
1163 /* Print total directories and freespace */
1164 szRoot[0] = szPath[0];
1165 GetUserDiskFreeSpace(szRoot, &uliFree);
1166 ConvertULargeInteger(uliFree, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
1167 LoadString(CMD_ModuleHandle, STRING_DIR_HELP6, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
1168 if(lpFlags->bPause)
1169 ConOutPrintfPaging(FALSE,szMsg,ulDirs, szBuffer);
1170 else
1171 ConOutPrintf(szMsg,ulDirs, szBuffer);
1172
1173 return 0;
1174 }
1175
1176 /*
1177 * getExt
1178 *
1179 * Get the extension of a filename
1180 */
1181 TCHAR* getExt(const TCHAR* file)
1182 {
1183 static TCHAR *NoExt = _T("");
1184 TCHAR* lastdot = _tcsrchr(file, _T('.'));
1185 return (lastdot != NULL ? lastdot + 1 : NoExt);
1186 }
1187
1188 /*
1189 * getName
1190 *
1191 * Get the name of the file without extension
1192 */
1193 static LPTSTR
1194 getName(const TCHAR* file, TCHAR * dest)
1195 {
1196 int iLen;
1197 LPTSTR end;
1198
1199 /* Check for "." and ".." folders */
1200 if ((_tcscmp(file, _T(".")) == 0) ||
1201 (_tcscmp(file, _T("..")) == 0))
1202 {
1203 _tcscpy(dest,file);
1204 return dest;
1205 }
1206
1207 end = _tcsrchr(file, _T('.'));
1208 if (!end)
1209 iLen = _tcslen(file);
1210 else
1211 iLen = (end - file);
1212
1213
1214 _tcsncpy(dest, file, iLen);
1215 *(dest + iLen) = _T('\0');
1216
1217 return dest;
1218 }
1219
1220
1221 /*
1222 * DirPrintNewList
1223 *
1224 * The function that prints in new style
1225 */
1226 static VOID
1227 DirPrintNewList(LPWIN32_FIND_DATA ptrFiles[], /* [IN]Files' Info */
1228 DWORD dwCount, /* [IN] The quantity of files */
1229 TCHAR *szCurPath, /* [IN] Full path of current directory */
1230 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1231 {
1232 DWORD i;
1233 TCHAR szSize[30];
1234 TCHAR szShortName[15];
1235 TCHAR szDate[20];
1236 TCHAR szTime[20];
1237 INT iSizeFormat;
1238 ULARGE_INTEGER u64FileSize;
1239
1240 for (i = 0;i < dwCount;i++)
1241 {
1242 /* Calculate size */
1243 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1244 {
1245 /* Directory */
1246 iSizeFormat = -14;
1247 _tcscpy(szSize, _T("<DIR>"));
1248 }
1249 else
1250 {
1251 /* File */
1252 iSizeFormat = 14;
1253 u64FileSize.HighPart = ptrFiles[i]->nFileSizeHigh;
1254 u64FileSize.LowPart = ptrFiles[i]->nFileSizeLow;
1255 ConvertULargeInteger(u64FileSize, szSize, 20, lpFlags->bTSeperator);
1256 }
1257
1258 /* Calculate short name */
1259 szShortName[0] = _T('\0');
1260 if (lpFlags->bShortName)
1261 _stprintf(szShortName, _T(" %-12s"), ptrFiles[i]->cAlternateFileName);
1262
1263 /* Format date and time */
1264 DirPrintFileDateTime(szDate, szTime, ptrFiles[i], lpFlags);
1265
1266 /* Print the line */
1267 if(lpFlags->bPause)
1268 ConOutPrintfPaging(FALSE,_T("%10s %-8s %*s%s %s\n"),
1269 szDate,
1270 szTime,
1271 iSizeFormat,
1272 szSize,
1273 szShortName,
1274 ptrFiles[i]->cFileName);
1275 else
1276 ConOutPrintf(_T("%10s %-8s %*s%s %s\n"),
1277 szDate,
1278 szTime,
1279 iSizeFormat,
1280 szSize,
1281 szShortName,
1282 ptrFiles[i]->cFileName);
1283 }
1284 }
1285
1286
1287 /*
1288 * DirPrintWideList
1289 *
1290 * The function that prints in wide list
1291 */
1292 static VOID
1293 DirPrintWideList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1294 DWORD dwCount, /* [IN] The quantity of files */
1295 TCHAR *szCurPath, /* [IN] Full path of current directory */
1296 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1297 {
1298 SHORT iScreenWidth;
1299 USHORT iColumns;
1300 USHORT iLines;
1301 UINT iLongestName;
1302 TCHAR szTempFname[MAX_PATH];
1303 DWORD i;
1304 DWORD j;
1305 DWORD temp;
1306
1307 /* Calculate longest name */
1308 iLongestName = 1;
1309 for (i = 0; i < dwCount; i++)
1310 {
1311 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1312 {
1313 /* Directories need 2 additinal characters for brackets */
1314 if ((_tcslen(ptrFiles[i]->cFileName) + 2) > iLongestName)
1315 iLongestName = _tcslen(ptrFiles[i]->cFileName) + 2;
1316 }
1317 else
1318 {
1319 if (_tcslen(ptrFiles[i]->cFileName) > iLongestName)
1320 iLongestName = _tcslen(ptrFiles[i]->cFileName);
1321 }
1322 }
1323
1324 /* Count the highest number of columns */
1325 GetScreenSize(&iScreenWidth, 0);
1326 iColumns = iScreenWidth / iLongestName;
1327
1328 /* Check if there is enough space for spaces between names */
1329 if (((iLongestName * iColumns) + iColumns) >= (UINT)iScreenWidth)
1330 iColumns --;
1331
1332 /* A last check at iColumns to avoid division by zero */
1333 if (!(iColumns))
1334 iColumns = 1;
1335
1336 /* Print Column sorted */
1337 if (lpFlags->bWideListColSort)
1338 {
1339 /* Calculate the lines that will be printed */
1340 // iLines = ceil((float)dwCount/(float)iColumns);
1341 iLines = (USHORT)(dwCount / iColumns);
1342
1343 for (i = 0;i < iLines;i++)
1344 {
1345 for (j = 0; j < iColumns; j++)
1346 {
1347 temp = (j * iLines) + i;
1348 if (temp >= dwCount)
1349 break;
1350
1351 if (ptrFiles[temp]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1352 _stprintf(szTempFname, _T("[%s]"), ptrFiles[temp]->cFileName);
1353 else
1354 _stprintf(szTempFname, _T("%s"), ptrFiles[temp]->cFileName);
1355
1356 if(lpFlags->bPause)
1357 ConOutPrintfPaging(FALSE,_T("%-*s"), iLongestName + 1 , szTempFname);
1358 else
1359 ConOutPrintf(_T("%-*s"), iLongestName + 1 , szTempFname);
1360 }
1361
1362 if(lpFlags->bPause)
1363 ConOutPrintfPaging(FALSE,_T("\n"));
1364 else
1365 ConOutPrintf(_T("\n"));
1366 }
1367 }
1368 else
1369 {
1370 /* Print Line sorted */
1371 for (i = 0; i < dwCount; i++)
1372 {
1373 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1374 _stprintf(szTempFname, _T("[%s]"), ptrFiles[i]->cFileName);
1375 else
1376 _stprintf(szTempFname, _T("%s"), ptrFiles[i]->cFileName);
1377
1378 if(lpFlags->bPause)
1379 ConOutPrintfPaging(FALSE,_T("%-*s"), iLongestName + 1, szTempFname );
1380 else
1381 ConOutPrintf(_T("%-*s"), iLongestName + 1, szTempFname );
1382
1383 /*
1384 * We print a new line at the end of each column
1385 * except for the case that it is the last item.
1386 */
1387 if (!((i + 1) % iColumns) && (i < (dwCount - 1)))
1388 {
1389 if(lpFlags->bPause)
1390 ConOutPrintfPaging(FALSE,_T("\n"));
1391 else
1392 ConOutPrintf(_T("\n"));
1393 }
1394 }
1395
1396 /* Add a new line after the last item */
1397 if(lpFlags->bPause)
1398 ConOutPrintfPaging(FALSE,_T("\n"));
1399 else
1400 ConOutPrintf(_T("\n"));
1401 }
1402 }
1403
1404
1405 /*
1406 * DirPrintOldList
1407 *
1408 * The function that prints in old style
1409 */
1410 static VOID
1411 DirPrintOldList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1412 DWORD dwCount, /* [IN] The quantity of files */
1413 TCHAR * szCurPath, /* [IN] Full path of current directory */
1414 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1415 {
1416 DWORD i; /* An indexer for "for"s */
1417 TCHAR szName[10]; /* The name of file */
1418 TCHAR szExt[5]; /* The extension of file */
1419 TCHAR szDate[30],szTime[30]; /* Used to format time and date */
1420 TCHAR szSize[30]; /* The size of file */
1421 int iSizeFormat; /* The format of size field */
1422 ULARGE_INTEGER u64FileSize; /* The file size */
1423
1424 for(i = 0;i < dwCount;i++)
1425 {
1426 /* Broke 8.3 format */
1427 if (*ptrFiles[i]->cAlternateFileName )
1428 {
1429 /* If the file is long named then we read the alter name */
1430 getName( ptrFiles[i]->cAlternateFileName, szName);
1431 _tcscpy(szExt, getExt( ptrFiles[i]->cAlternateFileName));
1432 }
1433 else
1434 {
1435 /* If the file is not long name we read its original name */
1436 getName( ptrFiles[i]->cFileName, szName);
1437 _tcscpy(szExt, getExt( ptrFiles[i]->cFileName));
1438 }
1439
1440 /* Calculate size */
1441 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1442 {
1443 /* Directory, no size it's a directory*/
1444 iSizeFormat = -17;
1445 _tcscpy(szSize, _T("<DIR>"));
1446 }
1447 else
1448 {
1449 /* File */
1450 iSizeFormat = 17;
1451 u64FileSize.HighPart = ptrFiles[i]->nFileSizeHigh;
1452 u64FileSize.LowPart = ptrFiles[i]->nFileSizeLow;
1453 ConvertULargeInteger(u64FileSize, szSize, 20, lpFlags->bTSeperator);
1454 }
1455
1456 /* Format date and time */
1457 DirPrintFileDateTime(szDate,szTime,ptrFiles[i],lpFlags);
1458
1459 /* Print the line */
1460 if(lpFlags->bPause)
1461 ConOutPrintfPaging(FALSE,_T("%-8s %-3s %*s %s %s\n"),
1462 szName, /* The file's 8.3 name */
1463 szExt, /* The file's 8.3 extension */
1464 iSizeFormat, /* print format for size column */
1465 szSize, /* The size of file or "<DIR>" for dirs */
1466 szDate, /* The date of file/dir */
1467 szTime); /* The time of file/dir */
1468 else
1469 ConOutPrintf(_T("%-8s %-3s %*s %s %s\n"),
1470 szName, /* The file's 8.3 name */
1471 szExt, /* The file's 8.3 extension */
1472 iSizeFormat, /* print format for size column */
1473 szSize, /* The size of file or "<DIR>" for dirs */
1474 szDate, /* The date of file/dir */
1475 szTime); /* The time of file/dir */
1476 }
1477 }
1478
1479 /*
1480 * DirPrintBareList
1481 *
1482 * The function that prints in bare format
1483 */
1484 static VOID
1485 DirPrintBareList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1486 DWORD dwCount, /* [IN] The number of files */
1487 LPTSTR lpCurPath, /* [IN] Full path of current directory */
1488 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1489 {
1490 TCHAR szFullName[MAX_PATH];
1491 DWORD i;
1492
1493 for (i = 0; i < dwCount; i++)
1494 {
1495 if ((_tcscmp(ptrFiles[i]->cFileName, _T(".")) == 0) ||
1496 (_tcscmp(ptrFiles[i]->cFileName, _T("..")) == 0))
1497 {
1498 /* at bare format we don't print "." and ".." folder */
1499 continue;
1500 }
1501 if (lpFlags->bRecursive)
1502 {
1503 /* at recursive mode we print full path of file */
1504 _tcscpy(szFullName, lpCurPath);
1505 _tcscat(szFullName, ptrFiles[i]->cFileName);
1506 if(lpFlags->bPause)
1507 ConOutPrintfPaging(FALSE,_T("%s\n"), szFullName);
1508 else
1509 ConOutPrintf(_T("%s\n"), szFullName);
1510 }
1511 else
1512 {
1513 /* if we are not in recursive mode we print the file names */
1514 if(lpFlags->bPause)
1515 ConOutPrintfPaging(FALSE,_T("%s\n"),ptrFiles[i]->cFileName);
1516 else
1517 ConOutPrintf(_T("%s\n"),ptrFiles[i]->cFileName);
1518 }
1519 }
1520 }
1521
1522
1523 /*
1524 * DirPrintFiles
1525 *
1526 * The functions that prints the files list
1527 */
1528 static VOID
1529 DirPrintFiles(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1530 DWORD dwCount, /* [IN] The quantity of files */
1531 TCHAR *szCurPath, /* [IN] Full path of current directory */
1532 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1533 {
1534 TCHAR szMsg[RC_STRING_MAX_SIZE];
1535 TCHAR szTemp[MAX_PATH]; /* A buffer to format the directory header */
1536
1537 /* Print directory header */
1538 _tcscpy(szTemp, szCurPath);
1539
1540 /* We cut the trailing \ of the full path */
1541 szTemp[_tcslen(szTemp)-1] = _T('\0');
1542
1543 /* Condition to print header:
1544 We are not printing in bare format
1545 and if we are in recursive mode... we must have results */
1546 if (!(lpFlags->bBareFormat ) && !((lpFlags->bRecursive) && (dwCount <= 0)))
1547 {
1548 LoadString(CMD_ModuleHandle, STRING_DIR_HELP7, szMsg, RC_STRING_MAX_SIZE);
1549 if(lpFlags->bPause)
1550 ConOutPrintfPaging(FALSE,szMsg, szTemp);
1551 else
1552 ConOutPrintf(szMsg, szTemp);
1553 }
1554
1555 if (lpFlags->bBareFormat)
1556 {
1557 /* Bare format */
1558 DirPrintBareList(ptrFiles, dwCount, szCurPath, lpFlags);
1559 }
1560 else if(lpFlags->bShortName)
1561 {
1562 /* New list style / Short names */
1563 DirPrintNewList(ptrFiles, dwCount, szCurPath, lpFlags);
1564 }
1565 else if(lpFlags->bWideListColSort || lpFlags->bWideList)
1566 {
1567 /* Wide list */
1568 DirPrintWideList(ptrFiles, dwCount, szCurPath, lpFlags);
1569 }
1570 else if (lpFlags->bNewLongList )
1571 {
1572 /* New list style*/
1573 DirPrintNewList(ptrFiles, dwCount, szCurPath, lpFlags);
1574 }
1575 else
1576 {
1577 /* If nothing is selected old list is the default */
1578 DirPrintOldList(ptrFiles, dwCount, szCurPath, lpFlags);
1579 }
1580 }
1581
1582
1583
1584 /*
1585 * CompareFiles
1586 *
1587 * Compares 2 files based on the order criteria
1588 */
1589 static BOOL
1590 CompareFiles(LPWIN32_FIND_DATA lpFile1, /* [IN] A pointer to WIN32_FIND_DATA of file 1 */
1591 LPWIN32_FIND_DATA lpFile2, /* [IN] A pointer to WIN32_FIND_DATA of file 2 */
1592 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags that we use to list */
1593 {
1594 ULARGE_INTEGER u64File1;
1595 ULARGE_INTEGER u64File2;
1596 int i;
1597 long iComp = 0; /* The comparison result */
1598
1599 /* Calculate critiries by order given from user */
1600 for (i = 0;i < lpFlags->stOrderBy.sCriteriaCount;i++)
1601 {
1602
1603 /* Calculate criteria */
1604 switch(lpFlags->stOrderBy.eCriteria[i])
1605 {
1606 case ORDER_SIZE: /* Order by size /o:s */
1607 /* concat the 32bit integers to a 64bit */
1608 u64File1.LowPart = lpFile1->nFileSizeLow;
1609 u64File1.HighPart = lpFile1->nFileSizeHigh;
1610 u64File2.LowPart = lpFile2->nFileSizeLow;
1611 u64File2.HighPart = lpFile2->nFileSizeHigh;
1612
1613 /* In case that differnce is too big for a long */
1614 if (u64File1.QuadPart < u64File2.QuadPart)
1615 iComp = -1;
1616 else if (u64File1.QuadPart > u64File2.QuadPart)
1617 iComp = 1;
1618 else
1619 iComp = 0;
1620 break;
1621
1622 case ORDER_DIRECTORY: /* Order by directory attribute /o:g */
1623 iComp = ((lpFile2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)-
1624 (lpFile1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
1625 break;
1626
1627 case ORDER_EXTENSION: /* Order by extension name /o:e */
1628 iComp = _tcsicmp(getExt(lpFile1->cFileName),getExt(lpFile2->cFileName));
1629 break;
1630
1631 case ORDER_NAME: /* Order by filename /o:n */
1632 iComp = _tcsicmp(lpFile1->cFileName, lpFile2->cFileName);
1633 break;
1634
1635 case ORDER_TIME: /* Order by file's time /o:t */
1636 /* We compare files based on the time field selected by /t */
1637 switch(lpFlags->stTimeField.eTimeField)
1638 {
1639 case TF_CREATIONDATE:
1640 /* concat the 32bit integers to a 64bit */
1641 u64File1.LowPart = lpFile1->ftCreationTime.dwLowDateTime;
1642 u64File1.HighPart = lpFile1->ftCreationTime.dwHighDateTime ;
1643 u64File2.LowPart = lpFile2->ftCreationTime.dwLowDateTime;
1644 u64File2.HighPart = lpFile2->ftCreationTime.dwHighDateTime ;
1645 break;
1646 case TF_LASTACCESSEDDATE :
1647 /* concat the 32bit integers to a 64bit */
1648 u64File1.LowPart = lpFile1->ftLastAccessTime.dwLowDateTime;
1649 u64File1.HighPart = lpFile1->ftLastAccessTime.dwHighDateTime ;
1650 u64File2.LowPart = lpFile2->ftLastAccessTime.dwLowDateTime;
1651 u64File2.HighPart = lpFile2->ftLastAccessTime.dwHighDateTime ;
1652 break;
1653 case TF_MODIFIEDDATE:
1654 /* concat the 32bit integers to a 64bit */
1655 u64File1.LowPart = lpFile1->ftLastWriteTime.dwLowDateTime;
1656 u64File1.HighPart = lpFile1->ftLastWriteTime.dwHighDateTime ;
1657 u64File2.LowPart = lpFile2->ftLastWriteTime.dwLowDateTime;
1658 u64File2.HighPart = lpFile2->ftLastWriteTime.dwHighDateTime ;
1659 break;
1660 }
1661
1662 /* In case that differnce is too big for a long */
1663 if (u64File1.QuadPart < u64File2.QuadPart)
1664 iComp = -1;
1665 else if (u64File1.QuadPart > u64File2.QuadPart)
1666 iComp = 1;
1667 else
1668 iComp = 0;
1669 break;
1670 }
1671
1672 /* Reverse if desired */
1673 if (lpFlags->stOrderBy.bCriteriaRev[i])
1674 iComp *= -1;
1675
1676 /* If that criteria was enough for distinguishing
1677 the files/dirs,there is no need to calculate the others*/
1678 if (iComp != 0) break;
1679 }
1680
1681 /* Translate the value of iComp to boolean */
1682 if (iComp > 0)
1683 return TRUE;
1684 else
1685 return FALSE;
1686 }
1687
1688 /*
1689 * QsortFiles
1690 *
1691 * Sort files by the order criterias using quicksort method
1692 */
1693 static VOID
1694 QsortFiles(LPWIN32_FIND_DATA ptrArray[], /* [IN/OUT] The array with file info pointers */
1695 int i, /* [IN] The index of first item in array */
1696 int j, /* [IN] The index to last item in array */
1697 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags that we will use to sort */
1698 {
1699 LPWIN32_FIND_DATA lpTemp; /* A temporary pointer */
1700 int First, Last, Temp;
1701 BOOL Way;
1702
1703 if (i < j)
1704 {
1705 First = i;
1706 Last = j;
1707 Way = TRUE;
1708 while (i != j)
1709 {
1710 if (Way == CompareFiles(ptrArray[i], ptrArray[j], lpFlags))
1711 {
1712 /* Swap the pointers of the array */
1713 lpTemp = ptrArray[i];
1714 ptrArray[i]= ptrArray[j];
1715 ptrArray[j] = lpTemp;
1716
1717 /* Swap the indexes for inverting sorting */
1718 Temp = i;
1719 i = j;
1720 j =Temp;
1721
1722 Way = !Way;
1723 }
1724
1725 j += (!Way - Way);
1726 }
1727
1728 QsortFiles(ptrArray,First, i-1, lpFlags);
1729 QsortFiles(ptrArray,i+1,Last, lpFlags);
1730 }
1731 }
1732
1733
1734
1735 /*
1736 * DirList
1737 *
1738 * The functions that does everything except for printing results
1739 */
1740 static INT
1741 DirList(LPTSTR szPath, /* [IN] The path that dir starts */
1742 LPTSTR szFilespec, /* [IN] The type of file that we are looking for */
1743 LPINT pLine, /* FIXME: Maybe used for paginating */
1744 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags of the listing */
1745 {
1746 HANDLE hSearch; /* The handle of the search */
1747 HANDLE hRecSearch; /* The handle for searching recursivly */
1748 WIN32_FIND_DATA wfdFileInfo; /* The info of file that found */
1749 LPWIN32_FIND_DATA * ptrFileArray; /* An array of pointers with all the files */
1750 PDIRFINDLISTNODE ptrStartNode; /* The pointer to the first node */
1751 PDIRFINDLISTNODE ptrNextNode; /* A pointer used for relatives refernces */
1752 TCHAR szFullPath[MAX_PATH]; /* The full path that we are listing with trailing \ */
1753 TCHAR szFullFileSpec[MAX_PATH]; /* The full path with file specs that we ll request\ */
1754 DWORD dwCount; /* A counter of files found in directory */
1755 DWORD dwCountFiles; /* Counter for files */
1756 DWORD dwCountDirs; /* Counter for directories */
1757 ULARGE_INTEGER u64CountBytes; /* Counter for bytes */
1758 ULARGE_INTEGER u64Temp; /* A temporary counter */
1759
1760 /* Initialize Variables */
1761 ptrStartNode = NULL;
1762 ptrNextNode = NULL;
1763 dwCount = 0;
1764 dwCountFiles = 0;
1765 dwCountDirs = 0;
1766 u64CountBytes.QuadPart = 0;
1767
1768 /* Create szFullPath and szFullFileSpec */
1769 _tcscpy (szFullPath, szPath);
1770 if (szFullPath[_tcslen(szFullPath) - 1] != _T('\\'))
1771 _tcscat (szFullPath, _T("\\"));
1772 _tcscpy (szFullFileSpec, szFullPath);
1773 _tcscat (szFullFileSpec, szFilespec);
1774
1775 /* Prepare the linked list, first node is allocated */
1776 ptrStartNode = malloc(sizeof(DIRFINDLISTNODE));
1777 if (ptrStartNode == NULL)
1778 {
1779 #ifdef _DEBUG
1780 ConErrPrintf(_T("DEBUG: Cannot allocate memory for ptrStartNode!\n"));
1781 #endif
1782 return 1; /* Error cannot allocate memory for 1st object */
1783 }
1784 ptrNextNode = ptrStartNode;
1785
1786 /* Collect the results for the current folder */
1787 hSearch = FindFirstFile(szFullFileSpec, &wfdFileInfo);
1788 do
1789 {
1790 if (hSearch != INVALID_HANDLE_VALUE)
1791 {
1792 /* Here we filter all the specified attributes */
1793 if ((wfdFileInfo.dwFileAttributes & lpFlags->stAttribs.dwAttribMask )
1794 == (lpFlags->stAttribs.dwAttribMask & lpFlags->stAttribs.dwAttribVal ))
1795 {
1796 ptrNextNode->ptrNext = malloc(sizeof(DIRFINDLISTNODE));
1797 if (ptrNextNode->ptrNext == NULL)
1798 {
1799 #ifdef _DEBUG
1800 ConErrPrintf(_T("DEBUG: Cannot allocate memory for ptrNextNode->ptrNext!\n"));
1801 #endif
1802 while (ptrStartNode)
1803 {
1804 ptrNextNode = ptrStartNode->ptrNext;
1805 free(ptrStartNode);
1806 ptrStartNode = ptrNextNode;
1807 dwCount --;
1808 }
1809 return 1;
1810 }
1811
1812 /* If malloc fails we go to next file in hope it works,
1813 without braking the linked list! */
1814 if (ptrNextNode->ptrNext)
1815 {
1816 /* Copy the info of search at linked list */
1817 memcpy(&ptrNextNode->ptrNext->stFindInfo,
1818 &wfdFileInfo,
1819 sizeof(WIN32_FIND_DATA));
1820
1821 /* If lower case is selected do it here */
1822 if (lpFlags->bLowerCase)
1823 {
1824 _tcslwr(ptrNextNode->ptrNext->stFindInfo.cAlternateFileName);
1825 _tcslwr(ptrNextNode->ptrNext->stFindInfo.cFileName);
1826 }
1827
1828 /* Continue at next node at linked list */
1829 ptrNextNode = ptrNextNode->ptrNext;
1830 dwCount ++;
1831
1832 /* Grab statistics */
1833 if (wfdFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1834 {
1835 /* Directory */
1836 dwCountDirs++;
1837 }
1838 else
1839 {
1840 /* File */
1841 dwCountFiles++;
1842 u64Temp.HighPart = wfdFileInfo.nFileSizeHigh;
1843 u64Temp.LowPart = wfdFileInfo.nFileSizeLow;
1844 u64CountBytes.QuadPart += u64Temp.QuadPart;
1845 }
1846 }
1847 }
1848 }
1849 }while(FindNextFile(hSearch, &wfdFileInfo));
1850 FindClose(hSearch);
1851
1852 /* Terminate list */
1853 ptrNextNode->ptrNext = NULL;
1854
1855 /* Calculate and allocate space need for making an array of pointers */
1856 ptrFileArray = malloc(sizeof(LPWIN32_FIND_DATA) * dwCount);
1857 if (ptrFileArray == NULL)
1858 {
1859 #ifdef _DEBUG
1860 ConErrPrintf(_T("DEBUG: Cannot allocate memory for ptrFileArray!\n"));
1861 #endif
1862 while (ptrStartNode)
1863 {
1864 ptrNextNode = ptrStartNode->ptrNext;
1865 free(ptrStartNode);
1866 ptrStartNode = ptrNextNode;
1867 dwCount --;
1868 }
1869 return 1;
1870 }
1871
1872 /*
1873 * Create an array of pointers from the linked list
1874 * this will be used to sort and print data, rather than the list
1875 */
1876 ptrNextNode = ptrStartNode;
1877 dwCount = 0;
1878 while (ptrNextNode->ptrNext)
1879 {
1880 *(ptrFileArray + dwCount) = &ptrNextNode->ptrNext->stFindInfo;
1881 ptrNextNode = ptrNextNode->ptrNext;
1882 dwCount++;
1883 }
1884
1885 /* Sort Data if requested*/
1886 if (lpFlags->stOrderBy.sCriteriaCount > 0)
1887 QsortFiles(ptrFileArray, 0, dwCount-1,lpFlags);
1888
1889 /* Print Data */
1890 DirPrintFiles(ptrFileArray, dwCount, szFullPath, lpFlags);
1891
1892 /* Free array */
1893 free(ptrFileArray);
1894
1895 /* Add statistics to recursive statistics*/
1896 recurse_dir_cnt += dwCountDirs;
1897 recurse_file_cnt += dwCountFiles;
1898 recurse_bytes.QuadPart += u64CountBytes.QuadPart;
1899
1900 /* Do the recursive job if requested
1901 the recursive is be done on ALL(indepent of their attribs)
1902 directoried of the current one.*/
1903 if (lpFlags->bRecursive)
1904 {
1905 /* The new search is involving any *.* file */
1906 _tcscpy(szFullFileSpec, szFullPath);
1907 _tcscat(szFullFileSpec, _T("*.*"));
1908 hRecSearch = FindFirstFile (szFullFileSpec, &wfdFileInfo);
1909 do
1910 {
1911 if (hRecSearch != INVALID_HANDLE_VALUE)
1912 {
1913 /* We search for directories other than "." and ".." */
1914 if ((_tcsicmp(wfdFileInfo.cFileName, _T(".")) != 0) &&
1915 (_tcsicmp(wfdFileInfo.cFileName, _T("..")) != 0 ) &&
1916 (wfdFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1917 {
1918 /* Concat the path and the directory to do recursive */
1919 _tcscpy(szFullFileSpec, szFullPath);
1920 _tcscat(szFullFileSpec, wfdFileInfo.cFileName);
1921 /* We do the same for tha folder */
1922 DirList(szFullFileSpec, szFilespec, pLine,lpFlags);
1923 }
1924 }
1925 }while(FindNextFile(hRecSearch,&wfdFileInfo));
1926 FindClose(hRecSearch);
1927 }
1928
1929 /* Free linked list */
1930 while (ptrStartNode)
1931 {
1932 ptrNextNode = ptrStartNode->ptrNext;
1933 free(ptrStartNode);
1934 ptrStartNode = ptrNextNode;
1935 dwCount --;
1936 }
1937
1938 return 0;
1939 }
1940
1941
1942
1943 /*
1944 * dir
1945 *
1946 * internal dir command
1947 */
1948 INT
1949 CommandDir(LPTSTR first, LPTSTR rest)
1950 {
1951 TCHAR dircmd[256]; /* A variable to store the DIRCMD enviroment variable */
1952 TCHAR cDrive;
1953 TCHAR szPath[MAX_PATH];
1954 TCHAR szFilespec[MAX_PATH];
1955 LPTSTR* params;
1956 INT entries = 0;
1957 INT nLine = 0;
1958 UINT loop = 0;
1959 DIRSWITCHFLAGS stFlags;
1960
1961 /* Initialize variables */
1962 cDrive = 0;
1963 recurse_dir_cnt = 0L;
1964 recurse_file_cnt = 0L;
1965 recurse_bytes.QuadPart = 0;
1966
1967 /* Initialize Switch Flags < Default switches are setted here!> */
1968 stFlags.b4Digit = TRUE;
1969 stFlags.bBareFormat = FALSE;
1970 stFlags.bLowerCase = FALSE;
1971 stFlags.bNewLongList = TRUE;
1972 stFlags.bPause = FALSE;
1973 stFlags.bRecursive = FALSE;
1974 stFlags.bShortName = FALSE;
1975 stFlags.bTSeperator = TRUE;
1976 stFlags.bUser = FALSE;
1977 stFlags.bWideList = FALSE;
1978 stFlags.bWideListColSort = FALSE;
1979 stFlags.stTimeField.eTimeField = TF_MODIFIEDDATE;
1980 stFlags.stTimeField.bUnSet = FALSE;
1981 stFlags.stAttribs.dwAttribMask = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
1982 stFlags.stAttribs.dwAttribVal = 0L;
1983 stFlags.stAttribs.bUnSet = FALSE;
1984 stFlags.stOrderBy.sCriteriaCount = 0;
1985 stFlags.stOrderBy.bUnSet = FALSE;
1986
1987 nErrorLevel = 0;
1988
1989 /* read the parameters from the DIRCMD environment variable */
1990 if (GetEnvironmentVariable (_T("DIRCMD"), dircmd, 256))
1991 if (!DirReadParam(dircmd, &params, &entries, &stFlags))
1992 {
1993 nErrorLevel = 1;
1994 return 1;
1995 }
1996
1997 /* read the parameters */
1998 if (!DirReadParam(rest, &params, &entries, &stFlags) || CheckCtrlBreak(BREAK_INPUT))
1999 {
2000 nErrorLevel = 1;
2001 return 1;
2002 }
2003
2004 /* default to current directory */
2005 if(entries == 0) {
2006 if(!add_entry(&entries, &params, _T("."))) {
2007 nErrorLevel = 1;
2008 return 1;
2009 }
2010 }
2011
2012 for(loop = 0; loop < entries; loop++)
2013 {
2014 /* parse the directory info */
2015 if (DirParsePathspec (params[loop], szPath, szFilespec) || CheckCtrlBreak(BREAK_INPUT))
2016 {
2017 nErrorLevel = 1;
2018 return 1;
2019 }
2020
2021 /* <Debug :>
2022 Uncomment this to show the final state of switch flags*/
2023 #ifdef _DEBUG
2024 {
2025 int i;
2026 ConOutPrintf(_T("Attributes mask/value %x/%x\n"),stFlags.stAttribs.dwAttribMask,stFlags.stAttribs.dwAttribVal );
2027 ConOutPrintf(_T("(B) Bare format : %i\n"), stFlags.bBareFormat );
2028 ConOutPrintf(_T("(C) Thousand : %i\n"), stFlags.bTSeperator );
2029 ConOutPrintf(_T("(W) Wide list : %i\n"), stFlags.bWideList );
2030 ConOutPrintf(_T("(D) Wide list sort by column : %i\n"), stFlags.bWideListColSort );
2031 ConOutPrintf(_T("(L) Lowercase : %i\n"), stFlags.bLowerCase );
2032 ConOutPrintf(_T("(N) New : %i\n"), stFlags.bNewLongList );
2033 ConOutPrintf(_T("(O) Order : %i\n"), stFlags.stOrderBy.sCriteriaCount );
2034 for (i =0;i<stFlags.stOrderBy.sCriteriaCount;i++)
2035 ConOutPrintf(_T(" Order Criteria [%i]: %i (Reversed: %i)\n"),i, stFlags.stOrderBy.eCriteria[i], stFlags.stOrderBy.bCriteriaRev[i] );
2036 ConOutPrintf(_T("(P) Pause : %i\n"), stFlags.bPause );
2037 ConOutPrintf(_T("(Q) Owner : %i\n"), stFlags.bUser );
2038 ConOutPrintf(_T("(S) Recursive : %i\n"), stFlags.bRecursive );
2039 ConOutPrintf(_T("(T) Time field : %i\n"), stFlags.stTimeField.eTimeField );
2040 ConOutPrintf(_T("(X) Short names : %i\n"), stFlags.bShortName );
2041 ConOutPrintf(_T("Parameter : %s\n"), params[loop] );
2042 }
2043 #endif
2044
2045 /* Print the drive header if the drive changed */
2046 if(cDrive != szPath[0] && !stFlags.bBareFormat) {
2047 if (!PrintDirectoryHeader (szPath, &nLine, &stFlags)) {
2048 nErrorLevel = 1;
2049 return 1;
2050 }
2051
2052 cDrive = szPath[0];
2053 }
2054
2055
2056 /* do the actual dir */
2057 if (DirList (szPath, szFilespec, &nLine, &stFlags))
2058 {
2059 nErrorLevel = 1;
2060 return 1;
2061 }
2062 }
2063
2064 /* print the footer */
2065 PrintSummary(szPath,
2066 recurse_file_cnt,
2067 recurse_dir_cnt,
2068 recurse_bytes,
2069 &nLine,
2070 &stFlags);
2071
2072 return 0;
2073 }
2074
2075 #endif
2076
2077 /* EOF */