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 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 WCMD_splitpath(filea
->cFileName
, drive
, dir
, fname
, extA
);
170 WCMD_splitpath(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 static const WCHAR fmt
[] = {'%','s','%','c','%','s','\0'};
224 snprintfW(owner
, ownerlen
, fmt
, domain
, '\\', name
);
226 heap_free(secBuffer
);
231 /*****************************************************************************
232 * WCMD_list_directory
234 * List a single file directory. This function (and those below it) can be called
235 * recursively when the /S switch is used.
237 * FIXME: Assumes 24-line display for the /P qualifier.
240 static DIRECTORY_STACK
*WCMD_list_directory (DIRECTORY_STACK
*inputparms
, int level
) {
242 WCHAR string
[1024], datestring
[32], timestring
[32];
243 WCHAR real_path
[MAX_PATH
];
244 WIN32_FIND_DATAW
*fd
;
248 int dir_count
, file_count
, entry_count
, i
, widest
, cur_width
, tmp_width
;
249 int numCols
, numRows
;
251 ULARGE_INTEGER byte_count
, file_size
;
252 DIRECTORY_STACK
*parms
;
253 int concurrentDirs
= 0;
254 BOOL done_header
= FALSE
;
256 static const WCHAR fmtDir
[] = {'%','1','!','1','0','s','!',' ',' ','%','2','!','8','s','!',' ',' ',
257 '<','D','I','R','>',' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
258 static const WCHAR fmtFile
[] = {'%','1','!','1','0','s','!',' ',' ','%','2','!','8','s','!',' ',' ',
259 ' ',' ','%','3','!','1','0','s','!',' ',' ','\0'};
260 static const WCHAR fmt2
[] = {'%','1','!','-','1','3','s','!','\0'};
261 static const WCHAR fmt3
[] = {'%','1','!','-','2','3','s','!','\0'};
262 static const WCHAR fmt4
[] = {'%','1','\0'};
263 static const WCHAR fmt5
[] = {'%','1','%','2','\0'};
268 byte_count
.QuadPart
= 0;
272 /* Loop merging all the files from consecutive parms which relate to the
273 same directory. Note issuing a directory header with no contents
274 mirrors what windows does */
276 fd
= heap_xalloc(sizeof(WIN32_FIND_DATAW
));
277 while (parms
&& strcmpW(inputparms
->dirName
, parms
->dirName
) == 0) {
280 /* Work out the full path + filename */
281 strcpyW(real_path
, parms
->dirName
);
282 strcatW(real_path
, parms
->fileName
);
284 /* Load all files into an in memory structure */
285 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path
));
286 hff
= FindFirstFileW(real_path
, &fd
[entry_count
]);
287 if (hff
!= INVALID_HANDLE_VALUE
) {
289 /* Skip any which are filtered out by attribute */
290 if ((fd
[entry_count
].dwFileAttributes
& attrsbits
) != showattrs
) continue;
294 /* Keep running track of longest filename for wide output */
295 if (wide
|| orderByCol
) {
296 int tmpLen
= strlenW(fd
[entry_count
-1].cFileName
) + 3;
297 if (fd
[entry_count
-1].dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) tmpLen
= tmpLen
+ 2;
298 if (tmpLen
> widest
) widest
= tmpLen
;
301 fd
= HeapReAlloc(GetProcessHeap(),0,fd
,(entry_count
+1)*sizeof(WIN32_FIND_DATAW
));
304 WINE_ERR("Out of memory\n");
308 } while (FindNextFileW(hff
, &fd
[entry_count
]) != 0);
312 /* Work out the actual current directory name without a trailing \ */
313 strcpyW(real_path
, parms
->dirName
);
314 real_path
[strlenW(parms
->dirName
)-1] = 0x00;
316 /* Output the results */
318 if (level
!= 0 && (entry_count
> 0)) WCMD_output_asis (newlineW
);
319 if (!recurse
|| ((entry_count
> 0) && done_header
==FALSE
)) {
320 static const WCHAR headerW
[] = {'D','i','r','e','c','t','o','r','y',' ','o','f',
321 ' ','%','1','\n','\n','\0'};
322 WCMD_output (headerW
, real_path
);
327 /* Move to next parm */
331 /* Handle case where everything is filtered out */
332 if (entry_count
> 0) {
334 /* Sort the list of files */
335 qsort (fd
, entry_count
, sizeof(WIN32_FIND_DATAW
), WCMD_dir_sort
);
337 /* Work out the number of columns */
338 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count
, max_width
, widest
);
339 if (wide
|| orderByCol
) {
340 numCols
= max(1, (int)max_width
/ widest
);
341 numRows
= entry_count
/ numCols
;
342 if (entry_count
% numCols
) numRows
++;
345 numRows
= entry_count
;
347 WINE_TRACE("cols=%d, rows=%d\n", numCols
, numRows
);
349 for (rows
=0; rows
<numRows
; rows
++) {
350 BOOL addNewLine
= TRUE
;
351 for (cols
=0; cols
<numCols
; cols
++) {
354 /* Work out the index of the entry being pointed to */
356 i
= (cols
* numRows
) + rows
;
357 if (i
>= entry_count
) continue;
359 i
= (rows
* numCols
) + cols
;
360 if (i
>= entry_count
) continue;
363 /* /L convers all names to lower case */
365 WCHAR
*p
= fd
[i
].cFileName
;
366 while ( (*p
= tolower(*p
)) ) ++p
;
369 /* /Q gets file ownership information */
371 strcpyW (string
, inputparms
->dirName
);
372 strcatW (string
, fd
[i
].cFileName
);
373 WCMD_getfileowner(string
, username
, ARRAY_SIZE(username
));
376 if (dirTime
== Written
) {
377 FileTimeToLocalFileTime (&fd
[i
].ftLastWriteTime
, &ft
);
378 } else if (dirTime
== Access
) {
379 FileTimeToLocalFileTime (&fd
[i
].ftLastAccessTime
, &ft
);
381 FileTimeToLocalFileTime (&fd
[i
].ftCreationTime
, &ft
);
383 FileTimeToSystemTime (&ft
, &st
);
384 GetDateFormatW(0, DATE_SHORTDATE
, &st
, NULL
, datestring
, ARRAY_SIZE(datestring
));
385 GetTimeFormatW(0, TIME_NOSECONDS
, &st
, NULL
, timestring
, ARRAY_SIZE(timestring
));
389 tmp_width
= cur_width
;
390 if (fd
[i
].dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
391 static const WCHAR fmt
[] = {'[','%','1',']','\0'};
392 WCMD_output (fmt
, fd
[i
].cFileName
);
394 tmp_width
= tmp_width
+ strlenW(fd
[i
].cFileName
) + 2;
396 static const WCHAR fmt
[] = {'%','1','\0'};
397 WCMD_output (fmt
, fd
[i
].cFileName
);
398 tmp_width
= tmp_width
+ strlenW(fd
[i
].cFileName
) ;
400 file_size
.u
.LowPart
= fd
[i
].nFileSizeLow
;
401 file_size
.u
.HighPart
= fd
[i
].nFileSizeHigh
;
402 byte_count
.QuadPart
+= file_size
.QuadPart
;
404 cur_width
= cur_width
+ widest
;
406 if ((cur_width
+ widest
) > max_width
) {
409 static const WCHAR padfmt
[] = {'%','1','!','*','s','!','\0'};
410 WCMD_output(padfmt
, cur_width
- tmp_width
, nullW
);
413 } else if (fd
[i
].dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
417 WCMD_output (fmtDir
, datestring
, timestring
);
418 if (shortname
) WCMD_output (fmt2
, fd
[i
].cAlternateFileName
);
419 if (usernames
) WCMD_output (fmt3
, username
);
420 WCMD_output(fmt4
,fd
[i
].cFileName
);
422 if (!((strcmpW(fd
[i
].cFileName
, dotW
) == 0) ||
423 (strcmpW(fd
[i
].cFileName
, dotdotW
) == 0))) {
424 WCMD_output (fmt5
, recurse
?inputparms
->dirName
:nullW
, fd
[i
].cFileName
);
432 file_size
.u
.LowPart
= fd
[i
].nFileSizeLow
;
433 file_size
.u
.HighPart
= fd
[i
].nFileSizeHigh
;
434 byte_count
.QuadPart
+= file_size
.QuadPart
;
436 WCMD_output (fmtFile
, datestring
, timestring
,
437 WCMD_filesize64(file_size
.QuadPart
));
438 if (shortname
) WCMD_output (fmt2
, fd
[i
].cAlternateFileName
);
439 if (usernames
) WCMD_output (fmt3
, username
);
440 WCMD_output(fmt4
,fd
[i
].cFileName
);
442 WCMD_output (fmt5
, recurse
?inputparms
->dirName
:nullW
, fd
[i
].cFileName
);
446 if (addNewLine
) WCMD_output_asis (newlineW
);
451 if (file_count
== 1) {
452 static const WCHAR fmt
[] = {' ',' ',' ',' ',' ',' ',' ','1',' ','f','i','l','e',' ',
453 '%','1','!','2','5','s','!',' ','b','y','t','e','s','\n','\0'};
454 WCMD_output (fmt
, WCMD_filesize64 (byte_count
.QuadPart
));
457 static const WCHAR fmt
[] = {'%','1','!','8','d','!',' ','f','i','l','e','s',' ','%','2','!','2','4','s','!',
458 ' ','b','y','t','e','s','\n','\0'};
459 WCMD_output (fmt
, file_count
, WCMD_filesize64 (byte_count
.QuadPart
));
462 byte_total
= byte_total
+ byte_count
.QuadPart
;
463 file_total
= file_total
+ file_count
;
464 dir_total
= dir_total
+ dir_count
;
466 if (!bare
&& !recurse
) {
467 if (dir_count
== 1) {
468 static const WCHAR fmt
[] = {'%','1','!','8','d','!',' ','d','i','r','e','c','t','o','r','y',
469 ' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
470 WCMD_output (fmt
, 1);
472 static const WCHAR fmt
[] = {'%','1','!','8','d','!',' ','d','i','r','e','c','t','o','r','i',
474 WCMD_output (fmt
, dir_count
);
480 /* When recursing, look in all subdirectories for matches */
482 DIRECTORY_STACK
*dirStack
= NULL
;
483 DIRECTORY_STACK
*lastEntry
= NULL
;
484 WIN32_FIND_DATAW finddata
;
486 /* Build path to search */
487 strcpyW(string
, inputparms
->dirName
);
488 strcatW(string
, starW
);
490 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string
));
491 hff
= FindFirstFileW(string
, &finddata
);
492 if (hff
!= INVALID_HANDLE_VALUE
) {
494 if ((finddata
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) &&
495 (strcmpW(finddata
.cFileName
, dotdotW
) != 0) &&
496 (strcmpW(finddata
.cFileName
, dotW
) != 0)) {
498 DIRECTORY_STACK
*thisDir
;
499 int dirsToCopy
= concurrentDirs
;
501 /* Loop creating list of subdirs for all concurrent entries */
503 while (dirsToCopy
> 0) {
506 /* Work out search parameter in sub dir */
507 strcpyW (string
, inputparms
->dirName
);
508 strcatW (string
, finddata
.cFileName
);
509 strcatW (string
, slashW
);
510 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string
));
512 /* Allocate memory, add to list */
513 thisDir
= heap_xalloc(sizeof(DIRECTORY_STACK
));
514 if (dirStack
== NULL
) dirStack
= thisDir
;
515 if (lastEntry
!= NULL
) lastEntry
->next
= thisDir
;
517 thisDir
->next
= NULL
;
518 thisDir
->dirName
= heap_strdupW(string
);
519 thisDir
->fileName
= heap_strdupW(parms
->fileName
);
523 } while (FindNextFileW(hff
, &finddata
) != 0);
526 while (dirStack
!= NULL
) {
527 DIRECTORY_STACK
*thisDir
= dirStack
;
528 dirStack
= WCMD_list_directory (thisDir
, 1);
529 while (thisDir
!= dirStack
) {
530 DIRECTORY_STACK
*tempDir
= thisDir
->next
;
531 heap_free(thisDir
->dirName
);
532 heap_free(thisDir
->fileName
);
540 /* Handle case where everything is filtered out */
541 if ((file_total
+ dir_total
== 0) && (level
== 0)) {
542 SetLastError (ERROR_FILE_NOT_FOUND
);
550 /*****************************************************************************
553 * Print out the trailer for the supplied drive letter
555 static void WCMD_dir_trailer(WCHAR drive
) {
556 ULARGE_INTEGER avail
, total
, freebytes
;
558 WCHAR driveName
[] = {'c',':','\\','\0'};
560 driveName
[0] = drive
;
561 status
= GetDiskFreeSpaceExW(driveName
, &avail
, &total
, &freebytes
);
562 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName
),
563 status
, GetLastError());
565 if (errorlevel
==0 && !bare
) {
567 static const WCHAR fmt1
[] = {'\n',' ',' ',' ',' ',' ','T','o','t','a','l',' ','f','i','l','e','s',
568 ' ','l','i','s','t','e','d',':','\n','%','1','!','8','d','!',' ','f','i','l','e',
569 's','%','2','!','2','5','s','!',' ','b','y','t','e','s','\n','\0'};
570 static const WCHAR fmt2
[] = {'%','1','!','8','d','!',' ','d','i','r','e','c','t','o','r','i','e','s',' ','%',
571 '2','!','1','8','s','!',' ','b','y','t','e','s',' ','f','r','e','e','\n','\n',
573 WCMD_output (fmt1
, file_total
, WCMD_filesize64 (byte_total
));
574 WCMD_output (fmt2
, dir_total
, WCMD_filesize64 (freebytes
.QuadPart
));
576 static const WCHAR fmt
[] = {' ','%','1','!','1','8','s','!',' ','b','y','t','e','s',' ','f','r','e','e',
578 WCMD_output (fmt
, WCMD_filesize64 (freebytes
.QuadPart
));
583 /*****************************************************************************
586 * List a file directory.
590 void WCMD_directory (WCHAR
*args
)
592 WCHAR path
[MAX_PATH
], cwd
[MAX_PATH
];
594 CONSOLE_SCREEN_BUFFER_INFO consoleInfo
;
596 WCHAR string
[MAXSTRING
];
600 BOOL trailerReqd
= FALSE
;
601 DIRECTORY_STACK
*fullParms
= NULL
;
602 DIRECTORY_STACK
*prevEntry
= NULL
;
603 DIRECTORY_STACK
*thisEntry
= NULL
;
606 WCHAR fname
[MAX_PATH
];
608 static const WCHAR dircmdW
[] = {'D','I','R','C','M','D','\0'};
612 /* Prefill quals with (uppercased) DIRCMD env var */
613 if (GetEnvironmentVariableW(dircmdW
, string
, ARRAY_SIZE(string
))) {
615 while ( (*p
= toupper(*p
)) ) ++p
;
616 strcatW(string
,quals
);
617 strcpyW(quals
, string
);
621 file_total
= dir_total
= 0;
623 /* Initialize all flags to their defaults as if no DIRCMD or quals */
635 orderReverse
= FALSE
;
636 orderGroupDirs
= FALSE
;
637 orderGroupDirsReverse
= FALSE
;
639 attrsbits
= FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
;
641 /* Handle args - Loop through so right most is the effective one */
642 /* Note: /- appears to be a negate rather than an off, eg. dir
643 /-W is wide, or dir /w /-w /-w is also wide */
645 while (*p
&& (*p
=='/' || *p
==' ')) {
647 if (*p
++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
654 WINE_TRACE("Processing arg '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
656 case 'P': if (negate
) paged_mode
= !paged_mode
;
657 else paged_mode
= TRUE
;
659 case 'S': if (negate
) recurse
= !recurse
;
662 case 'W': if (negate
) wide
= !wide
;
665 case 'B': if (negate
) bare
= !bare
;
668 case 'L': if (negate
) lower
= !lower
;
671 case 'X': if (negate
) shortname
= !shortname
;
672 else shortname
= TRUE
;
674 case 'Q': if (negate
) usernames
= !usernames
;
675 else usernames
= TRUE
;
677 case 'D': if (negate
) orderByCol
= !orderByCol
;
678 else orderByCol
= TRUE
;
680 case 'C': if (negate
) separator
= !separator
;
681 else separator
= TRUE
;
684 if (*p
==':') p
++; /* Skip optional : */
686 if (*p
== 'A') dirTime
= Access
;
687 else if (*p
== 'C') dirTime
= Creation
;
688 else if (*p
== 'W') dirTime
= Written
;
690 /* Support /T and /T: with no parms, default to written */
691 else if (*p
== 0x00 || *p
== '/') {
693 p
= p
- 1; /* So when step on, move to '/' */
695 SetLastError(ERROR_INVALID_PARAMETER
);
702 if (*p
==':') p
++; /* Skip optional : */
703 while (*p
&& *p
!= '/') {
704 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
706 case 'N': dirOrder
= Name
; break;
707 case 'E': dirOrder
= Extension
; break;
708 case 'S': dirOrder
= Size
; break;
709 case 'D': dirOrder
= Date
; break;
710 case '-': if (*(p
+1)=='G') orderGroupDirsReverse
=TRUE
;
711 else orderReverse
= TRUE
;
713 case 'G': orderGroupDirs
= TRUE
; break;
715 SetLastError(ERROR_INVALID_PARAMETER
);
722 p
= p
- 1; /* So when step on, move to '/' */
727 if (*p
==':') p
++; /* Skip optional : */
728 while (*p
&& *p
!= '/') {
729 BOOL anegate
= FALSE
;
732 /* Note /A: - options are 'offs' not toggles */
738 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p
, wine_dbgstr_w(quals
));
740 case 'D': mask
= FILE_ATTRIBUTE_DIRECTORY
; break;
741 case 'H': mask
= FILE_ATTRIBUTE_HIDDEN
; break;
742 case 'S': mask
= FILE_ATTRIBUTE_SYSTEM
; break;
743 case 'R': mask
= FILE_ATTRIBUTE_READONLY
; break;
744 case 'A': mask
= FILE_ATTRIBUTE_ARCHIVE
; break;
746 SetLastError(ERROR_INVALID_PARAMETER
);
752 /* Keep running list of bits we care about */
755 /* Mask shows what MUST be in the bits we care about */
756 if (anegate
) showattrs
= showattrs
& ~mask
;
757 else showattrs
|= mask
;
761 p
= p
- 1; /* So when step on, move to '/' */
762 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs
, attrsbits
);
765 SetLastError(ERROR_INVALID_PARAMETER
);
773 /* Handle conflicting args and initialization */
774 if (bare
|| shortname
) wide
= FALSE
;
775 if (bare
) shortname
= FALSE
;
776 if (wide
) usernames
= FALSE
;
777 if (orderByCol
) wide
= TRUE
;
780 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &consoleInfo
))
781 max_width
= consoleInfo
.dwSize
.X
;
786 WCMD_enter_paged_mode(NULL
);
791 GetCurrentDirectoryW(MAX_PATH
, cwd
);
792 strcatW(cwd
, slashW
);
794 /* Loop through all args, calculating full effective directory */
798 WCHAR fullname
[MAXSTRING
];
799 WCHAR
*thisArg
= WCMD_parameter(args
, argno
++, &argN
, FALSE
, FALSE
);
800 if (argN
&& argN
[0] != '/') {
802 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg
));
803 if (thisArg
[1] == ':' && thisArg
[2] == '\\') {
804 strcpyW(fullname
, thisArg
);
805 } else if (thisArg
[1] == ':' && thisArg
[2] != '\\') {
807 static const WCHAR envFmt
[] = {'=','%','c',':','\0'};
808 wsprintfW(envvar
, envFmt
, thisArg
[0]);
809 if (!GetEnvironmentVariableW(envvar
, fullname
, MAX_PATH
)) {
810 static const WCHAR noEnvFmt
[] = {'%','c',':','\0'};
811 wsprintfW(fullname
, noEnvFmt
, thisArg
[0]);
813 strcatW(fullname
, slashW
);
814 strcatW(fullname
, &thisArg
[2]);
815 } else if (thisArg
[0] == '\\') {
816 memcpy(fullname
, cwd
, 2 * sizeof(WCHAR
));
817 strcpyW(fullname
+2, thisArg
);
819 strcpyW(fullname
, cwd
);
820 strcatW(fullname
, thisArg
);
822 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname
));
824 status
= GetFullPathNameW(fullname
, ARRAY_SIZE(path
), path
, NULL
);
827 * If the path supplied does not include a wildcard, and the endpoint of the
828 * path references a directory, we need to list the *contents* of that
829 * directory not the directory file itself.
831 if ((strchrW(path
, '*') == NULL
) && (strchrW(path
, '%') == NULL
)) {
832 status
= GetFileAttributesW(path
);
833 if ((status
!= INVALID_FILE_ATTRIBUTES
) && (status
& FILE_ATTRIBUTE_DIRECTORY
)) {
834 if (!ends_with_backslash( path
)) strcatW( path
, slashW
);
835 strcatW (path
, starW
);
838 /* Special case wildcard search with no extension (ie parameters ending in '.') as
839 GetFullPathName strips off the additional '.' */
840 if (fullname
[strlenW(fullname
)-1] == '.') strcatW(path
, dotW
);
843 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path
));
844 thisEntry
= heap_xalloc(sizeof(DIRECTORY_STACK
));
845 if (fullParms
== NULL
) fullParms
= thisEntry
;
846 if (prevEntry
!= NULL
) prevEntry
->next
= thisEntry
;
847 prevEntry
= thisEntry
;
848 thisEntry
->next
= NULL
;
850 /* Split into components */
851 WCMD_splitpath(path
, drive
, dir
, fname
, ext
);
852 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
853 wine_dbgstr_w(drive
), wine_dbgstr_w(dir
),
854 wine_dbgstr_w(fname
), wine_dbgstr_w(ext
));
856 thisEntry
->dirName
= heap_xalloc(sizeof(WCHAR
) * (strlenW(drive
)+strlenW(dir
)+1));
857 strcpyW(thisEntry
->dirName
, drive
);
858 strcatW(thisEntry
->dirName
, dir
);
860 thisEntry
->fileName
= heap_xalloc(sizeof(WCHAR
) * (strlenW(fname
)+strlenW(ext
)+1));
861 strcpyW(thisEntry
->fileName
, fname
);
862 strcatW(thisEntry
->fileName
, ext
);
867 /* If just 'dir' entered, a '*' parameter is assumed */
868 if (fullParms
== NULL
) {
869 WINE_TRACE("Inserting default '*'\n");
870 fullParms
= heap_xalloc(sizeof(DIRECTORY_STACK
));
871 fullParms
->next
= NULL
;
872 fullParms
->dirName
= heap_strdupW(cwd
);
873 fullParms
->fileName
= heap_strdupW(starW
);
878 thisEntry
= fullParms
;
881 while (thisEntry
!= NULL
) {
883 /* Output disk free (trailer) and volume information (header) if the drive
885 if (lastDrive
!= toupper(thisEntry
->dirName
[0])) {
887 /* Trailer Information */
888 if (lastDrive
!= '?') {
890 WCMD_dir_trailer(prevEntry
->dirName
[0]);
893 lastDrive
= toupper(thisEntry
->dirName
[0]);
898 WINE_TRACE("Writing volume for '%c:'\n", thisEntry
->dirName
[0]);
899 memcpy(drive
, thisEntry
->dirName
, 2 * sizeof(WCHAR
));
901 status
= WCMD_volume (0, drive
);
909 static const WCHAR newLine2
[] = {'\n','\n','\0'};
910 if (!bare
) WCMD_output_asis (newLine2
);
913 /* Clear any errors from previous invocations, and process it */
915 prevEntry
= thisEntry
;
916 thisEntry
= WCMD_list_directory (thisEntry
, 0);
919 /* Trailer Information */
921 WCMD_dir_trailer(prevEntry
->dirName
[0]);
925 if (paged_mode
) WCMD_leave_paged_mode();
927 /* Free storage allocated for parms */
928 while (fullParms
!= NULL
) {
929 prevEntry
= fullParms
;
930 fullParms
= prevEntry
->next
;
931 heap_free(prevEntry
->dirName
);
932 heap_free(prevEntry
->fileName
);
933 heap_free(prevEntry
);