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);
36 extern WCHAR quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
37 extern DWORD errorlevel;
39 typedef enum _DISPLAYTIME
46 typedef enum _DISPLAYORDER
54 static int file_total, dir_total, recurse, wide, bare, max_width, lower;
55 static int shortname, usernames;
56 static ULONGLONG byte_total;
57 static DISPLAYTIME dirTime;
58 static DISPLAYORDER dirOrder;
59 static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
60 static BOOL separator;
61 static ULONG showattrs, attrsbits;
63 static const WCHAR dotW[] = {'.','\0'};
64 static const WCHAR dotdotW[] = {'.','.','\0'};
65 static const WCHAR starW[] = {'*','\0'};
66 static const WCHAR slashW[] = {'\\','\0'};
67 static const WCHAR emptyW[] = {'\0'};
68 static const WCHAR spaceW[] = {' ','\0'};
70 /*****************************************************************************
73 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
75 static WCHAR * WCMD_strrev (WCHAR *buff) {
81 for (i=0; i<r/2; i++) {
83 buff[i] = buff[r-i-1];
89 /*****************************************************************************
92 * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
93 * Result is returned in a static string overwritten with each call.
94 * FIXME: There must be a better algorithm!
96 static WCHAR * WCMD_filesize64 (ULONGLONG n) {
101 static WCHAR buff[32];
106 if (separator && ((++i)%3 == 1)) *p++ = ',';
117 /*****************************************************************************
120 * Sort based on the /O options supplied on the command line
122 static int WCMD_dir_sort (const void *a, const void *b)
124 const WIN32_FIND_DATAW *filea = (const WIN32_FIND_DATAW *)a;
125 const WIN32_FIND_DATAW *fileb = (const WIN32_FIND_DATAW *)b;
128 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
129 requested sort order for the directory components */
130 if (orderGroupDirs &&
131 ((filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
132 (fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
134 BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
135 if (aDir) result = -1;
137 if (orderGroupDirsReverse) result = -result;
141 } else if (dirOrder == Name) {
142 result = lstrcmpiW(filea->cFileName, fileb->cFileName);
145 } else if (dirOrder == Size) {
146 ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
147 ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
148 if( sizea < sizeb ) result = -1;
149 else if( sizea == sizeb ) result = 0;
152 /* Order by Date: (Takes into account which date (/T option) */
153 } else if (dirOrder == Date) {
156 ULONG64 timea, timeb;
158 if (dirTime == Written) {
159 ft = &filea->ftLastWriteTime;
160 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
161 ft = &fileb->ftLastWriteTime;
162 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
163 } else if (dirTime == Access) {
164 ft = &filea->ftLastAccessTime;
165 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
166 ft = &fileb->ftLastAccessTime;
167 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
169 ft = &filea->ftCreationTime;
170 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
171 ft = &fileb->ftCreationTime;
172 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
174 if( timea < timeb ) result = -1;
175 else if( timea == timeb ) result = 0;
178 /* Order by Extension: (Takes into account which date (/T option) */
179 } else if (dirOrder == Extension) {
182 WCHAR fname[MAX_PATH];
183 WCHAR extA[MAX_PATH];
184 WCHAR extB[MAX_PATH];
186 /* Split into components */
187 WCMD_splitpath(filea->cFileName, drive, dir, fname, extA);
188 WCMD_splitpath(fileb->cFileName, drive, dir, fname, extB);
189 result = lstrcmpiW(extA, extB);
192 if (orderReverse) result = -result;
196 /*****************************************************************************
199 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
201 static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) {
203 ULONG sizeNeeded = 0;
205 WCHAR name[MAXSTRING];
206 WCHAR domain[MAXSTRING];
208 /* In case of error, return empty string */
211 /* Find out how much space we need for the owner security descriptor */
212 GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
215 if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
219 BOOL defaulted = FALSE;
220 ULONG nameLen = MAXSTRING;
221 ULONG domainLen = MAXSTRING;
222 SID_NAME_USE nameuse;
224 secBuffer = HeapAlloc(GetProcessHeap(),0,sizeNeeded * sizeof(BYTE));
225 if(!secBuffer) return;
227 /* Get the owners security descriptor */
228 if(!GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, secBuffer,
229 sizeNeeded, &sizeNeeded)) {
230 HeapFree(GetProcessHeap(),0,secBuffer);
234 /* Get the SID from the SD */
235 if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
236 HeapFree(GetProcessHeap(),0,secBuffer);
240 /* Convert to a username */
241 if (LookupAccountSidW(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
242 static const WCHAR fmt[] = {'%','s','%','c','%','s','\0'};
243 snprintfW(owner, ownerlen, fmt, domain, '\\', name);
245 HeapFree(GetProcessHeap(),0,secBuffer);
250 /*****************************************************************************
251 * WCMD_list_directory
253 * List a single file directory. This function (and those below it) can be called
254 * recursively when the /S switch is used.
256 * FIXME: Assumes 24-line display for the /P qualifier.
259 static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int level) {
261 WCHAR string[1024], datestring[32], timestring[32];
262 WCHAR real_path[MAX_PATH];
263 WIN32_FIND_DATAW *fd;
267 int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
268 int numCols, numRows;
270 ULARGE_INTEGER byte_count, file_size;
271 DIRECTORY_STACK *parms;
272 int concurrentDirs = 0;
273 BOOL done_header = FALSE;
275 static const WCHAR fmtDir[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
276 '<','D','I','R','>',' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
277 static const WCHAR fmtFile[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
278 ' ',' ','%','1','0','s',' ',' ','\0'};
279 static const WCHAR fmt2[] = {'%','-','1','3','s','\0'};
280 static const WCHAR fmt3[] = {'%','-','2','3','s','\0'};
281 static const WCHAR fmt4[] = {'%','s','\0'};
282 static const WCHAR fmt5[] = {'%','s','%','s','\0'};
287 byte_count.QuadPart = 0;
291 /* Loop merging all the files from consecutive parms which relate to the
292 same directory. Note issuing a directory header with no contents
293 mirrors what windows does */
295 fd = HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATAW));
296 while (parms && strcmpW(inputparms->dirName, parms->dirName) == 0) {
299 /* Work out the full path + filename */
300 strcpyW(real_path, parms->dirName);
301 strcatW(real_path, parms->fileName);
303 /* Load all files into an in memory structure */
304 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
305 hff = FindFirstFileW(real_path, (fd+entry_count));
306 if (hff != INVALID_HANDLE_VALUE) {
308 /* Skip any which are filtered out by attribute */
309 if (((fd+entry_count)->dwFileAttributes & attrsbits) != showattrs) continue;
313 /* Keep running track of longest filename for wide output */
314 if (wide || orderByCol) {
315 int tmpLen = strlenW((fd+(entry_count-1))->cFileName) + 3;
316 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
317 if (tmpLen > widest) widest = tmpLen;
320 fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATAW));
323 WINE_ERR("Out of memory\n");
327 } while (FindNextFileW(hff, (fd+entry_count)) != 0);
331 /* Work out the actual current directory name without a trailing \ */
332 strcpyW(real_path, parms->dirName);
333 real_path[strlenW(parms->dirName)-1] = 0x00;
335 /* Output the results */
337 if (level != 0 && (entry_count > 0)) WCMD_output (newline);
338 if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
339 static const WCHAR headerW[] = {'D','i','r','e','c','t','o','r','y',' ','o','f',
340 ' ','%','s','\n','\n','\0'};
341 WCMD_output (headerW, real_path);
346 /* Move to next parm */
350 /* Handle case where everything is filtered out */
351 if (entry_count > 0) {
353 /* Sort the list of files */
354 qsort (fd, entry_count, sizeof(WIN32_FIND_DATAW), WCMD_dir_sort);
356 /* Work out the number of columns */
357 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
358 if (wide || orderByCol) {
359 numCols = max(1, (int)max_width / widest);
360 numRows = entry_count / numCols;
361 if (entry_count % numCols) numRows++;
364 numRows = entry_count;
366 WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
368 for (rows=0; rows<numRows; rows++) {
369 BOOL addNewLine = TRUE;
370 for (cols=0; cols<numCols; cols++) {
373 /* Work out the index of the entry being pointed to */
375 i = (cols * numRows) + rows;
376 if (i >= entry_count) continue;
378 i = (rows * numCols) + cols;
379 if (i >= entry_count) continue;
382 /* /L convers all names to lower case */
384 WCHAR *p = (fd+i)->cFileName;
385 while ( (*p = tolower(*p)) ) ++p;
388 /* /Q gets file ownership information */
390 strcpyW (string, inputparms->dirName);
391 strcatW (string, (fd+i)->cFileName);
392 WCMD_getfileowner(string, username, sizeof(username)/sizeof(WCHAR));
395 if (dirTime == Written) {
396 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
397 } else if (dirTime == Access) {
398 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
400 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
402 FileTimeToSystemTime (&ft, &st);
403 GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring,
404 sizeof(datestring)/sizeof(WCHAR));
405 GetTimeFormatW(0, TIME_NOSECONDS, &st,
406 NULL, timestring, sizeof(timestring)/sizeof(WCHAR));
410 tmp_width = cur_width;
411 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
412 static const WCHAR fmt[] = {'[','%','s',']','\0'};
413 WCMD_output (fmt, (fd+i)->cFileName);
415 tmp_width = tmp_width + strlenW((fd+i)->cFileName) + 2;
417 static const WCHAR fmt[] = {'%','s','\0'};
418 WCMD_output (fmt, (fd+i)->cFileName);
419 tmp_width = tmp_width + strlenW((fd+i)->cFileName) ;
421 file_size.u.LowPart = (fd+i)->nFileSizeLow;
422 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
423 byte_count.QuadPart += file_size.QuadPart;
425 cur_width = cur_width + widest;
427 if ((cur_width + widest) > max_width) {
430 int padding = cur_width - tmp_width;
434 /* Note: WCMD_output uses wvsprintf which does not allow %*
435 so manually pad with spaces to appropriate width */
436 strcpyW(temp, emptyW);
437 while (padding > 0) {
438 strcatW(&temp[toWrite], spaceW);
443 strcpyW(temp, emptyW);
450 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
454 WCMD_output (fmtDir, datestring, timestring);
455 if (shortname) WCMD_output (fmt2, (fd+i)->cAlternateFileName);
456 if (usernames) WCMD_output (fmt3, username);
457 WCMD_output(fmt4,(fd+i)->cFileName);
459 if (!((strcmpW((fd+i)->cFileName, dotW) == 0) ||
460 (strcmpW((fd+i)->cFileName, dotdotW) == 0))) {
461 WCMD_output (fmt5, recurse?inputparms->dirName:emptyW, (fd+i)->cFileName);
469 file_size.u.LowPart = (fd+i)->nFileSizeLow;
470 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
471 byte_count.QuadPart += file_size.QuadPart;
473 WCMD_output (fmtFile, datestring, timestring,
474 WCMD_filesize64(file_size.QuadPart));
475 if (shortname) WCMD_output (fmt2, (fd+i)->cAlternateFileName);
476 if (usernames) WCMD_output (fmt3, username);
477 WCMD_output(fmt4,(fd+i)->cFileName);
479 WCMD_output (fmt5, recurse?inputparms->dirName:emptyW, (fd+i)->cFileName);
483 if (addNewLine) WCMD_output (newline);
488 if (file_count == 1) {
489 static const WCHAR fmt[] = {' ',' ',' ',' ',' ',' ',' ','1',' ','f','i','l','e',' ',
490 '%','2','5','s',' ','b','y','t','e','s','\n','\0'};
491 WCMD_output (fmt, WCMD_filesize64 (byte_count.QuadPart));
494 static const WCHAR fmt[] = {'%','8','d',' ','f','i','l','e','s',' ','%','2','4','s',
495 ' ','b','y','t','e','s','\n','\0'};
496 WCMD_output (fmt, file_count, WCMD_filesize64 (byte_count.QuadPart));
499 byte_total = byte_total + byte_count.QuadPart;
500 file_total = file_total + file_count;
501 dir_total = dir_total + dir_count;
503 if (!bare && !recurse) {
504 if (dir_count == 1) {
505 static const WCHAR fmt[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','y',
506 ' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
507 WCMD_output (fmt, 1);
509 static const WCHAR fmt[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i',
511 WCMD_output (fmt, dir_count);
515 HeapFree(GetProcessHeap(),0,fd);
517 /* When recursing, look in all subdirectories for matches */
519 DIRECTORY_STACK *dirStack = NULL;
520 DIRECTORY_STACK *lastEntry = NULL;
521 WIN32_FIND_DATAW finddata;
523 /* Build path to search */
524 strcpyW(string, inputparms->dirName);
525 strcatW(string, starW);
527 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
528 hff = FindFirstFileW(string, &finddata);
529 if (hff != INVALID_HANDLE_VALUE) {
531 if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
532 (strcmpW(finddata.cFileName, dotdotW) != 0) &&
533 (strcmpW(finddata.cFileName, dotW) != 0)) {
535 DIRECTORY_STACK *thisDir;
536 int dirsToCopy = concurrentDirs;
538 /* Loop creating list of subdirs for all concurrent entries */
540 while (dirsToCopy > 0) {
543 /* Work out search parameter in sub dir */
544 strcpyW (string, inputparms->dirName);
545 strcatW (string, finddata.cFileName);
546 strcatW (string, slashW);
547 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
549 /* Allocate memory, add to list */
550 thisDir = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
551 if (dirStack == NULL) dirStack = thisDir;
552 if (lastEntry != NULL) lastEntry->next = thisDir;
554 thisDir->next = NULL;
555 thisDir->dirName = HeapAlloc(GetProcessHeap(),0,
556 sizeof(WCHAR) * (strlenW(string)+1));
557 strcpyW(thisDir->dirName, string);
558 thisDir->fileName = HeapAlloc(GetProcessHeap(),0,
559 sizeof(WCHAR) * (strlenW(parms->fileName)+1));
560 strcpyW(thisDir->fileName, parms->fileName);
564 } while (FindNextFileW(hff, &finddata) != 0);
567 while (dirStack != NULL) {
568 DIRECTORY_STACK *thisDir = dirStack;
569 dirStack = WCMD_list_directory (thisDir, 1);
570 while (thisDir != dirStack) {
571 DIRECTORY_STACK *tempDir = thisDir->next;
572 HeapFree(GetProcessHeap(),0,thisDir->dirName);
573 HeapFree(GetProcessHeap(),0,thisDir->fileName);
574 HeapFree(GetProcessHeap(),0,thisDir);
581 /* Handle case where everything is filtered out */
582 if ((file_total + dir_total == 0) && (level == 0)) {
583 SetLastError (ERROR_FILE_NOT_FOUND);
591 /*****************************************************************************
594 * Print out the trailer for the supplied drive letter
596 static void WCMD_dir_trailer(WCHAR drive) {
597 ULARGE_INTEGER avail, total, freebytes;
599 WCHAR driveName[4] = {'c',':','\\','\0'};
601 driveName[0] = drive;
602 status = GetDiskFreeSpaceExW(driveName, &avail, &total, &freebytes);
603 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName),
604 status, GetLastError());
606 if (errorlevel==0 && !bare) {
608 static const WCHAR fmt1[] = {'\n',' ',' ',' ',' ',' ','T','o','t','a','l',' ','f','i','l','e','s',
609 ' ','l','i','s','t','e','d',':','\n','%','8','d',' ','f','i','l','e',
610 's','%','2','5','s',' ','b','y','t','e','s','\n','\0'};
611 static const WCHAR fmt2[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i','e','s',' ','%',
612 '1','8','s',' ','b','y','t','e','s',' ','f','r','e','e','\n','\n',
614 WCMD_output (fmt1, file_total, WCMD_filesize64 (byte_total));
615 WCMD_output (fmt2, dir_total, WCMD_filesize64 (freebytes.QuadPart));
617 static const WCHAR fmt[] = {' ','%','1','8','s',' ','b','y','t','e','s',' ','f','r','e','e',
619 WCMD_output (fmt, WCMD_filesize64 (freebytes.QuadPart));
624 /*****************************************************************************
627 * List a file directory.
631 void WCMD_directory (WCHAR *cmd) {
633 WCHAR path[MAX_PATH], cwd[MAX_PATH];
634 int status, paged_mode;
635 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
637 WCHAR string[MAXSTRING];
641 BOOL trailerReqd = FALSE;
642 DIRECTORY_STACK *fullParms = NULL;
643 DIRECTORY_STACK *prevEntry = NULL;
644 DIRECTORY_STACK *thisEntry = NULL;
647 WCHAR fname[MAX_PATH];
649 static const WCHAR dircmdW[] = {'D','I','R','C','M','D','\0'};
653 /* Prefill quals with (uppercased) DIRCMD env var */
654 if (GetEnvironmentVariableW(dircmdW, string, sizeof(string)/sizeof(WCHAR))) {
656 while ( (*p = toupper(*p)) ) ++p;
657 strcatW(string,quals);
658 strcpyW(quals, string);
662 file_total = dir_total = 0;
664 /* Initialize all flags to their defaults as if no DIRCMD or quals */
676 orderReverse = FALSE;
677 orderGroupDirs = FALSE;
678 orderGroupDirsReverse = FALSE;
680 attrsbits = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
682 /* Handle args - Loop through so right most is the effective one */
683 /* Note: /- appears to be a negate rather than an off, eg. dir
684 /-W is wide, or dir /w /-w /-w is also wide */
686 while (*p && (*p=='/' || *p==' ')) {
688 if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
695 WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
697 case 'P': if (negate) paged_mode = !paged_mode;
698 else paged_mode = TRUE;
700 case 'S': if (negate) recurse = !recurse;
703 case 'W': if (negate) wide = !wide;
706 case 'B': if (negate) bare = !bare;
709 case 'L': if (negate) lower = !lower;
712 case 'X': if (negate) shortname = !shortname;
713 else shortname = TRUE;
715 case 'Q': if (negate) usernames = !usernames;
716 else usernames = TRUE;
718 case 'D': if (negate) orderByCol = !orderByCol;
719 else orderByCol = TRUE;
721 case 'C': if (negate) separator = !separator;
722 else separator = TRUE;
725 if (*p==':') p++; /* Skip optional : */
727 if (*p == 'A') dirTime = Access;
728 else if (*p == 'C') dirTime = Creation;
729 else if (*p == 'W') dirTime = Written;
731 /* Support /T and /T: with no parms, default to written */
732 else if (*p == 0x00 || *p == '/') {
734 p = p - 1; /* So when step on, move to '/' */
736 SetLastError(ERROR_INVALID_PARAMETER);
743 if (*p==':') p++; /* Skip optional : */
744 while (*p && *p != '/') {
745 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
747 case 'N': dirOrder = Name; break;
748 case 'E': dirOrder = Extension; break;
749 case 'S': dirOrder = Size; break;
750 case 'D': dirOrder = Date; break;
751 case '-': if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
752 else orderReverse = TRUE;
754 case 'G': orderGroupDirs = TRUE; break;
756 SetLastError(ERROR_INVALID_PARAMETER);
763 p = p - 1; /* So when step on, move to '/' */
768 if (*p==':') p++; /* Skip optional : */
769 while (*p && *p != '/') {
770 BOOL anegate = FALSE;
773 /* Note /A: - options are 'offs' not toggles */
779 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
781 case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
782 case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
783 case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
784 case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
785 case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
787 SetLastError(ERROR_INVALID_PARAMETER);
793 /* Keep running list of bits we care about */
796 /* Mask shows what MUST be in the bits we care about */
797 if (anegate) showattrs = showattrs & ~mask;
798 else showattrs |= mask;
802 p = p - 1; /* So when step on, move to '/' */
803 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs, attrsbits);
806 SetLastError(ERROR_INVALID_PARAMETER);
814 /* Handle conflicting args and initialization */
815 if (bare || shortname) wide = FALSE;
816 if (bare) shortname = FALSE;
817 if (wide) usernames = FALSE;
818 if (orderByCol) wide = TRUE;
821 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
822 max_width = consoleInfo.dwSize.X;
827 WCMD_enter_paged_mode(NULL);
832 GetCurrentDirectoryW(MAX_PATH, cwd);
833 strcatW(cwd, slashW);
835 /* Loop through all args, calculating full effective directory */
839 WCHAR fullname[MAXSTRING];
840 WCHAR *thisArg = WCMD_parameter(cmd, argno++, &argN, NULL);
841 if (argN && argN[0] != '/') {
843 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
844 if (thisArg[1] == ':' && thisArg[2] == '\\') {
845 strcpyW(fullname, thisArg);
846 } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
848 static const WCHAR envFmt[] = {'=','%','c',':','\0'};
849 wsprintfW(envvar, envFmt, thisArg[0]);
850 if (!GetEnvironmentVariableW(envvar, fullname, MAX_PATH)) {
851 static const WCHAR noEnvFmt[] = {'%','c',':','\0'};
852 wsprintfW(fullname, noEnvFmt, thisArg[0]);
854 strcatW(fullname, slashW);
855 strcatW(fullname, &thisArg[2]);
856 } else if (thisArg[0] == '\\') {
857 memcpy(fullname, cwd, 2 * sizeof(WCHAR));
858 strcpyW(fullname+2, thisArg);
860 strcpyW(fullname, cwd);
861 strcatW(fullname, thisArg);
863 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
865 status = GetFullPathNameW(fullname, sizeof(path)/sizeof(WCHAR), path, NULL);
868 * If the path supplied does not include a wildcard, and the endpoint of the
869 * path references a directory, we need to list the *contents* of that
870 * directory not the directory file itself.
872 if ((strchrW(path, '*') == NULL) && (strchrW(path, '%') == NULL)) {
873 status = GetFileAttributesW(path);
874 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
875 if (path[strlenW(path)-1] == '\\') {
876 strcatW (path, starW);
879 static const WCHAR slashStarW[] = {'\\','*','\0'};
880 strcatW (path, slashStarW);
884 /* Special case wildcard search with no extension (ie parameters ending in '.') as
885 GetFullPathName strips off the additional '.' */
886 if (fullname[strlenW(fullname)-1] == '.') strcatW(path, dotW);
889 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
890 thisEntry = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
891 if (fullParms == NULL) fullParms = thisEntry;
892 if (prevEntry != NULL) prevEntry->next = thisEntry;
893 prevEntry = thisEntry;
894 thisEntry->next = NULL;
896 /* Split into components */
897 WCMD_splitpath(path, drive, dir, fname, ext);
898 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
899 wine_dbgstr_w(drive), wine_dbgstr_w(dir),
900 wine_dbgstr_w(fname), wine_dbgstr_w(ext));
902 thisEntry->dirName = HeapAlloc(GetProcessHeap(),0,
903 sizeof(WCHAR) * (strlenW(drive)+strlenW(dir)+1));
904 strcpyW(thisEntry->dirName, drive);
905 strcatW(thisEntry->dirName, dir);
907 thisEntry->fileName = HeapAlloc(GetProcessHeap(),0,
908 sizeof(WCHAR) * (strlenW(fname)+strlenW(ext)+1));
909 strcpyW(thisEntry->fileName, fname);
910 strcatW(thisEntry->fileName, ext);
915 /* If just 'dir' entered, a '*' parameter is assumed */
916 if (fullParms == NULL) {
917 WINE_TRACE("Inserting default '*'\n");
918 fullParms = HeapAlloc(GetProcessHeap(),0, sizeof(DIRECTORY_STACK));
919 fullParms->next = NULL;
920 fullParms->dirName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * (strlenW(cwd)+1));
921 strcpyW(fullParms->dirName, cwd);
922 fullParms->fileName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * 2);
923 strcpyW(fullParms->fileName, starW);
928 thisEntry = fullParms;
931 while (thisEntry != NULL) {
933 /* Output disk free (trailer) and volume information (header) if the drive
935 if (lastDrive != toupper(thisEntry->dirName[0])) {
937 /* Trailer Information */
938 if (lastDrive != '?') {
940 WCMD_dir_trailer(prevEntry->dirName[0]);
943 lastDrive = toupper(thisEntry->dirName[0]);
948 WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
949 memcpy(drive, thisEntry->dirName, 2 * sizeof(WCHAR));
951 status = WCMD_volume (0, drive);
959 static const WCHAR newLine2[] = {'\n','\n','\0'};
960 if (!bare) WCMD_output (newLine2);
963 /* Clear any errors from previous invocations, and process it */
965 prevEntry = thisEntry;
966 thisEntry = WCMD_list_directory (thisEntry, 0);
969 /* Trailer Information */
971 WCMD_dir_trailer(prevEntry->dirName[0]);
975 if (paged_mode) WCMD_leave_paged_mode();
977 /* Free storage allocated for parms */
978 while (fullParms != NULL) {
979 prevEntry = fullParms;
980 fullParms = prevEntry->next;
981 HeapFree(GetProcessHeap(),0,prevEntry->dirName);
982 HeapFree(GetProcessHeap(),0,prevEntry->fileName);
983 HeapFree(GetProcessHeap(),0,prevEntry);