take care of Bug#: 1084
[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
1134 /* In bare format we don't print results */
1135 if (lpFlags->bBareFormat)
1136 return 0;
1137
1138 /* Print recursive specific results */
1139
1140 /* Take this code offline to fix /S does not print duoble info */
1141 if (lpFlags->bRecursive)
1142 {
1143 ConvertULargeInteger(u64Bytes, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
1144
1145 LoadString(CMD_ModuleHandle, STRING_DIR_HELP5, szMsg, RC_STRING_MAX_SIZE);
1146 if(lpFlags->bPause)
1147 ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
1148 else
1149 ConOutPrintf(szMsg,ulFiles, szBuffer);
1150
1151 if (ulFiles > 0)
1152 {
1153 ConvertULargeInteger(u64Bytes, szBuffer, 20, lpFlags->bTSeperator);
1154 LoadString(CMD_ModuleHandle, STRING_DIR_HELP8, szMsg, RC_STRING_MAX_SIZE);
1155 if(lpFlags->bPause)
1156 ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
1157 else
1158 ConOutPrintf(szMsg,ulFiles, szBuffer);
1159
1160 }
1161
1162 }
1163 else
1164 {
1165
1166 /* Print File Summary */
1167 /* Condition to print summary is:
1168 If we are not in bare format and if we have results! */
1169 if (ulFiles > 0)
1170 {
1171 ConvertULargeInteger(u64Bytes, szBuffer, 20, lpFlags->bTSeperator);
1172 LoadString(CMD_ModuleHandle, STRING_DIR_HELP8, szMsg, RC_STRING_MAX_SIZE);
1173 if(lpFlags->bPause)
1174 ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
1175 else
1176 ConOutPrintf(szMsg,ulFiles, szBuffer);
1177
1178 }
1179
1180 }
1181 /* Print total directories and freespace */
1182 szRoot[0] = szPath[0];
1183 GetUserDiskFreeSpace(szRoot, &uliFree);
1184 ConvertULargeInteger(uliFree, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
1185 LoadString(CMD_ModuleHandle, STRING_DIR_HELP6, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
1186 if(lpFlags->bPause)
1187 ConOutPrintfPaging(FALSE,szMsg,ulDirs, szBuffer);
1188 else
1189 ConOutPrintf(szMsg,ulDirs, szBuffer);
1190
1191 return 0;
1192 }
1193
1194 /*
1195 * getExt
1196 *
1197 * Get the extension of a filename
1198 */
1199 TCHAR* getExt(const TCHAR* file)
1200 {
1201 static TCHAR *NoExt = _T("");
1202 TCHAR* lastdot = _tcsrchr(file, _T('.'));
1203 return (lastdot != NULL ? lastdot + 1 : NoExt);
1204 }
1205
1206 /*
1207 * getName
1208 *
1209 * Get the name of the file without extension
1210 */
1211 static LPTSTR
1212 getName(const TCHAR* file, TCHAR * dest)
1213 {
1214 int iLen;
1215 LPTSTR end;
1216
1217 /* Check for "." and ".." folders */
1218 if ((_tcscmp(file, _T(".")) == 0) ||
1219 (_tcscmp(file, _T("..")) == 0))
1220 {
1221 _tcscpy(dest,file);
1222 return dest;
1223 }
1224
1225 end = _tcsrchr(file, _T('.'));
1226 if (!end)
1227 iLen = _tcslen(file);
1228 else
1229 iLen = (end - file);
1230
1231
1232 _tcsncpy(dest, file, iLen);
1233 *(dest + iLen) = _T('\0');
1234
1235 return dest;
1236 }
1237
1238
1239 /*
1240 * DirPrintNewList
1241 *
1242 * The function that prints in new style
1243 */
1244 static VOID
1245 DirPrintNewList(LPWIN32_FIND_DATA ptrFiles[], /* [IN]Files' Info */
1246 DWORD dwCount, /* [IN] The quantity of files */
1247 TCHAR *szCurPath, /* [IN] Full path of current directory */
1248 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1249 {
1250 DWORD i;
1251 TCHAR szSize[30];
1252 TCHAR szShortName[15];
1253 TCHAR szDate[20];
1254 TCHAR szTime[20];
1255 INT iSizeFormat;
1256 ULARGE_INTEGER u64FileSize;
1257
1258 for (i = 0;i < dwCount;i++)
1259 {
1260 /* Calculate size */
1261 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1262 {
1263 /* Directory */
1264 iSizeFormat = -14;
1265 _tcscpy(szSize, _T("<DIR>"));
1266 }
1267 else
1268 {
1269 /* File */
1270 iSizeFormat = 14;
1271 u64FileSize.HighPart = ptrFiles[i]->nFileSizeHigh;
1272 u64FileSize.LowPart = ptrFiles[i]->nFileSizeLow;
1273 ConvertULargeInteger(u64FileSize, szSize, 20, lpFlags->bTSeperator);
1274 }
1275
1276 /* Calculate short name */
1277 szShortName[0] = _T('\0');
1278 if (lpFlags->bShortName)
1279 _stprintf(szShortName, _T(" %-12s"), ptrFiles[i]->cAlternateFileName);
1280
1281 /* Format date and time */
1282 DirPrintFileDateTime(szDate, szTime, ptrFiles[i], lpFlags);
1283
1284 /* Print the line */
1285 if(lpFlags->bPause)
1286 ConOutPrintfPaging(FALSE,_T("%10s %-8s %*s%s %s\n"),
1287 szDate,
1288 szTime,
1289 iSizeFormat,
1290 szSize,
1291 szShortName,
1292 ptrFiles[i]->cFileName);
1293 else
1294 ConOutPrintf(_T("%10s %-8s %*s%s %s\n"),
1295 szDate,
1296 szTime,
1297 iSizeFormat,
1298 szSize,
1299 szShortName,
1300 ptrFiles[i]->cFileName);
1301 }
1302 }
1303
1304
1305 /*
1306 * DirPrintWideList
1307 *
1308 * The function that prints in wide list
1309 */
1310 static VOID
1311 DirPrintWideList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1312 DWORD dwCount, /* [IN] The quantity of files */
1313 TCHAR *szCurPath, /* [IN] Full path of current directory */
1314 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1315 {
1316 SHORT iScreenWidth;
1317 USHORT iColumns;
1318 USHORT iLines;
1319 UINT iLongestName;
1320 TCHAR szTempFname[MAX_PATH];
1321 DWORD i;
1322 DWORD j;
1323 DWORD temp;
1324
1325 /* Calculate longest name */
1326 iLongestName = 1;
1327 for (i = 0; i < dwCount; i++)
1328 {
1329 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1330 {
1331 /* Directories need 2 additinal characters for brackets */
1332 if ((_tcslen(ptrFiles[i]->cFileName) + 2) > iLongestName)
1333 iLongestName = _tcslen(ptrFiles[i]->cFileName) + 2;
1334 }
1335 else
1336 {
1337 if (_tcslen(ptrFiles[i]->cFileName) > iLongestName)
1338 iLongestName = _tcslen(ptrFiles[i]->cFileName);
1339 }
1340 }
1341
1342 /* Count the highest number of columns */
1343 GetScreenSize(&iScreenWidth, 0);
1344 iColumns = iScreenWidth / iLongestName;
1345
1346 /* Check if there is enough space for spaces between names */
1347 if (((iLongestName * iColumns) + iColumns) >= (UINT)iScreenWidth)
1348 iColumns --;
1349
1350 /* A last check at iColumns to avoid division by zero */
1351 if (!(iColumns))
1352 iColumns = 1;
1353
1354 /* Print Column sorted */
1355 if (lpFlags->bWideListColSort)
1356 {
1357 /* Calculate the lines that will be printed */
1358 // iLines = ceil((float)dwCount/(float)iColumns);
1359 iLines = (USHORT)(dwCount / iColumns);
1360
1361 for (i = 0;i < iLines;i++)
1362 {
1363 for (j = 0; j < iColumns; j++)
1364 {
1365 temp = (j * iLines) + i;
1366 if (temp >= dwCount)
1367 break;
1368
1369 if (ptrFiles[temp]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1370 _stprintf(szTempFname, _T("[%s]"), ptrFiles[temp]->cFileName);
1371 else
1372 _stprintf(szTempFname, _T("%s"), ptrFiles[temp]->cFileName);
1373
1374 if(lpFlags->bPause)
1375 ConOutPrintfPaging(FALSE,_T("%-*s"), iLongestName + 1 , szTempFname);
1376 else
1377 ConOutPrintf(_T("%-*s"), iLongestName + 1 , szTempFname);
1378 }
1379
1380 if(lpFlags->bPause)
1381 ConOutPrintfPaging(FALSE,_T("\n"));
1382 else
1383 ConOutPrintf(_T("\n"));
1384 }
1385 }
1386 else
1387 {
1388 /* Print Line sorted */
1389 for (i = 0; i < dwCount; i++)
1390 {
1391 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1392 _stprintf(szTempFname, _T("[%s]"), ptrFiles[i]->cFileName);
1393 else
1394 _stprintf(szTempFname, _T("%s"), ptrFiles[i]->cFileName);
1395
1396 if(lpFlags->bPause)
1397 ConOutPrintfPaging(FALSE,_T("%-*s"), iLongestName + 1, szTempFname );
1398 else
1399 ConOutPrintf(_T("%-*s"), iLongestName + 1, szTempFname );
1400
1401 /*
1402 * We print a new line at the end of each column
1403 * except for the case that it is the last item.
1404 */
1405 if (!((i + 1) % iColumns) && (i < (dwCount - 1)))
1406 {
1407 if(lpFlags->bPause)
1408 ConOutPrintfPaging(FALSE,_T("\n"));
1409 else
1410 ConOutPrintf(_T("\n"));
1411 }
1412 }
1413
1414 /* Add a new line after the last item */
1415 if(lpFlags->bPause)
1416 ConOutPrintfPaging(FALSE,_T("\n"));
1417 else
1418 ConOutPrintf(_T("\n"));
1419 }
1420 }
1421
1422
1423 /*
1424 * DirPrintOldList
1425 *
1426 * The function that prints in old style
1427 */
1428 static VOID
1429 DirPrintOldList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1430 DWORD dwCount, /* [IN] The quantity of files */
1431 TCHAR * szCurPath, /* [IN] Full path of current directory */
1432 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1433 {
1434 DWORD i; /* An indexer for "for"s */
1435 TCHAR szName[10]; /* The name of file */
1436 TCHAR szExt[5]; /* The extension of file */
1437 TCHAR szDate[30],szTime[30]; /* Used to format time and date */
1438 TCHAR szSize[30]; /* The size of file */
1439 int iSizeFormat; /* The format of size field */
1440 ULARGE_INTEGER u64FileSize; /* The file size */
1441
1442 for(i = 0;i < dwCount;i++)
1443 {
1444 /* Broke 8.3 format */
1445 if (*ptrFiles[i]->cAlternateFileName )
1446 {
1447 /* If the file is long named then we read the alter name */
1448 getName( ptrFiles[i]->cAlternateFileName, szName);
1449 _tcscpy(szExt, getExt( ptrFiles[i]->cAlternateFileName));
1450 }
1451 else
1452 {
1453 /* If the file is not long name we read its original name */
1454 getName( ptrFiles[i]->cFileName, szName);
1455 _tcscpy(szExt, getExt( ptrFiles[i]->cFileName));
1456 }
1457
1458 /* Calculate size */
1459 if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1460 {
1461 /* Directory, no size it's a directory*/
1462 iSizeFormat = -17;
1463 _tcscpy(szSize, _T("<DIR>"));
1464 }
1465 else
1466 {
1467 /* File */
1468 iSizeFormat = 17;
1469 u64FileSize.HighPart = ptrFiles[i]->nFileSizeHigh;
1470 u64FileSize.LowPart = ptrFiles[i]->nFileSizeLow;
1471 ConvertULargeInteger(u64FileSize, szSize, 20, lpFlags->bTSeperator);
1472 }
1473
1474 /* Format date and time */
1475 DirPrintFileDateTime(szDate,szTime,ptrFiles[i],lpFlags);
1476
1477 /* Print the line */
1478 if(lpFlags->bPause)
1479 ConOutPrintfPaging(FALSE,_T("%-8s %-3s %*s %s %s\n"),
1480 szName, /* The file's 8.3 name */
1481 szExt, /* The file's 8.3 extension */
1482 iSizeFormat, /* print format for size column */
1483 szSize, /* The size of file or "<DIR>" for dirs */
1484 szDate, /* The date of file/dir */
1485 szTime); /* The time of file/dir */
1486 else
1487 ConOutPrintf(_T("%-8s %-3s %*s %s %s\n"),
1488 szName, /* The file's 8.3 name */
1489 szExt, /* The file's 8.3 extension */
1490 iSizeFormat, /* print format for size column */
1491 szSize, /* The size of file or "<DIR>" for dirs */
1492 szDate, /* The date of file/dir */
1493 szTime); /* The time of file/dir */
1494 }
1495 }
1496
1497 /*
1498 * DirPrintBareList
1499 *
1500 * The function that prints in bare format
1501 */
1502 static VOID
1503 DirPrintBareList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1504 DWORD dwCount, /* [IN] The number of files */
1505 LPTSTR lpCurPath, /* [IN] Full path of current directory */
1506 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1507 {
1508 TCHAR szFullName[MAX_PATH];
1509 DWORD i;
1510
1511 for (i = 0; i < dwCount; i++)
1512 {
1513 if ((_tcscmp(ptrFiles[i]->cFileName, _T(".")) == 0) ||
1514 (_tcscmp(ptrFiles[i]->cFileName, _T("..")) == 0))
1515 {
1516 /* at bare format we don't print "." and ".." folder */
1517 continue;
1518 }
1519 if (lpFlags->bRecursive)
1520 {
1521 /* at recursive mode we print full path of file */
1522 _tcscpy(szFullName, lpCurPath);
1523 _tcscat(szFullName, ptrFiles[i]->cFileName);
1524 if(lpFlags->bPause)
1525 ConOutPrintfPaging(FALSE,_T("%s\n"), szFullName);
1526 else
1527 ConOutPrintf(_T("%s\n"), szFullName);
1528 }
1529 else
1530 {
1531 /* if we are not in recursive mode we print the file names */
1532 if(lpFlags->bPause)
1533 ConOutPrintfPaging(FALSE,_T("%s\n"),ptrFiles[i]->cFileName);
1534 else
1535 ConOutPrintf(_T("%s\n"),ptrFiles[i]->cFileName);
1536 }
1537 }
1538 }
1539
1540
1541 /*
1542 * DirPrintFiles
1543 *
1544 * The functions that prints the files list
1545 */
1546 static VOID
1547 DirPrintFiles(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
1548 DWORD dwCount, /* [IN] The quantity of files */
1549 TCHAR *szCurPath, /* [IN] Full path of current directory */
1550 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags used */
1551 {
1552 TCHAR szMsg[RC_STRING_MAX_SIZE];
1553 TCHAR szTemp[MAX_PATH]; /* A buffer to format the directory header */
1554
1555 /* Print directory header */
1556 _tcscpy(szTemp, szCurPath);
1557
1558 /* We cut the trailing \ of the full path */
1559 szTemp[_tcslen(szTemp)-1] = _T('\0');
1560
1561 /* Condition to print header:
1562 We are not printing in bare format
1563 and if we are in recursive mode... we must have results */
1564 if (!(lpFlags->bBareFormat ) && !((lpFlags->bRecursive) && (dwCount <= 0)))
1565 {
1566 LoadString(CMD_ModuleHandle, STRING_DIR_HELP7, szMsg, RC_STRING_MAX_SIZE);
1567 if(lpFlags->bPause)
1568 ConOutPrintfPaging(FALSE,szMsg, szTemp);
1569 else
1570 ConOutPrintf(szMsg, szTemp);
1571 }
1572
1573 if (lpFlags->bBareFormat)
1574 {
1575 /* Bare format */
1576 DirPrintBareList(ptrFiles, dwCount, szCurPath, lpFlags);
1577 }
1578 else if(lpFlags->bShortName)
1579 {
1580 /* New list style / Short names */
1581 DirPrintNewList(ptrFiles, dwCount, szCurPath, lpFlags);
1582 }
1583 else if(lpFlags->bWideListColSort || lpFlags->bWideList)
1584 {
1585 /* Wide list */
1586 DirPrintWideList(ptrFiles, dwCount, szCurPath, lpFlags);
1587 }
1588 else if (lpFlags->bNewLongList )
1589 {
1590 /* New list style*/
1591 DirPrintNewList(ptrFiles, dwCount, szCurPath, lpFlags);
1592 }
1593 else
1594 {
1595 /* If nothing is selected old list is the default */
1596 DirPrintOldList(ptrFiles, dwCount, szCurPath, lpFlags);
1597 }
1598 }
1599
1600
1601
1602 /*
1603 * CompareFiles
1604 *
1605 * Compares 2 files based on the order criteria
1606 */
1607 static BOOL
1608 CompareFiles(LPWIN32_FIND_DATA lpFile1, /* [IN] A pointer to WIN32_FIND_DATA of file 1 */
1609 LPWIN32_FIND_DATA lpFile2, /* [IN] A pointer to WIN32_FIND_DATA of file 2 */
1610 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags that we use to list */
1611 {
1612 ULARGE_INTEGER u64File1;
1613 ULARGE_INTEGER u64File2;
1614 int i;
1615 long iComp = 0; /* The comparison result */
1616
1617 /* Calculate critiries by order given from user */
1618 for (i = 0;i < lpFlags->stOrderBy.sCriteriaCount;i++)
1619 {
1620
1621 /* Calculate criteria */
1622 switch(lpFlags->stOrderBy.eCriteria[i])
1623 {
1624 case ORDER_SIZE: /* Order by size /o:s */
1625 /* concat the 32bit integers to a 64bit */
1626 u64File1.LowPart = lpFile1->nFileSizeLow;
1627 u64File1.HighPart = lpFile1->nFileSizeHigh;
1628 u64File2.LowPart = lpFile2->nFileSizeLow;
1629 u64File2.HighPart = lpFile2->nFileSizeHigh;
1630
1631 /* In case that differnce is too big for a long */
1632 if (u64File1.QuadPart < u64File2.QuadPart)
1633 iComp = -1;
1634 else if (u64File1.QuadPart > u64File2.QuadPart)
1635 iComp = 1;
1636 else
1637 iComp = 0;
1638 break;
1639
1640 case ORDER_DIRECTORY: /* Order by directory attribute /o:g */
1641 iComp = ((lpFile2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)-
1642 (lpFile1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
1643 break;
1644
1645 case ORDER_EXTENSION: /* Order by extension name /o:e */
1646 iComp = _tcsicmp(getExt(lpFile1->cFileName),getExt(lpFile2->cFileName));
1647 break;
1648
1649 case ORDER_NAME: /* Order by filename /o:n */
1650 iComp = _tcsicmp(lpFile1->cFileName, lpFile2->cFileName);
1651 break;
1652
1653 case ORDER_TIME: /* Order by file's time /o:t */
1654 /* We compare files based on the time field selected by /t */
1655 switch(lpFlags->stTimeField.eTimeField)
1656 {
1657 case TF_CREATIONDATE:
1658 /* concat the 32bit integers to a 64bit */
1659 u64File1.LowPart = lpFile1->ftCreationTime.dwLowDateTime;
1660 u64File1.HighPart = lpFile1->ftCreationTime.dwHighDateTime ;
1661 u64File2.LowPart = lpFile2->ftCreationTime.dwLowDateTime;
1662 u64File2.HighPart = lpFile2->ftCreationTime.dwHighDateTime ;
1663 break;
1664 case TF_LASTACCESSEDDATE :
1665 /* concat the 32bit integers to a 64bit */
1666 u64File1.LowPart = lpFile1->ftLastAccessTime.dwLowDateTime;
1667 u64File1.HighPart = lpFile1->ftLastAccessTime.dwHighDateTime ;
1668 u64File2.LowPart = lpFile2->ftLastAccessTime.dwLowDateTime;
1669 u64File2.HighPart = lpFile2->ftLastAccessTime.dwHighDateTime ;
1670 break;
1671 case TF_MODIFIEDDATE:
1672 /* concat the 32bit integers to a 64bit */
1673 u64File1.LowPart = lpFile1->ftLastWriteTime.dwLowDateTime;
1674 u64File1.HighPart = lpFile1->ftLastWriteTime.dwHighDateTime ;
1675 u64File2.LowPart = lpFile2->ftLastWriteTime.dwLowDateTime;
1676 u64File2.HighPart = lpFile2->ftLastWriteTime.dwHighDateTime ;
1677 break;
1678 }
1679
1680 /* In case that differnce is too big for a long */
1681 if (u64File1.QuadPart < u64File2.QuadPart)
1682 iComp = -1;
1683 else if (u64File1.QuadPart > u64File2.QuadPart)
1684 iComp = 1;
1685 else
1686 iComp = 0;
1687 break;
1688 }
1689
1690 /* Reverse if desired */
1691 if (lpFlags->stOrderBy.bCriteriaRev[i])
1692 iComp *= -1;
1693
1694 /* If that criteria was enough for distinguishing
1695 the files/dirs,there is no need to calculate the others*/
1696 if (iComp != 0) break;
1697 }
1698
1699 /* Translate the value of iComp to boolean */
1700 if (iComp > 0)
1701 return TRUE;
1702 else
1703 return FALSE;
1704 }
1705
1706 /*
1707 * QsortFiles
1708 *
1709 * Sort files by the order criterias using quicksort method
1710 */
1711 static VOID
1712 QsortFiles(LPWIN32_FIND_DATA ptrArray[], /* [IN/OUT] The array with file info pointers */
1713 int i, /* [IN] The index of first item in array */
1714 int j, /* [IN] The index to last item in array */
1715 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags that we will use to sort */
1716 {
1717 LPWIN32_FIND_DATA lpTemp; /* A temporary pointer */
1718 int First, Last, Temp;
1719 BOOL Way;
1720
1721 if (i < j)
1722 {
1723 First = i;
1724 Last = j;
1725 Way = TRUE;
1726 while (i != j)
1727 {
1728 if (Way == CompareFiles(ptrArray[i], ptrArray[j], lpFlags))
1729 {
1730 /* Swap the pointers of the array */
1731 lpTemp = ptrArray[i];
1732 ptrArray[i]= ptrArray[j];
1733 ptrArray[j] = lpTemp;
1734
1735 /* Swap the indexes for inverting sorting */
1736 Temp = i;
1737 i = j;
1738 j =Temp;
1739
1740 Way = !Way;
1741 }
1742
1743 j += (!Way - Way);
1744 }
1745
1746 QsortFiles(ptrArray,First, i-1, lpFlags);
1747 QsortFiles(ptrArray,i+1,Last, lpFlags);
1748 }
1749 }
1750
1751
1752
1753 /*
1754 * DirList
1755 *
1756 * The functions that does everything except for printing results
1757 */
1758 static INT
1759 DirList(LPTSTR szPath, /* [IN] The path that dir starts */
1760 LPTSTR szFilespec, /* [IN] The type of file that we are looking for */
1761 LPINT pLine, /* FIXME: Maybe used for paginating */
1762 LPDIRSWITCHFLAGS lpFlags) /* [IN] The flags of the listing */
1763 {
1764 HANDLE hSearch; /* The handle of the search */
1765 HANDLE hRecSearch; /* The handle for searching recursivly */
1766 WIN32_FIND_DATA wfdFileInfo; /* The info of file that found */
1767 LPWIN32_FIND_DATA * ptrFileArray; /* An array of pointers with all the files */
1768 PDIRFINDLISTNODE ptrStartNode; /* The pointer to the first node */
1769 PDIRFINDLISTNODE ptrNextNode; /* A pointer used for relatives refernces */
1770 TCHAR szFullPath[MAX_PATH]; /* The full path that we are listing with trailing \ */
1771 TCHAR szFullFileSpec[MAX_PATH]; /* The full path with file specs that we ll request\ */
1772 DWORD dwCount; /* A counter of files found in directory */
1773 DWORD dwCountFiles; /* Counter for files */
1774 DWORD dwCountDirs; /* Counter for directories */
1775 ULARGE_INTEGER u64CountBytes; /* Counter for bytes */
1776 ULARGE_INTEGER u64Temp; /* A temporary counter */
1777
1778 /* Initialize Variables */
1779 ptrStartNode = NULL;
1780 ptrNextNode = NULL;
1781 dwCount = 0;
1782 dwCountFiles = 0;
1783 dwCountDirs = 0;
1784 u64CountBytes.QuadPart = 0;
1785
1786 /* Create szFullPath and szFullFileSpec */
1787 _tcscpy (szFullPath, szPath);
1788 if (szFullPath[_tcslen(szFullPath) - 1] != _T('\\'))
1789 _tcscat (szFullPath, _T("\\"));
1790 _tcscpy (szFullFileSpec, szFullPath);
1791 _tcscat (szFullFileSpec, szFilespec);
1792
1793 /* Prepare the linked list, first node is allocated */
1794 ptrStartNode = malloc(sizeof(DIRFINDLISTNODE));
1795 if (ptrStartNode == NULL)
1796 {
1797 #ifdef _DEBUG
1798 ConErrPrintf(_T("DEBUG: Cannot allocate memory for ptrStartNode!\n"));
1799 #endif
1800 return 1; /* Error cannot allocate memory for 1st object */
1801 }
1802 ptrNextNode = ptrStartNode;
1803
1804 /* Collect the results for the current folder */
1805 hSearch = FindFirstFile(szFullFileSpec, &wfdFileInfo);
1806 do
1807 {
1808 if (hSearch != INVALID_HANDLE_VALUE)
1809 {
1810 /* Here we filter all the specified attributes */
1811 if ((wfdFileInfo.dwFileAttributes & lpFlags->stAttribs.dwAttribMask )
1812 == (lpFlags->stAttribs.dwAttribMask & lpFlags->stAttribs.dwAttribVal ))
1813 {
1814 ptrNextNode->ptrNext = malloc(sizeof(DIRFINDLISTNODE));
1815 if (ptrNextNode->ptrNext == NULL)
1816 {
1817 #ifdef _DEBUG
1818 ConErrPrintf(_T("DEBUG: Cannot allocate memory for ptrNextNode->ptrNext!\n"));
1819 #endif
1820 while (ptrStartNode)
1821 {
1822 ptrNextNode = ptrStartNode->ptrNext;
1823 free(ptrStartNode);
1824 ptrStartNode = ptrNextNode;
1825 dwCount --;
1826 }
1827 return 1;
1828 }
1829
1830 /* If malloc fails we go to next file in hope it works,
1831 without braking the linked list! */
1832 if (ptrNextNode->ptrNext)
1833 {
1834 /* Copy the info of search at linked list */
1835 memcpy(&ptrNextNode->ptrNext->stFindInfo,
1836 &wfdFileInfo,
1837 sizeof(WIN32_FIND_DATA));
1838
1839 /* If lower case is selected do it here */
1840 if (lpFlags->bLowerCase)
1841 {
1842 _tcslwr(ptrNextNode->ptrNext->stFindInfo.cAlternateFileName);
1843 _tcslwr(ptrNextNode->ptrNext->stFindInfo.cFileName);
1844 }
1845
1846 /* Continue at next node at linked list */
1847 ptrNextNode = ptrNextNode->ptrNext;
1848 dwCount ++;
1849
1850 /* Grab statistics */
1851 if (wfdFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1852 {
1853 /* Directory */
1854 dwCountDirs++;
1855 }
1856 else
1857 {
1858 /* File */
1859 dwCountFiles++;
1860 u64Temp.HighPart = wfdFileInfo.nFileSizeHigh;
1861 u64Temp.LowPart = wfdFileInfo.nFileSizeLow;
1862 u64CountBytes.QuadPart += u64Temp.QuadPart;
1863 }
1864 }
1865 }
1866 }
1867 }while(FindNextFile(hSearch, &wfdFileInfo));
1868 FindClose(hSearch);
1869
1870 /* Terminate list */
1871 ptrNextNode->ptrNext = NULL;
1872
1873 /* Calculate and allocate space need for making an array of pointers */
1874 ptrFileArray = malloc(sizeof(LPWIN32_FIND_DATA) * dwCount);
1875 if (ptrFileArray == NULL)
1876 {
1877 #ifdef _DEBUG
1878 ConErrPrintf(_T("DEBUG: Cannot allocate memory for ptrFileArray!\n"));
1879 #endif
1880 while (ptrStartNode)
1881 {
1882 ptrNextNode = ptrStartNode->ptrNext;
1883 free(ptrStartNode);
1884 ptrStartNode = ptrNextNode;
1885 dwCount --;
1886 }
1887 return 1;
1888 }
1889
1890 /*
1891 * Create an array of pointers from the linked list
1892 * this will be used to sort and print data, rather than the list
1893 */
1894 ptrNextNode = ptrStartNode;
1895 dwCount = 0;
1896 while (ptrNextNode->ptrNext)
1897 {
1898 *(ptrFileArray + dwCount) = &ptrNextNode->ptrNext->stFindInfo;
1899 ptrNextNode = ptrNextNode->ptrNext;
1900 dwCount++;
1901 }
1902
1903 /* Sort Data if requested*/
1904 if (lpFlags->stOrderBy.sCriteriaCount > 0)
1905 QsortFiles(ptrFileArray, 0, dwCount-1,lpFlags);
1906
1907 /* Print Data */
1908 DirPrintFiles(ptrFileArray, dwCount, szFullPath, lpFlags);
1909
1910 /* Free array */
1911 free(ptrFileArray);
1912
1913 /* Add statistics to recursive statistics*/
1914 recurse_dir_cnt += dwCountDirs;
1915 recurse_file_cnt += dwCountFiles;
1916 recurse_bytes.QuadPart += u64CountBytes.QuadPart;
1917
1918 /* Do the recursive job if requested
1919 the recursive is be done on ALL(indepent of their attribs)
1920 directoried of the current one.*/
1921 if (lpFlags->bRecursive)
1922 {
1923 /* The new search is involving any *.* file */
1924 _tcscpy(szFullFileSpec, szFullPath);
1925 _tcscat(szFullFileSpec, _T("*.*"));
1926 hRecSearch = FindFirstFile (szFullFileSpec, &wfdFileInfo);
1927 do
1928 {
1929 if (hRecSearch != INVALID_HANDLE_VALUE)
1930 {
1931 /* We search for directories other than "." and ".." */
1932 if ((_tcsicmp(wfdFileInfo.cFileName, _T(".")) != 0) &&
1933 (_tcsicmp(wfdFileInfo.cFileName, _T("..")) != 0 ) &&
1934 (wfdFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
1935 {
1936 /* Concat the path and the directory to do recursive */
1937 _tcscpy(szFullFileSpec, szFullPath);
1938 _tcscat(szFullFileSpec, wfdFileInfo.cFileName);
1939 /* We do the same for tha folder */
1940 DirList(szFullFileSpec, szFilespec, pLine,lpFlags);
1941 }
1942 }
1943 }while(FindNextFile(hRecSearch,&wfdFileInfo));
1944 FindClose(hRecSearch);
1945 }
1946
1947 /* Free linked list */
1948 while (ptrStartNode)
1949 {
1950 ptrNextNode = ptrStartNode->ptrNext;
1951 free(ptrStartNode);
1952 ptrStartNode = ptrNextNode;
1953 dwCount --;
1954 }
1955
1956 return 0;
1957 }
1958
1959
1960
1961 /*
1962 * dir
1963 *
1964 * internal dir command
1965 */
1966 INT
1967 CommandDir(LPTSTR first, LPTSTR rest)
1968 {
1969 TCHAR dircmd[256]; /* A variable to store the DIRCMD enviroment variable */
1970 TCHAR cDrive;
1971 TCHAR szPath[MAX_PATH];
1972 TCHAR szFilespec[MAX_PATH];
1973 LPTSTR* params;
1974 INT entries = 0;
1975 INT nLine = 0;
1976 UINT loop = 0;
1977 DIRSWITCHFLAGS stFlags;
1978
1979 /* Initialize variables */
1980 cDrive = 0;
1981 recurse_dir_cnt = 0L;
1982 recurse_file_cnt = 0L;
1983 recurse_bytes.QuadPart = 0;
1984
1985 /* Initialize Switch Flags < Default switches are setted here!> */
1986 stFlags.b4Digit = TRUE;
1987 stFlags.bBareFormat = FALSE;
1988 stFlags.bLowerCase = FALSE;
1989 stFlags.bNewLongList = TRUE;
1990 stFlags.bPause = FALSE;
1991 stFlags.bRecursive = FALSE;
1992 stFlags.bShortName = FALSE;
1993 stFlags.bTSeperator = TRUE;
1994 stFlags.bUser = FALSE;
1995 stFlags.bWideList = FALSE;
1996 stFlags.bWideListColSort = FALSE;
1997 stFlags.stTimeField.eTimeField = TF_MODIFIEDDATE;
1998 stFlags.stTimeField.bUnSet = FALSE;
1999 stFlags.stAttribs.dwAttribMask = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
2000 stFlags.stAttribs.dwAttribVal = 0L;
2001 stFlags.stAttribs.bUnSet = FALSE;
2002 stFlags.stOrderBy.sCriteriaCount = 0;
2003 stFlags.stOrderBy.bUnSet = FALSE;
2004
2005 nErrorLevel = 0;
2006
2007 /* read the parameters from the DIRCMD environment variable */
2008 if (GetEnvironmentVariable (_T("DIRCMD"), dircmd, 256))
2009 if (!DirReadParam(dircmd, &params, &entries, &stFlags))
2010 {
2011 nErrorLevel = 1;
2012 return 1;
2013 }
2014
2015 /* read the parameters */
2016 if (!DirReadParam(rest, &params, &entries, &stFlags) || CheckCtrlBreak(BREAK_INPUT))
2017 {
2018 nErrorLevel = 1;
2019 return 1;
2020 }
2021
2022 /* default to current directory */
2023 if(entries == 0) {
2024 if(!add_entry(&entries, &params, _T("."))) {
2025 nErrorLevel = 1;
2026 return 1;
2027 }
2028 }
2029
2030 for(loop = 0; loop < entries; loop++)
2031 {
2032 /* parse the directory info */
2033 if (DirParsePathspec (params[loop], szPath, szFilespec) || CheckCtrlBreak(BREAK_INPUT))
2034 {
2035 nErrorLevel = 1;
2036 return 1;
2037 }
2038
2039 /* <Debug :>
2040 Uncomment this to show the final state of switch flags*/
2041 #ifdef _DEBUG
2042 {
2043 int i;
2044 ConOutPrintf(_T("Attributes mask/value %x/%x\n"),stFlags.stAttribs.dwAttribMask,stFlags.stAttribs.dwAttribVal );
2045 ConOutPrintf(_T("(B) Bare format : %i\n"), stFlags.bBareFormat );
2046 ConOutPrintf(_T("(C) Thousand : %i\n"), stFlags.bTSeperator );
2047 ConOutPrintf(_T("(W) Wide list : %i\n"), stFlags.bWideList );
2048 ConOutPrintf(_T("(D) Wide list sort by column : %i\n"), stFlags.bWideListColSort );
2049 ConOutPrintf(_T("(L) Lowercase : %i\n"), stFlags.bLowerCase );
2050 ConOutPrintf(_T("(N) New : %i\n"), stFlags.bNewLongList );
2051 ConOutPrintf(_T("(O) Order : %i\n"), stFlags.stOrderBy.sCriteriaCount );
2052 for (i =0;i<stFlags.stOrderBy.sCriteriaCount;i++)
2053 ConOutPrintf(_T(" Order Criteria [%i]: %i (Reversed: %i)\n"),i, stFlags.stOrderBy.eCriteria[i], stFlags.stOrderBy.bCriteriaRev[i] );
2054 ConOutPrintf(_T("(P) Pause : %i\n"), stFlags.bPause );
2055 ConOutPrintf(_T("(Q) Owner : %i\n"), stFlags.bUser );
2056 ConOutPrintf(_T("(S) Recursive : %i\n"), stFlags.bRecursive );
2057 ConOutPrintf(_T("(T) Time field : %i\n"), stFlags.stTimeField.eTimeField );
2058 ConOutPrintf(_T("(X) Short names : %i\n"), stFlags.bShortName );
2059 ConOutPrintf(_T("Parameter : %s\n"), params[loop] );
2060 }
2061 #endif
2062
2063 /* Print the drive header if the drive changed */
2064 if(cDrive != szPath[0] && !stFlags.bBareFormat) {
2065 if (!PrintDirectoryHeader (szPath, &nLine, &stFlags)) {
2066 nErrorLevel = 1;
2067 return 1;
2068 }
2069
2070 cDrive = szPath[0];
2071 }
2072
2073
2074 /* do the actual dir */
2075 if (DirList (szPath, szFilespec, &nLine, &stFlags))
2076 {
2077 nErrorLevel = 1;
2078 return 1;
2079 }
2080 }
2081
2082 /* print the footer */
2083 PrintSummary(szPath,
2084 recurse_file_cnt,
2085 recurse_dir_cnt,
2086 recurse_bytes,
2087 &nLine,
2088 &stFlags);
2089
2090 return 0;
2091 }
2092
2093 #endif
2094
2095 /* EOF */