2 * CMD - Wine-compatible command line interface - Directory functions.
4 * Copyright (C) 1999 D A Pickles
5 * Copyright (C) 2007 J Edmeades
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * On entry, global variables quals, param1, param2 contain
25 * the qualifiers (uppercased and concatenated) and parameters entered, with
26 * environment-variable and batch parameter substitution already done.
29 #define WIN32_LEAN_AND_MEAN
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(cmd
);
38 extern WCHAR quals
[MAX_PATH
], param1
[MAX_PATH
], param2
[MAX_PATH
];
39 extern DWORD errorlevel
;
41 typedef enum _DISPLAYTIME
48 typedef enum _DISPLAYORDER
56 static int file_total
, dir_total
, recurse
, wide
, bare
, max_width
, lower
;
57 static int shortname
, usernames
;
58 static ULONGLONG byte_total
;
59 static DISPLAYTIME dirTime
;
60 static DISPLAYORDER dirOrder
;
61 static BOOL orderReverse
, orderGroupDirs
, orderGroupDirsReverse
, orderByCol
;
62 static BOOL separator
;
63 static ULONG showattrs
, attrsbits
;
65 static const WCHAR dotW
[] = {'.','\0'};
66 static const WCHAR dotdotW
[] = {'.','.','\0'};
67 static const WCHAR starW
[] = {'*','\0'};
68 static const WCHAR slashW
[] = {'\\','\0'};
69 static const WCHAR emptyW
[] = {'\0'};
70 static const WCHAR spaceW
[] = {' ','\0'};
72 /*****************************************************************************
75 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
77 static WCHAR
* WCMD_strrev (WCHAR
*buff
) {
83 for (i
=0; i
<r
/2; i
++) {
85 buff
[i
] = buff
[r
-i
-1];
91 /*****************************************************************************
94 * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
95 * Result is returned in a static string overwritten with each call.
96 * FIXME: There must be a better algorithm!
98 static WCHAR
* WCMD_filesize64 (ULONGLONG n
) {
103 static WCHAR buff
[32];
108 if (separator
&& ((++i
)%3 == 1)) *p
++ = ',';
119 /*****************************************************************************
122 * Sort based on the /O options supplied on the command line
124 static int WCMD_dir_sort (const void *a
, const void *b
)
126 WIN32_FIND_DATA
*filea
= (WIN32_FIND_DATA
*)a
;
127 WIN32_FIND_DATA
*fileb
= (WIN32_FIND_DATA
*)b
;
130 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
131 requested sort order for the directory components */
132 if (orderGroupDirs
&&
133 ((filea
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
134 (fileb
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)))
136 BOOL aDir
= filea
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
137 if (aDir
) result
= -1;
139 if (orderGroupDirsReverse
) result
= -result
;
143 } else if (dirOrder
== Name
) {
144 result
= lstrcmpiW(filea
->cFileName
, fileb
->cFileName
);
147 } else if (dirOrder
== Size
) {
148 ULONG64 sizea
= (((ULONG64
)filea
->nFileSizeHigh
) << 32) + filea
->nFileSizeLow
;
149 ULONG64 sizeb
= (((ULONG64
)fileb
->nFileSizeHigh
) << 32) + fileb
->nFileSizeLow
;
150 if( sizea
< sizeb
) result
= -1;
151 else if( sizea
== sizeb
) result
= 0;
154 /* Order by Date: (Takes into account which date (/T option) */
155 } else if (dirOrder
== Date
) {
158 ULONG64 timea
, timeb
;
160 if (dirTime
== Written
) {
161 ft
= &filea
->ftLastWriteTime
;
162 timea
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
163 ft
= &fileb
->ftLastWriteTime
;
164 timeb
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
165 } else if (dirTime
== Access
) {
166 ft
= &filea
->ftLastAccessTime
;
167 timea
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
168 ft
= &fileb
->ftLastAccessTime
;
169 timeb
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
171 ft
= &filea
->ftCreationTime
;
172 timea
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
173 ft
= &fileb
->ftCreationTime
;
174 timeb
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
176 if( timea
< timeb
) result
= -1;
177 else if( timea
== timeb
) result
= 0;
180 /* Order by Extension: (Takes into account which date (/T option) */
181 } else if (dirOrder
== Extension
) {
184 WCHAR fname
[MAX_PATH
];
185 WCHAR extA
[MAX_PATH
];
186 WCHAR extB
[MAX_PATH
];
188 /* Split into components */
189 WCMD_splitpath(filea
->cFileName
, drive
, dir
, fname
, extA
);
190 WCMD_splitpath(fileb
->cFileName
, drive
, dir
, fname
, extB
);
191 result
= lstrcmpiW(extA
, extB
);
194 if (orderReverse
) result
= -result
;
198 /*****************************************************************************
201 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
203 static void WCMD_getfileowner(WCHAR
*filename
, WCHAR
*owner
, int ownerlen
) {
205 ULONG sizeNeeded
= 0;
207 WCHAR name
[MAXSTRING
];
208 WCHAR domain
[MAXSTRING
];
210 /* In case of error, return empty string */
213 /* Find out how much space we need for the owner security descriptor */
214 GetFileSecurity(filename
, OWNER_SECURITY_INFORMATION
, 0, 0, &sizeNeeded
);
217 if(rc
== ERROR_INSUFFICIENT_BUFFER
&& sizeNeeded
> 0) {
221 BOOL defaulted
= FALSE
;
222 ULONG nameLen
= MAXSTRING
;
223 ULONG domainLen
= MAXSTRING
;
224 SID_NAME_USE nameuse
;
226 secBuffer
= HeapAlloc(GetProcessHeap(),0,sizeNeeded
* sizeof(BYTE
));
227 if(!secBuffer
) return;
229 /* Get the owners security descriptor */
230 if(!GetFileSecurity(filename
, OWNER_SECURITY_INFORMATION
, secBuffer
,
231 sizeNeeded
, &sizeNeeded
)) {
232 HeapFree(GetProcessHeap(),0,secBuffer
);
236 /* Get the SID from the SD */
237 if(!GetSecurityDescriptorOwner(secBuffer
, &pSID
, &defaulted
)) {
238 HeapFree(GetProcessHeap(),0,secBuffer
);
242 /* Convert to a username */
243 if (LookupAccountSid(NULL
, pSID
, name
, &nameLen
, domain
, &domainLen
, &nameuse
)) {
244 static const WCHAR fmt
[] = {'%','s','%','c','%','s','\0'};
245 snprintfW(owner
, ownerlen
, fmt
, domain
, '\\', name
);
247 HeapFree(GetProcessHeap(),0,secBuffer
);
252 /*****************************************************************************
253 * WCMD_list_directory
255 * List a single file directory. This function (and those below it) can be called
256 * recursively when the /S switch is used.
258 * FIXME: Assumes 24-line display for the /P qualifier.
261 static DIRECTORY_STACK
*WCMD_list_directory (DIRECTORY_STACK
*inputparms
, int level
) {
263 WCHAR string
[1024], datestring
[32], timestring
[32];
264 WCHAR real_path
[MAX_PATH
];
269 int dir_count
, file_count
, entry_count
, i
, widest
, cur_width
, tmp_width
;
270 int numCols
, numRows
;
272 ULARGE_INTEGER byte_count
, file_size
;
273 DIRECTORY_STACK
*parms
;
274 int concurrentDirs
= 0;
275 BOOL done_header
= FALSE
;
277 static const WCHAR fmtDir
[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
278 '<','D','I','R','>',' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
279 static const WCHAR fmtFile
[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
280 ' ',' ','%','1','0','s',' ',' ','\0'};
281 static const WCHAR fmt2
[] = {'%','-','1','3','s','\0'};
282 static const WCHAR fmt3
[] = {'%','-','2','3','s','\0'};
283 static const WCHAR fmt4
[] = {'%','s','\0'};
284 static const WCHAR fmt5
[] = {'%','s','%','s','\0'};
289 byte_count
.QuadPart
= 0;
293 /* Loop merging all the files from consecutive parms which relate to the
294 same directory. Note issuing a directory header with no contents
295 mirrors what windows does */
297 fd
= HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATA
));
298 while (parms
&& strcmpW(inputparms
->dirName
, parms
->dirName
) == 0) {
301 /* Work out the full path + filename */
302 strcpyW(real_path
, parms
->dirName
);
303 strcatW(real_path
, parms
->fileName
);
305 /* Load all files into an in memory structure */
306 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path
));
307 hff
= FindFirstFile (real_path
, (fd
+entry_count
));
308 if (hff
!= INVALID_HANDLE_VALUE
) {
310 /* Skip any which are filtered out by attribute */
311 if (((fd
+entry_count
)->dwFileAttributes
& attrsbits
) != showattrs
) continue;
315 /* Keep running track of longest filename for wide output */
316 if (wide
|| orderByCol
) {
317 int tmpLen
= strlenW((fd
+(entry_count
-1))->cFileName
) + 3;
318 if ((fd
+(entry_count
-1))->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) tmpLen
= tmpLen
+ 2;
319 if (tmpLen
> widest
) widest
= tmpLen
;
322 fd
= HeapReAlloc(GetProcessHeap(),0,fd
,(entry_count
+1)*sizeof(WIN32_FIND_DATA
));
325 WINE_ERR("Out of memory\n");
329 } while (FindNextFile(hff
, (fd
+entry_count
)) != 0);
333 /* Work out the actual current directory name without a trailing \ */
334 strcpyW(real_path
, parms
->dirName
);
335 real_path
[strlenW(parms
->dirName
)-1] = 0x00;
337 /* Output the results */
339 if (level
!= 0 && (entry_count
> 0)) WCMD_output (newline
);
340 if (!recurse
|| ((entry_count
> 0) && done_header
==FALSE
)) {
341 static const WCHAR headerW
[] = {'D','i','r','e','c','t','o','r','y',' ','o','f',
342 ' ','%','s','\n','\n','\0'};
343 WCMD_output (headerW
, real_path
);
348 /* Move to next parm */
352 /* Handle case where everything is filtered out */
353 if (entry_count
> 0) {
355 /* Sort the list of files */
356 qsort (fd
, entry_count
, sizeof(WIN32_FIND_DATA
), WCMD_dir_sort
);
358 /* Work out the number of columns */
359 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count
, max_width
, widest
);
360 if (wide
|| orderByCol
) {
361 numCols
= max(1, (int)max_width
/ widest
);
362 numRows
= entry_count
/ numCols
;
363 if (entry_count
% numCols
) numRows
++;
366 numRows
= entry_count
;
368 WINE_TRACE("cols=%d, rows=%d\n", numCols
, numRows
);
370 for (rows
=0; rows
<numRows
; rows
++) {
371 BOOL addNewLine
= TRUE
;
372 for (cols
=0; cols
<numCols
; cols
++) {
375 /* Work out the index of the entry being pointed to */
377 i
= (cols
* numRows
) + rows
;
378 if (i
>= entry_count
) continue;
380 i
= (rows
* numCols
) + cols
;
381 if (i
>= entry_count
) continue;
384 /* /L convers all names to lower case */
386 WCHAR
*p
= (fd
+i
)->cFileName
;
387 while ( (*p
= tolower(*p
)) ) ++p
;
390 /* /Q gets file ownership information */
392 strcpyW (string
, inputparms
->dirName
);
393 strcatW (string
, (fd
+i
)->cFileName
);
394 WCMD_getfileowner(string
, username
, sizeof(username
)/sizeof(WCHAR
));
397 if (dirTime
== Written
) {
398 FileTimeToLocalFileTime (&(fd
+i
)->ftLastWriteTime
, &ft
);
399 } else if (dirTime
== Access
) {
400 FileTimeToLocalFileTime (&(fd
+i
)->ftLastAccessTime
, &ft
);
402 FileTimeToLocalFileTime (&(fd
+i
)->ftCreationTime
, &ft
);
404 FileTimeToSystemTime (&ft
, &st
);
405 GetDateFormat (0, DATE_SHORTDATE
, &st
, NULL
, datestring
,
406 sizeof(datestring
)/sizeof(WCHAR
));
407 GetTimeFormat (0, TIME_NOSECONDS
, &st
,
408 NULL
, timestring
, sizeof(timestring
)/sizeof(WCHAR
));
412 tmp_width
= cur_width
;
413 if ((fd
+i
)->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
414 static const WCHAR fmt
[] = {'[','%','s',']','\0'};
415 WCMD_output (fmt
, (fd
+i
)->cFileName
);
417 tmp_width
= tmp_width
+ strlenW((fd
+i
)->cFileName
) + 2;
419 static const WCHAR fmt
[] = {'%','s','\0'};
420 WCMD_output (fmt
, (fd
+i
)->cFileName
);
421 tmp_width
= tmp_width
+ strlenW((fd
+i
)->cFileName
) ;
423 file_size
.u
.LowPart
= (fd
+i
)->nFileSizeLow
;
424 file_size
.u
.HighPart
= (fd
+i
)->nFileSizeHigh
;
425 byte_count
.QuadPart
+= file_size
.QuadPart
;
427 cur_width
= cur_width
+ widest
;
429 if ((cur_width
+ widest
) > max_width
) {
432 int padding
= cur_width
- tmp_width
;
436 /* Note: WCMD_output uses wvsprintf which does not allow %*
437 so manually pad with spaces to appropriate width */
438 strcpyW(temp
, emptyW
);
439 while (padding
> 0) {
440 strcatW(&temp
[toWrite
], spaceW
);
445 strcpyW(temp
, emptyW
);
452 } else if ((fd
+i
)->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
456 WCMD_output (fmtDir
, datestring
, timestring
);
457 if (shortname
) WCMD_output (fmt2
, (fd
+i
)->cAlternateFileName
);
458 if (usernames
) WCMD_output (fmt3
, username
);
459 WCMD_output(fmt4
,(fd
+i
)->cFileName
);
461 if (!((strcmpW((fd
+i
)->cFileName
, dotW
) == 0) ||
462 (strcmpW((fd
+i
)->cFileName
, dotdotW
) == 0))) {
463 WCMD_output (fmt5
, recurse
?inputparms
->dirName
:emptyW
, (fd
+i
)->cFileName
);
471 file_size
.u
.LowPart
= (fd
+i
)->nFileSizeLow
;
472 file_size
.u
.HighPart
= (fd
+i
)->nFileSizeHigh
;
473 byte_count
.QuadPart
+= file_size
.QuadPart
;
475 WCMD_output (fmtFile
, datestring
, timestring
,
476 WCMD_filesize64(file_size
.QuadPart
));
477 if (shortname
) WCMD_output (fmt2
, (fd
+i
)->cAlternateFileName
);
478 if (usernames
) WCMD_output (fmt3
, username
);
479 WCMD_output(fmt4
,(fd
+i
)->cFileName
);
481 WCMD_output (fmt5
, recurse
?inputparms
->dirName
:emptyW
, (fd
+i
)->cFileName
);
485 if (addNewLine
) WCMD_output (newline
);
490 if (file_count
== 1) {
491 static const WCHAR fmt
[] = {' ',' ',' ',' ',' ',' ',' ','1',' ','f','i','l','e',' ',
492 '%','2','5','s',' ','b','y','t','e','s','\n','\0'};
493 WCMD_output (fmt
, WCMD_filesize64 (byte_count
.QuadPart
));
496 static const WCHAR fmt
[] = {'%','8','d',' ','f','i','l','e','s',' ','%','2','4','s',
497 ' ','b','y','t','e','s','\n','\0'};
498 WCMD_output (fmt
, file_count
, WCMD_filesize64 (byte_count
.QuadPart
));
501 byte_total
= byte_total
+ byte_count
.QuadPart
;
502 file_total
= file_total
+ file_count
;
503 dir_total
= dir_total
+ dir_count
;
505 if (!bare
&& !recurse
) {
506 if (dir_count
== 1) {
507 static const WCHAR fmt
[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','y',
508 ' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
509 WCMD_output (fmt
, 1);
511 static const WCHAR fmt
[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i',
513 WCMD_output (fmt
, dir_count
);
517 HeapFree(GetProcessHeap(),0,fd
);
519 /* When recursing, look in all subdirectories for matches */
521 DIRECTORY_STACK
*dirStack
= NULL
;
522 DIRECTORY_STACK
*lastEntry
= NULL
;
523 WIN32_FIND_DATA finddata
;
525 /* Build path to search */
526 strcpyW(string
, inputparms
->dirName
);
527 strcatW(string
, starW
);
529 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string
));
530 hff
= FindFirstFile (string
, &finddata
);
531 if (hff
!= INVALID_HANDLE_VALUE
) {
533 if ((finddata
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
534 (strcmpW(finddata
.cFileName
, dotdotW
) != 0) &&
535 (strcmpW(finddata
.cFileName
, dotW
) != 0)) {
537 DIRECTORY_STACK
*thisDir
;
538 int dirsToCopy
= concurrentDirs
;
540 /* Loop creating list of subdirs for all concurrent entries */
542 while (dirsToCopy
> 0) {
545 /* Work out search parameter in sub dir */
546 strcpyW (string
, inputparms
->dirName
);
547 strcatW (string
, finddata
.cFileName
);
548 strcatW (string
, slashW
);
549 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string
));
551 /* Allocate memory, add to list */
552 thisDir
= HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK
));
553 if (dirStack
== NULL
) dirStack
= thisDir
;
554 if (lastEntry
!= NULL
) lastEntry
->next
= thisDir
;
556 thisDir
->next
= NULL
;
557 thisDir
->dirName
= HeapAlloc(GetProcessHeap(),0,
558 sizeof(WCHAR
) * (strlenW(string
)+1));
559 strcpyW(thisDir
->dirName
, string
);
560 thisDir
->fileName
= HeapAlloc(GetProcessHeap(),0,
561 sizeof(WCHAR
) * (strlenW(parms
->fileName
)+1));
562 strcpyW(thisDir
->fileName
, parms
->fileName
);
566 } while (FindNextFile(hff
, &finddata
) != 0);
569 while (dirStack
!= NULL
) {
570 DIRECTORY_STACK
*thisDir
= dirStack
;
571 dirStack
= WCMD_list_directory (thisDir
, 1);
572 while (thisDir
!= dirStack
) {
573 DIRECTORY_STACK
*tempDir
= thisDir
->next
;
574 HeapFree(GetProcessHeap(),0,thisDir
->dirName
);
575 HeapFree(GetProcessHeap(),0,thisDir
->fileName
);
576 HeapFree(GetProcessHeap(),0,thisDir
);
583 /* Handle case where everything is filtered out */
584 if ((file_total
+ dir_total
== 0) && (level
== 0)) {
585 SetLastError (ERROR_FILE_NOT_FOUND
);
593 /*****************************************************************************
596 * Print out the trailer for the supplied drive letter
598 static void WCMD_dir_trailer(WCHAR drive
) {
599 ULARGE_INTEGER avail
, total
, freebytes
;
601 WCHAR driveName
[4] = {'c',':','\\','\0'};
603 driveName
[0] = drive
;
604 status
= GetDiskFreeSpaceEx (driveName
, &avail
, &total
, &freebytes
);
605 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName
),
606 status
, GetLastError());
608 if (errorlevel
==0 && !bare
) {
610 static const WCHAR fmt1
[] = {'\n',' ',' ',' ',' ',' ','T','o','t','a','l',' ','f','i','l','e','s',
611 ' ','l','i','s','t','e','d',':','\n','%','8','d',' ','f','i','l','e',
612 's','%','2','5','s',' ','b','y','t','e','s','\n','\0'};
613 static const WCHAR fmt2
[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i','e','s',' ','%',
614 '1','8','s',' ','b','y','t','e','s',' ','f','r','e','e','\n','\n',
616 WCMD_output (fmt1
, file_total
, WCMD_filesize64 (byte_total
));
617 WCMD_output (fmt2
, dir_total
, WCMD_filesize64 (freebytes
.QuadPart
));
619 static const WCHAR fmt
[] = {' ','%','1','8','s',' ','b','y','t','e','s',' ','f','r','e','e',
621 WCMD_output (fmt
, WCMD_filesize64 (freebytes
.QuadPart
));
626 /*****************************************************************************
629 * List a file directory.
633 void WCMD_directory (WCHAR
*cmd
) {
635 WCHAR path
[MAX_PATH
], cwd
[MAX_PATH
];
636 int status
, paged_mode
;
637 CONSOLE_SCREEN_BUFFER_INFO consoleInfo
;
639 WCHAR string
[MAXSTRING
];
641 int argsProcessed
= 0;
644 BOOL trailerReqd
= FALSE
;
645 DIRECTORY_STACK
*fullParms
= NULL
;
646 DIRECTORY_STACK
*prevEntry
= NULL
;
647 DIRECTORY_STACK
*thisEntry
= NULL
;
650 WCHAR fname
[MAX_PATH
];
652 static const WCHAR dircmdW
[] = {'D','I','R','C','M','D','\0'};
656 /* Prefill quals with (uppercased) DIRCMD env var */
657 if (GetEnvironmentVariable (dircmdW
, string
, sizeof(string
)/sizeof(WCHAR
))) {
659 while ( (*p
= toupper(*p
)) ) ++p
;
660 strcatW(string
,quals
);
661 strcpyW(quals
, string
);
665 file_total
= dir_total
= 0;
667 /* Initialize all flags to their defaults as if no DIRCMD or quals */
679 orderReverse
= FALSE
;
680 orderGroupDirs
= FALSE
;
681 orderGroupDirsReverse
= FALSE
;
683 attrsbits
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
685 /* Handle args - Loop through so right most is the effective one */
686 /* Note: /- appears to be a negate rather than an off, eg. dir
687 /-W is wide, or dir /w /-w /-w is also wide */
689 while (*p
&& (*p
=='/' || *p
==' ')) {
691 if (*p
++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
698 WINE_TRACE("Processing arg '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
700 case 'P': if (negate
) paged_mode
= !paged_mode
;
701 else paged_mode
= TRUE
;
703 case 'S': if (negate
) recurse
= !recurse
;
706 case 'W': if (negate
) wide
= !wide
;
709 case 'B': if (negate
) bare
= !bare
;
712 case 'L': if (negate
) lower
= !lower
;
715 case 'X': if (negate
) shortname
= !shortname
;
716 else shortname
= TRUE
;
718 case 'Q': if (negate
) usernames
= !usernames
;
719 else usernames
= TRUE
;
721 case 'D': if (negate
) orderByCol
= !orderByCol
;
722 else orderByCol
= TRUE
;
724 case 'C': if (negate
) separator
= !separator
;
725 else separator
= TRUE
;
728 if (*p
==':') p
++; /* Skip optional : */
730 if (*p
== 'A') dirTime
= Access
;
731 else if (*p
== 'C') dirTime
= Creation
;
732 else if (*p
== 'W') dirTime
= Written
;
734 /* Support /T and /T: with no parms, default to written */
735 else if (*p
== 0x00 || *p
== '/') {
737 p
= p
- 1; /* So when step on, move to '/' */
739 SetLastError(ERROR_INVALID_PARAMETER
);
746 if (*p
==':') p
++; /* Skip optional : */
747 while (*p
&& *p
!= '/') {
748 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
750 case 'N': dirOrder
= Name
; break;
751 case 'E': dirOrder
= Extension
; break;
752 case 'S': dirOrder
= Size
; break;
753 case 'D': dirOrder
= Date
; break;
754 case '-': if (*(p
+1)=='G') orderGroupDirsReverse
=TRUE
;
755 else orderReverse
= TRUE
;
757 case 'G': orderGroupDirs
= TRUE
; break;
759 SetLastError(ERROR_INVALID_PARAMETER
);
766 p
= p
- 1; /* So when step on, move to '/' */
771 if (*p
==':') p
++; /* Skip optional : */
772 while (*p
&& *p
!= '/') {
773 BOOL anegate
= FALSE
;
776 /* Note /A: - options are 'offs' not toggles */
782 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
784 case 'D': mask
= FILE_ATTRIBUTE_DIRECTORY
; break;
785 case 'H': mask
= FILE_ATTRIBUTE_HIDDEN
; break;
786 case 'S': mask
= FILE_ATTRIBUTE_SYSTEM
; break;
787 case 'R': mask
= FILE_ATTRIBUTE_READONLY
; break;
788 case 'A': mask
= FILE_ATTRIBUTE_ARCHIVE
; break;
790 SetLastError(ERROR_INVALID_PARAMETER
);
796 /* Keep running list of bits we care about */
799 /* Mask shows what MUST be in the bits we care about */
800 if (anegate
) showattrs
= showattrs
& ~mask
;
801 else showattrs
|= mask
;
805 p
= p
- 1; /* So when step on, move to '/' */
806 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs
, attrsbits
);
809 SetLastError(ERROR_INVALID_PARAMETER
);
817 /* Handle conflicting args and initialization */
818 if (bare
|| shortname
) wide
= FALSE
;
819 if (bare
) shortname
= FALSE
;
820 if (wide
) usernames
= FALSE
;
821 if (orderByCol
) wide
= TRUE
;
824 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &consoleInfo
))
825 max_width
= consoleInfo
.dwSize
.X
;
830 WCMD_enter_paged_mode(NULL
);
836 GetCurrentDirectory (MAX_PATH
, cwd
);
837 strcatW(cwd
, slashW
);
839 /* Loop through all args, calculating full effective directory */
843 WCHAR fullname
[MAXSTRING
];
844 WCHAR
*thisArg
= WCMD_parameter (cmd
, argno
++, &argN
);
845 if (argN
&& argN
[0] != '/') {
847 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg
));
848 if (thisArg
[1] == ':' && thisArg
[2] == '\\') {
849 strcpyW(fullname
, thisArg
);
850 } else if (thisArg
[1] == ':' && thisArg
[2] != '\\') {
852 static const WCHAR envFmt
[] = {'=','%','c',':','\0'};
853 wsprintf(envvar
, envFmt
, thisArg
[0]);
854 if (!GetEnvironmentVariable(envvar
, fullname
, MAX_PATH
)) {
855 static const WCHAR noEnvFmt
[] = {'%','c',':','\0'};
856 wsprintf(fullname
, noEnvFmt
, thisArg
[0]);
858 strcatW(fullname
, slashW
);
859 strcatW(fullname
, &thisArg
[2]);
860 } else if (thisArg
[0] == '\\') {
861 memcpy(fullname
, cwd
, 2 * sizeof(WCHAR
));
862 strcpyW(fullname
+2, thisArg
);
864 strcpyW(fullname
, cwd
);
865 strcatW(fullname
, thisArg
);
867 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname
));
869 status
= GetFullPathName (fullname
, sizeof(path
)/sizeof(WCHAR
), path
, NULL
);
872 * If the path supplied does not include a wildcard, and the endpoint of the
873 * path references a directory, we need to list the *contents* of that
874 * directory not the directory file itself.
876 if ((strchrW(path
, '*') == NULL
) && (strchrW(path
, '%') == NULL
)) {
877 status
= GetFileAttributes (path
);
878 if ((status
!= INVALID_FILE_ATTRIBUTES
) && (status
& FILE_ATTRIBUTE_DIRECTORY
)) {
879 if (path
[strlenW(path
)-1] == '\\') {
880 strcatW (path
, starW
);
883 const WCHAR slashStarW
[] = {'\\','*','\0'};
884 strcatW (path
, slashStarW
);
888 /* Special case wildcard search with no extension (ie parameters ending in '.') as
889 GetFullPathName strips off the additional '.' */
890 if (fullname
[strlenW(fullname
)-1] == '.') strcatW(path
, dotW
);
893 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path
));
894 thisEntry
= HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK
));
895 if (fullParms
== NULL
) fullParms
= thisEntry
;
896 if (prevEntry
!= NULL
) prevEntry
->next
= thisEntry
;
897 prevEntry
= thisEntry
;
898 thisEntry
->next
= NULL
;
900 /* Split into components */
901 WCMD_splitpath(path
, drive
, dir
, fname
, ext
);
902 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
903 wine_dbgstr_w(drive
), wine_dbgstr_w(dir
),
904 wine_dbgstr_w(fname
), wine_dbgstr_w(ext
));
906 thisEntry
->dirName
= HeapAlloc(GetProcessHeap(),0,
907 sizeof(WCHAR
) * (strlenW(drive
)+strlenW(dir
)+1));
908 strcpyW(thisEntry
->dirName
, drive
);
909 strcatW(thisEntry
->dirName
, dir
);
911 thisEntry
->fileName
= HeapAlloc(GetProcessHeap(),0,
912 sizeof(WCHAR
) * (strlenW(fname
)+strlenW(ext
)+1));
913 strcpyW(thisEntry
->fileName
, fname
);
914 strcatW(thisEntry
->fileName
, ext
);
919 /* If just 'dir' entered, a '*' parameter is assumed */
920 if (fullParms
== NULL
) {
921 WINE_TRACE("Inserting default '*'\n");
922 fullParms
= HeapAlloc(GetProcessHeap(),0, sizeof(DIRECTORY_STACK
));
923 fullParms
->next
= NULL
;
924 fullParms
->dirName
= HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
) * (strlenW(cwd
)+1));
925 strcpyW(fullParms
->dirName
, cwd
);
926 fullParms
->fileName
= HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR
) * 2);
927 strcpyW(fullParms
->fileName
, starW
);
932 thisEntry
= fullParms
;
935 while (thisEntry
!= NULL
) {
937 /* Output disk free (trailer) and volume information (header) if the drive
939 if (lastDrive
!= toupper(thisEntry
->dirName
[0])) {
941 /* Trailer Information */
942 if (lastDrive
!= '?') {
944 WCMD_dir_trailer(prevEntry
->dirName
[0]);
947 lastDrive
= toupper(thisEntry
->dirName
[0]);
952 WINE_TRACE("Writing volume for '%c:'\n", thisEntry
->dirName
[0]);
953 memcpy(drive
, thisEntry
->dirName
, 2 * sizeof(WCHAR
));
955 status
= WCMD_volume (0, drive
);
963 static const WCHAR newLine2
[] = {'\n','\n','\0'};
964 if (!bare
) WCMD_output (newLine2
);
967 /* Clear any errors from previous invocations, and process it */
969 prevEntry
= thisEntry
;
970 thisEntry
= WCMD_list_directory (thisEntry
, 0);
973 /* Trailer Information */
975 WCMD_dir_trailer(prevEntry
->dirName
[0]);
979 if (paged_mode
) WCMD_leave_paged_mode();
981 /* Free storage allocated for parms */
982 while (fullParms
!= NULL
) {
983 prevEntry
= fullParms
;
984 fullParms
= prevEntry
->next
;
985 HeapFree(GetProcessHeap(),0,prevEntry
->dirName
);
986 HeapFree(GetProcessHeap(),0,prevEntry
->fileName
);
987 HeapFree(GetProcessHeap(),0,prevEntry
);