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
22 #define WIN32_LEAN_AND_MEAN
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(cmd
);
29 typedef enum _DISPLAYTIME
36 typedef enum _DISPLAYORDER
44 static int file_total
, dir_total
, max_width
;
45 static ULONGLONG byte_total
;
46 static DISPLAYTIME dirTime
;
47 static DISPLAYORDER dirOrder
;
48 static BOOL orderReverse
, orderGroupDirs
, orderGroupDirsReverse
, orderByCol
;
49 static BOOL paged_mode
, recurse
, wide
, bare
, lower
, shortname
, usernames
, separator
;
50 static ULONG showattrs
, attrsbits
;
52 /*****************************************************************************
55 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
57 static WCHAR
* WCMD_strrev (WCHAR
*buff
) {
63 for (i
=0; i
<r
/2; i
++) {
65 buff
[i
] = buff
[r
-i
-1];
71 /*****************************************************************************
74 * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
75 * Result is returned in a static string overwritten with each call.
76 * FIXME: There must be a better algorithm!
78 static WCHAR
* WCMD_filesize64 (ULONGLONG n
) {
83 static WCHAR buff
[32];
88 if (separator
&& ((++i
)%3 == 1)) *p
++ = ',';
99 /*****************************************************************************
102 * Sort based on the /O options supplied on the command line
104 static int __cdecl
WCMD_dir_sort (const void *a
, const void *b
)
106 const WIN32_FIND_DATAW
*filea
= (const WIN32_FIND_DATAW
*)a
;
107 const WIN32_FIND_DATAW
*fileb
= (const WIN32_FIND_DATAW
*)b
;
110 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
111 requested sort order for the directory components */
112 if (orderGroupDirs
&&
113 ((filea
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
114 (fileb
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)))
116 BOOL aDir
= filea
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
117 if (aDir
) result
= -1;
119 if (orderGroupDirsReverse
) result
= -result
;
123 } else if (dirOrder
== Name
) {
124 result
= lstrcmpiW(filea
->cFileName
, fileb
->cFileName
);
127 } else if (dirOrder
== Size
) {
128 ULONG64 sizea
= (((ULONG64
)filea
->nFileSizeHigh
) << 32) + filea
->nFileSizeLow
;
129 ULONG64 sizeb
= (((ULONG64
)fileb
->nFileSizeHigh
) << 32) + fileb
->nFileSizeLow
;
130 if( sizea
< sizeb
) result
= -1;
131 else if( sizea
== sizeb
) result
= 0;
134 /* Order by Date: (Takes into account which date (/T option) */
135 } else if (dirOrder
== Date
) {
138 ULONG64 timea
, timeb
;
140 if (dirTime
== Written
) {
141 ft
= &filea
->ftLastWriteTime
;
142 timea
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
143 ft
= &fileb
->ftLastWriteTime
;
144 timeb
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
145 } else if (dirTime
== Access
) {
146 ft
= &filea
->ftLastAccessTime
;
147 timea
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
148 ft
= &fileb
->ftLastAccessTime
;
149 timeb
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
151 ft
= &filea
->ftCreationTime
;
152 timea
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
153 ft
= &fileb
->ftCreationTime
;
154 timeb
= (((ULONG64
)ft
->dwHighDateTime
) << 32) + ft
->dwLowDateTime
;
156 if( timea
< timeb
) result
= -1;
157 else if( timea
== timeb
) result
= 0;
160 /* Order by Extension: (Takes into account which date (/T option) */
161 } else if (dirOrder
== Extension
) {
164 WCHAR fname
[MAX_PATH
];
165 WCHAR extA
[MAX_PATH
];
166 WCHAR extB
[MAX_PATH
];
168 /* Split into components */
169 _wsplitpath(filea
->cFileName
, drive
, dir
, fname
, extA
);
170 _wsplitpath(fileb
->cFileName
, drive
, dir
, fname
, extB
);
171 result
= lstrcmpiW(extA
, extB
);
174 if (orderReverse
) result
= -result
;
178 /*****************************************************************************
181 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
183 static void WCMD_getfileowner(WCHAR
*filename
, WCHAR
*owner
, int ownerlen
) {
185 ULONG sizeNeeded
= 0;
187 WCHAR name
[MAXSTRING
];
188 WCHAR domain
[MAXSTRING
];
190 /* In case of error, return empty string */
193 /* Find out how much space we need for the owner security descriptor */
194 GetFileSecurityW(filename
, OWNER_SECURITY_INFORMATION
, 0, 0, &sizeNeeded
);
197 if(rc
== ERROR_INSUFFICIENT_BUFFER
&& sizeNeeded
> 0) {
201 BOOL defaulted
= FALSE
;
202 ULONG nameLen
= MAXSTRING
;
203 ULONG domainLen
= MAXSTRING
;
204 SID_NAME_USE nameuse
;
206 secBuffer
= heap_xalloc(sizeNeeded
* sizeof(BYTE
));
208 /* Get the owners security descriptor */
209 if(!GetFileSecurityW(filename
, OWNER_SECURITY_INFORMATION
, secBuffer
,
210 sizeNeeded
, &sizeNeeded
)) {
211 heap_free(secBuffer
);
215 /* Get the SID from the SD */
216 if(!GetSecurityDescriptorOwner(secBuffer
, &pSID
, &defaulted
)) {
217 heap_free(secBuffer
);
221 /* Convert to a username */
222 if (LookupAccountSidW(NULL
, pSID
, name
, &nameLen
, domain
, &domainLen
, &nameuse
)) {
223 swprintf(owner
, ownerlen
, L
"%s%c%s", domain
, '\\', name
);
225 heap_free(secBuffer
);
230 /*****************************************************************************
231 * WCMD_list_directory
233 * List a single file directory. This function (and those below it) can be called
234 * recursively when the /S switch is used.
236 * FIXME: Assumes 24-line display for the /P qualifier.
239 static DIRECTORY_STACK
*WCMD_list_directory (DIRECTORY_STACK
*inputparms
, int level
) {
241 WCHAR string
[1024], datestring
[32], timestring
[32];
242 WCHAR real_path
[MAX_PATH
];
243 WIN32_FIND_DATAW
*fd
;
247 int dir_count
, file_count
, entry_count
, i
, widest
, cur_width
, tmp_width
;
248 int numCols
, numRows
;
250 ULARGE_INTEGER byte_count
, file_size
;
251 DIRECTORY_STACK
*parms
;
252 int concurrentDirs
= 0;
253 BOOL done_header
= FALSE
;
255 static const WCHAR fmt2
[] = {'%','1','!','-','1','3','s','!','\0'};
256 static const WCHAR fmt3
[] = {'%','1','!','-','2','3','s','!','\0'};
257 static const WCHAR fmt4
[] = {'%','1','\0'};
258 static const WCHAR fmt5
[] = {'%','1','%','2','\0'};
263 byte_count
.QuadPart
= 0;
267 /* Loop merging all the files from consecutive parms which relate to the
268 same directory. Note issuing a directory header with no contents
269 mirrors what windows does */
271 fd
= heap_xalloc(sizeof(WIN32_FIND_DATAW
));
272 while (parms
&& lstrcmpW(inputparms
->dirName
, parms
->dirName
) == 0) {
275 /* Work out the full path + filename */
276 lstrcpyW(real_path
, parms
->dirName
);
277 lstrcatW(real_path
, parms
->fileName
);
279 /* Load all files into an in memory structure */
280 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path
));
281 hff
= FindFirstFileW(real_path
, &fd
[entry_count
]);
282 if (hff
!= INVALID_HANDLE_VALUE
) {
284 /* Skip any which are filtered out by attribute */
285 if ((fd
[entry_count
].dwFileAttributes
& attrsbits
) != showattrs
) continue;
289 /* Keep running track of longest filename for wide output */
290 if (wide
|| orderByCol
) {
291 int tmpLen
= lstrlenW(fd
[entry_count
-1].cFileName
) + 3;
292 if (fd
[entry_count
-1].dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) tmpLen
= tmpLen
+ 2;
293 if (tmpLen
> widest
) widest
= tmpLen
;
296 fd
= HeapReAlloc(GetProcessHeap(),0,fd
,(entry_count
+1)*sizeof(WIN32_FIND_DATAW
));
299 WINE_ERR("Out of memory\n");
303 } while (FindNextFileW(hff
, &fd
[entry_count
]) != 0);
307 /* Work out the actual current directory name without a trailing \ */
308 lstrcpyW(real_path
, parms
->dirName
);
309 real_path
[lstrlenW(parms
->dirName
)-1] = 0x00;
311 /* Output the results */
313 if (level
!= 0 && (entry_count
> 0)) WCMD_output_asis (newlineW
);
314 if (!recurse
|| ((entry_count
> 0) && done_header
==FALSE
)) {
315 WCMD_output (L
"Directory of %1\n\n", real_path
);
320 /* Move to next parm */
324 /* Handle case where everything is filtered out */
325 if (entry_count
> 0) {
327 /* Sort the list of files */
328 qsort (fd
, entry_count
, sizeof(WIN32_FIND_DATAW
), WCMD_dir_sort
);
330 /* Work out the number of columns */
331 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count
, max_width
, widest
);
332 if (wide
|| orderByCol
) {
333 numCols
= max(1, max_width
/ widest
);
334 numRows
= entry_count
/ numCols
;
335 if (entry_count
% numCols
) numRows
++;
338 numRows
= entry_count
;
340 WINE_TRACE("cols=%d, rows=%d\n", numCols
, numRows
);
342 for (rows
=0; rows
<numRows
; rows
++) {
343 BOOL addNewLine
= TRUE
;
344 for (cols
=0; cols
<numCols
; cols
++) {
347 /* Work out the index of the entry being pointed to */
349 i
= (cols
* numRows
) + rows
;
350 if (i
>= entry_count
) continue;
352 i
= (rows
* numCols
) + cols
;
353 if (i
>= entry_count
) continue;
356 /* /L convers all names to lower case */
358 WCHAR
*p
= fd
[i
].cFileName
;
359 while ( (*p
= tolower(*p
)) ) ++p
;
362 /* /Q gets file ownership information */
364 lstrcpyW (string
, inputparms
->dirName
);
365 lstrcatW (string
, fd
[i
].cFileName
);
366 WCMD_getfileowner(string
, username
, ARRAY_SIZE(username
));
369 if (dirTime
== Written
) {
370 FileTimeToLocalFileTime (&fd
[i
].ftLastWriteTime
, &ft
);
371 } else if (dirTime
== Access
) {
372 FileTimeToLocalFileTime (&fd
[i
].ftLastAccessTime
, &ft
);
374 FileTimeToLocalFileTime (&fd
[i
].ftCreationTime
, &ft
);
376 FileTimeToSystemTime (&ft
, &st
);
377 GetDateFormatW(0, DATE_SHORTDATE
, &st
, NULL
, datestring
, ARRAY_SIZE(datestring
));
378 GetTimeFormatW(0, TIME_NOSECONDS
, &st
, NULL
, timestring
, ARRAY_SIZE(timestring
));
382 tmp_width
= cur_width
;
383 if (fd
[i
].dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
384 WCMD_output (L
"[%1]", fd
[i
].cFileName
);
386 tmp_width
= tmp_width
+ lstrlenW(fd
[i
].cFileName
) + 2;
388 WCMD_output (L
"%1", fd
[i
].cFileName
);
389 tmp_width
= tmp_width
+ lstrlenW(fd
[i
].cFileName
) ;
391 file_size
.u
.LowPart
= fd
[i
].nFileSizeLow
;
392 file_size
.u
.HighPart
= fd
[i
].nFileSizeHigh
;
393 byte_count
.QuadPart
+= file_size
.QuadPart
;
395 cur_width
= cur_width
+ widest
;
397 if ((cur_width
+ widest
) > max_width
) {
400 WCMD_output(L
"%1!*s!", cur_width
- tmp_width
, nullW
);
403 } else if (fd
[i
].dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
407 WCMD_output (L
"%1!10s! %2!8s! <DIR> ", datestring
, timestring
);
408 if (shortname
) WCMD_output (fmt2
, fd
[i
].cAlternateFileName
);
409 if (usernames
) WCMD_output (fmt3
, username
);
410 WCMD_output(fmt4
,fd
[i
].cFileName
);
412 if (!((lstrcmpW(fd
[i
].cFileName
, dotW
) == 0) ||
413 (lstrcmpW(fd
[i
].cFileName
, dotdotW
) == 0))) {
414 WCMD_output (fmt5
, recurse
?inputparms
->dirName
:nullW
, fd
[i
].cFileName
);
422 file_size
.u
.LowPart
= fd
[i
].nFileSizeLow
;
423 file_size
.u
.HighPart
= fd
[i
].nFileSizeHigh
;
424 byte_count
.QuadPart
+= file_size
.QuadPart
;
426 WCMD_output (L
"%1!10s! %2!8s! %3!10s! ", datestring
, timestring
,
427 WCMD_filesize64(file_size
.QuadPart
));
428 if (shortname
) WCMD_output (fmt2
, fd
[i
].cAlternateFileName
);
429 if (usernames
) WCMD_output (fmt3
, username
);
430 WCMD_output(fmt4
,fd
[i
].cFileName
);
432 WCMD_output (fmt5
, recurse
?inputparms
->dirName
:nullW
, fd
[i
].cFileName
);
436 if (addNewLine
) WCMD_output_asis (newlineW
);
441 if (file_count
== 1) {
442 WCMD_output (L
" 1 file %1!25s! bytes\n", WCMD_filesize64 (byte_count
.QuadPart
));
445 WCMD_output (L
"%1!8d! files %2!24s! bytes\n", file_count
, WCMD_filesize64 (byte_count
.QuadPart
));
448 byte_total
= byte_total
+ byte_count
.QuadPart
;
449 file_total
= file_total
+ file_count
;
450 dir_total
= dir_total
+ dir_count
;
452 if (!bare
&& !recurse
) {
453 if (dir_count
== 1) {
454 WCMD_output (L
"%1!8d! directory ", 1);
456 WCMD_output (L
"%1!8d! directories", dir_count
);
462 /* When recursing, look in all subdirectories for matches */
464 DIRECTORY_STACK
*dirStack
= NULL
;
465 DIRECTORY_STACK
*lastEntry
= NULL
;
466 WIN32_FIND_DATAW finddata
;
468 /* Build path to search */
469 lstrcpyW(string
, inputparms
->dirName
);
470 lstrcatW(string
, starW
);
472 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string
));
473 hff
= FindFirstFileW(string
, &finddata
);
474 if (hff
!= INVALID_HANDLE_VALUE
) {
476 if ((finddata
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
477 (lstrcmpW(finddata
.cFileName
, dotdotW
) != 0) &&
478 (lstrcmpW(finddata
.cFileName
, dotW
) != 0)) {
480 DIRECTORY_STACK
*thisDir
;
481 int dirsToCopy
= concurrentDirs
;
483 /* Loop creating list of subdirs for all concurrent entries */
485 while (dirsToCopy
> 0) {
488 /* Work out search parameter in sub dir */
489 lstrcpyW (string
, inputparms
->dirName
);
490 lstrcatW (string
, finddata
.cFileName
);
491 lstrcatW (string
, slashW
);
492 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string
));
494 /* Allocate memory, add to list */
495 thisDir
= heap_xalloc(sizeof(DIRECTORY_STACK
));
496 if (dirStack
== NULL
) dirStack
= thisDir
;
497 if (lastEntry
!= NULL
) lastEntry
->next
= thisDir
;
499 thisDir
->next
= NULL
;
500 thisDir
->dirName
= heap_strdupW(string
);
501 thisDir
->fileName
= heap_strdupW(parms
->fileName
);
505 } while (FindNextFileW(hff
, &finddata
) != 0);
508 while (dirStack
!= NULL
) {
509 DIRECTORY_STACK
*thisDir
= dirStack
;
510 dirStack
= WCMD_list_directory (thisDir
, 1);
511 while (thisDir
!= dirStack
) {
512 DIRECTORY_STACK
*tempDir
= thisDir
->next
;
513 heap_free(thisDir
->dirName
);
514 heap_free(thisDir
->fileName
);
522 /* Handle case where everything is filtered out */
523 if ((file_total
+ dir_total
== 0) && (level
== 0)) {
524 SetLastError (ERROR_FILE_NOT_FOUND
);
532 /*****************************************************************************
535 * Print out the trailer for the supplied drive letter
537 static void WCMD_dir_trailer(WCHAR drive
) {
538 ULARGE_INTEGER avail
, total
, freebytes
;
540 WCHAR driveName
[] = L
"c:\\";
542 driveName
[0] = drive
;
543 status
= GetDiskFreeSpaceExW(driveName
, &avail
, &total
, &freebytes
);
544 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName
),
545 status
, GetLastError());
547 if (errorlevel
==0 && !bare
) {
549 WCMD_output (L
"\n Total files listed:\n%1!8d! files%2!25s! bytes\n", file_total
, WCMD_filesize64 (byte_total
));
550 WCMD_output (L
"%1!8d! directories %2!18s! bytes free\n\n", dir_total
, WCMD_filesize64 (freebytes
.QuadPart
));
552 WCMD_output (L
" %1!18s! bytes free\n\n", WCMD_filesize64 (freebytes
.QuadPart
));
557 /*****************************************************************************
560 * List a file directory.
564 void WCMD_directory (WCHAR
*args
)
566 WCHAR path
[MAX_PATH
], cwd
[MAX_PATH
];
568 CONSOLE_SCREEN_BUFFER_INFO consoleInfo
;
570 WCHAR string
[MAXSTRING
];
574 BOOL trailerReqd
= FALSE
;
575 DIRECTORY_STACK
*fullParms
= NULL
;
576 DIRECTORY_STACK
*prevEntry
= NULL
;
577 DIRECTORY_STACK
*thisEntry
= NULL
;
580 WCHAR fname
[MAX_PATH
];
585 /* Prefill quals with (uppercased) DIRCMD env var */
586 if (GetEnvironmentVariableW(L
"DIRCMD", string
, ARRAY_SIZE(string
))) {
588 while ( (*p
= toupper(*p
)) ) ++p
;
589 lstrcatW(string
,quals
);
590 lstrcpyW(quals
, string
);
594 file_total
= dir_total
= 0;
596 /* Initialize all flags to their defaults as if no DIRCMD or quals */
608 orderReverse
= FALSE
;
609 orderGroupDirs
= FALSE
;
610 orderGroupDirsReverse
= FALSE
;
612 attrsbits
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
614 /* Handle args - Loop through so right most is the effective one */
615 /* Note: /- appears to be a negate rather than an off, eg. dir
616 /-W is wide, or dir /w /-w /-w is also wide */
618 while (*p
&& (*p
=='/' || *p
==' ')) {
620 if (*p
++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
627 WINE_TRACE("Processing arg '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
629 case 'P': if (negate
) paged_mode
= !paged_mode
;
630 else paged_mode
= TRUE
;
632 case 'S': if (negate
) recurse
= !recurse
;
635 case 'W': if (negate
) wide
= !wide
;
638 case 'B': if (negate
) bare
= !bare
;
641 case 'L': if (negate
) lower
= !lower
;
644 case 'X': if (negate
) shortname
= !shortname
;
645 else shortname
= TRUE
;
647 case 'Q': if (negate
) usernames
= !usernames
;
648 else usernames
= TRUE
;
650 case 'D': if (negate
) orderByCol
= !orderByCol
;
651 else orderByCol
= TRUE
;
653 case 'C': if (negate
) separator
= !separator
;
654 else separator
= TRUE
;
657 if (*p
==':') p
++; /* Skip optional : */
659 if (*p
== 'A') dirTime
= Access
;
660 else if (*p
== 'C') dirTime
= Creation
;
661 else if (*p
== 'W') dirTime
= Written
;
663 /* Support /T and /T: with no parms, default to written */
664 else if (*p
== 0x00 || *p
== '/') {
666 p
= p
- 1; /* So when step on, move to '/' */
668 SetLastError(ERROR_INVALID_PARAMETER
);
675 if (*p
==':') p
++; /* Skip optional : */
676 while (*p
&& *p
!= '/') {
677 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
679 case 'N': dirOrder
= Name
; break;
680 case 'E': dirOrder
= Extension
; break;
681 case 'S': dirOrder
= Size
; break;
682 case 'D': dirOrder
= Date
; break;
683 case '-': if (*(p
+1)=='G') orderGroupDirsReverse
=TRUE
;
684 else orderReverse
= TRUE
;
686 case 'G': orderGroupDirs
= TRUE
; break;
688 SetLastError(ERROR_INVALID_PARAMETER
);
695 p
= p
- 1; /* So when step on, move to '/' */
700 if (*p
==':') p
++; /* Skip optional : */
701 while (*p
&& *p
!= '/') {
702 BOOL anegate
= FALSE
;
705 /* Note /A: - options are 'offs' not toggles */
711 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
713 case 'D': mask
= FILE_ATTRIBUTE_DIRECTORY
; break;
714 case 'H': mask
= FILE_ATTRIBUTE_HIDDEN
; break;
715 case 'S': mask
= FILE_ATTRIBUTE_SYSTEM
; break;
716 case 'R': mask
= FILE_ATTRIBUTE_READONLY
; break;
717 case 'A': mask
= FILE_ATTRIBUTE_ARCHIVE
; break;
719 SetLastError(ERROR_INVALID_PARAMETER
);
725 /* Keep running list of bits we care about */
728 /* Mask shows what MUST be in the bits we care about */
729 if (anegate
) showattrs
= showattrs
& ~mask
;
730 else showattrs
|= mask
;
734 p
= p
- 1; /* So when step on, move to '/' */
735 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs
, attrsbits
);
738 SetLastError(ERROR_INVALID_PARAMETER
);
746 /* Handle conflicting args and initialization */
747 if (bare
|| shortname
) wide
= FALSE
;
748 if (bare
) shortname
= FALSE
;
749 if (wide
) usernames
= FALSE
;
750 if (orderByCol
) wide
= TRUE
;
753 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &consoleInfo
))
754 max_width
= consoleInfo
.dwSize
.X
;
759 WCMD_enter_paged_mode(NULL
);
764 GetCurrentDirectoryW(MAX_PATH
, cwd
);
765 lstrcatW(cwd
, slashW
);
767 /* Loop through all args, calculating full effective directory */
771 WCHAR fullname
[MAXSTRING
];
772 WCHAR
*thisArg
= WCMD_parameter(args
, argno
++, &argN
, FALSE
, FALSE
);
773 if (argN
&& argN
[0] != '/') {
775 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg
));
776 if (thisArg
[1] == ':' && thisArg
[2] == '\\') {
777 lstrcpyW(fullname
, thisArg
);
778 } else if (thisArg
[1] == ':' && thisArg
[2] != '\\') {
780 wsprintfW(envvar
, L
"=%c:", thisArg
[0]);
781 if (!GetEnvironmentVariableW(envvar
, fullname
, MAX_PATH
)) {
782 wsprintfW(fullname
, L
"%c:", thisArg
[0]);
784 lstrcatW(fullname
, slashW
);
785 lstrcatW(fullname
, &thisArg
[2]);
786 } else if (thisArg
[0] == '\\') {
787 memcpy(fullname
, cwd
, 2 * sizeof(WCHAR
));
788 lstrcpyW(fullname
+2, thisArg
);
790 lstrcpyW(fullname
, cwd
);
791 lstrcatW(fullname
, thisArg
);
793 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname
));
795 status
= GetFullPathNameW(fullname
, ARRAY_SIZE(path
), path
, NULL
);
798 * If the path supplied does not include a wildcard, and the endpoint of the
799 * path references a directory, we need to list the *contents* of that
800 * directory not the directory file itself.
802 if ((wcschr(path
, '*') == NULL
) && (wcschr(path
, '%') == NULL
)) {
803 status
= GetFileAttributesW(path
);
804 if ((status
!= INVALID_FILE_ATTRIBUTES
) && (status
& FILE_ATTRIBUTE_DIRECTORY
)) {
805 if (!ends_with_backslash( path
)) lstrcatW( path
, slashW
);
806 lstrcatW (path
, starW
);
809 /* Special case wildcard search with no extension (ie parameters ending in '.') as
810 GetFullPathName strips off the additional '.' */
811 if (fullname
[lstrlenW(fullname
)-1] == '.') lstrcatW(path
, dotW
);
814 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path
));
815 thisEntry
= heap_xalloc(sizeof(DIRECTORY_STACK
));
816 if (fullParms
== NULL
) fullParms
= thisEntry
;
817 if (prevEntry
!= NULL
) prevEntry
->next
= thisEntry
;
818 prevEntry
= thisEntry
;
819 thisEntry
->next
= NULL
;
821 /* Split into components */
822 _wsplitpath(path
, drive
, dir
, fname
, ext
);
823 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
824 wine_dbgstr_w(drive
), wine_dbgstr_w(dir
),
825 wine_dbgstr_w(fname
), wine_dbgstr_w(ext
));
827 thisEntry
->dirName
= heap_xalloc(sizeof(WCHAR
) * (lstrlenW(drive
)+lstrlenW(dir
)+1));
828 lstrcpyW(thisEntry
->dirName
, drive
);
829 lstrcatW(thisEntry
->dirName
, dir
);
831 thisEntry
->fileName
= heap_xalloc(sizeof(WCHAR
) * (lstrlenW(fname
)+lstrlenW(ext
)+1));
832 lstrcpyW(thisEntry
->fileName
, fname
);
833 lstrcatW(thisEntry
->fileName
, ext
);
838 /* If just 'dir' entered, a '*' parameter is assumed */
839 if (fullParms
== NULL
) {
840 WINE_TRACE("Inserting default '*'\n");
841 fullParms
= heap_xalloc(sizeof(DIRECTORY_STACK
));
842 fullParms
->next
= NULL
;
843 fullParms
->dirName
= heap_strdupW(cwd
);
844 fullParms
->fileName
= heap_strdupW(starW
);
849 thisEntry
= fullParms
;
852 while (thisEntry
!= NULL
) {
854 /* Output disk free (trailer) and volume information (header) if the drive
856 if (lastDrive
!= toupper(thisEntry
->dirName
[0])) {
858 /* Trailer Information */
859 if (lastDrive
!= '?') {
861 WCMD_dir_trailer(prevEntry
->dirName
[0]);
864 lastDrive
= toupper(thisEntry
->dirName
[0]);
869 WINE_TRACE("Writing volume for '%c:'\n", thisEntry
->dirName
[0]);
870 memcpy(drive
, thisEntry
->dirName
, 2 * sizeof(WCHAR
));
872 status
= WCMD_volume (0, drive
);
880 if (!bare
) WCMD_output_asis (L
"\n\n");
883 /* Clear any errors from previous invocations, and process it */
885 prevEntry
= thisEntry
;
886 thisEntry
= WCMD_list_directory (thisEntry
, 0);
889 /* Trailer Information */
891 WCMD_dir_trailer(prevEntry
->dirName
[0]);
895 if (paged_mode
) WCMD_leave_paged_mode();
897 /* Free storage allocated for parms */
898 while (fullParms
!= NULL
) {
899 prevEntry
= fullParms
;
900 fullParms
= prevEntry
->next
;
901 heap_free(prevEntry
->dirName
);
902 heap_free(prevEntry
->fileName
);
903 heap_free(prevEntry
);