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, max_width;
55 static ULONGLONG byte_total;
56 static DISPLAYTIME dirTime;
57 static DISPLAYORDER dirOrder;
58 static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
59 static BOOL paged_mode, recurse, wide, bare, lower, shortname, usernames, separator;
60 static ULONG showattrs, attrsbits;
62 /*****************************************************************************
65 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
67 static WCHAR * WCMD_strrev (WCHAR *buff) {
73 for (i=0; i<r/2; i++) {
75 buff[i] = buff[r-i-1];
81 /*****************************************************************************
84 * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
85 * Result is returned in a static string overwritten with each call.
86 * FIXME: There must be a better algorithm!
88 static WCHAR * WCMD_filesize64 (ULONGLONG n) {
93 static WCHAR buff[32];
98 if (separator && ((++i)%3 == 1)) *p++ = ',';
109 /*****************************************************************************
112 * Sort based on the /O options supplied on the command line
114 static int WCMD_dir_sort (const void *a, const void *b)
116 const WIN32_FIND_DATAW *filea = (const WIN32_FIND_DATAW *)a;
117 const WIN32_FIND_DATAW *fileb = (const WIN32_FIND_DATAW *)b;
120 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
121 requested sort order for the directory components */
122 if (orderGroupDirs &&
123 ((filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
124 (fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
126 BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
127 if (aDir) result = -1;
129 if (orderGroupDirsReverse) result = -result;
133 } else if (dirOrder == Name) {
134 result = lstrcmpiW(filea->cFileName, fileb->cFileName);
137 } else if (dirOrder == Size) {
138 ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
139 ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
140 if( sizea < sizeb ) result = -1;
141 else if( sizea == sizeb ) result = 0;
144 /* Order by Date: (Takes into account which date (/T option) */
145 } else if (dirOrder == Date) {
148 ULONG64 timea, timeb;
150 if (dirTime == Written) {
151 ft = &filea->ftLastWriteTime;
152 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
153 ft = &fileb->ftLastWriteTime;
154 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
155 } else if (dirTime == Access) {
156 ft = &filea->ftLastAccessTime;
157 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
158 ft = &fileb->ftLastAccessTime;
159 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
161 ft = &filea->ftCreationTime;
162 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
163 ft = &fileb->ftCreationTime;
164 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
166 if( timea < timeb ) result = -1;
167 else if( timea == timeb ) result = 0;
170 /* Order by Extension: (Takes into account which date (/T option) */
171 } else if (dirOrder == Extension) {
174 WCHAR fname[MAX_PATH];
175 WCHAR extA[MAX_PATH];
176 WCHAR extB[MAX_PATH];
178 /* Split into components */
179 WCMD_splitpath(filea->cFileName, drive, dir, fname, extA);
180 WCMD_splitpath(fileb->cFileName, drive, dir, fname, extB);
181 result = lstrcmpiW(extA, extB);
184 if (orderReverse) result = -result;
188 /*****************************************************************************
191 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
193 static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) {
195 ULONG sizeNeeded = 0;
197 WCHAR name[MAXSTRING];
198 WCHAR domain[MAXSTRING];
200 /* In case of error, return empty string */
203 /* Find out how much space we need for the owner security descriptor */
204 GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
207 if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
211 BOOL defaulted = FALSE;
212 ULONG nameLen = MAXSTRING;
213 ULONG domainLen = MAXSTRING;
214 SID_NAME_USE nameuse;
216 secBuffer = HeapAlloc(GetProcessHeap(),0,sizeNeeded * sizeof(BYTE));
217 if(!secBuffer) return;
219 /* Get the owners security descriptor */
220 if(!GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, secBuffer,
221 sizeNeeded, &sizeNeeded)) {
222 HeapFree(GetProcessHeap(),0,secBuffer);
226 /* Get the SID from the SD */
227 if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
228 HeapFree(GetProcessHeap(),0,secBuffer);
232 /* Convert to a username */
233 if (LookupAccountSidW(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
234 static const WCHAR fmt[] = {'%','s','%','c','%','s','\0'};
235 snprintfW(owner, ownerlen, fmt, domain, '\\', name);
237 HeapFree(GetProcessHeap(),0,secBuffer);
242 /*****************************************************************************
243 * WCMD_list_directory
245 * List a single file directory. This function (and those below it) can be called
246 * recursively when the /S switch is used.
248 * FIXME: Assumes 24-line display for the /P qualifier.
251 static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int level) {
253 WCHAR string[1024], datestring[32], timestring[32];
254 WCHAR real_path[MAX_PATH];
255 WIN32_FIND_DATAW *fd;
259 int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
260 int numCols, numRows;
262 ULARGE_INTEGER byte_count, file_size;
263 DIRECTORY_STACK *parms;
264 int concurrentDirs = 0;
265 BOOL done_header = FALSE;
267 static const WCHAR fmtDir[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
268 '<','D','I','R','>',' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
269 static const WCHAR fmtFile[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
270 ' ',' ','%','1','0','s',' ',' ','\0'};
271 static const WCHAR fmt2[] = {'%','-','1','3','s','\0'};
272 static const WCHAR fmt3[] = {'%','-','2','3','s','\0'};
273 static const WCHAR fmt4[] = {'%','s','\0'};
274 static const WCHAR fmt5[] = {'%','s','%','s','\0'};
279 byte_count.QuadPart = 0;
283 /* Loop merging all the files from consecutive parms which relate to the
284 same directory. Note issuing a directory header with no contents
285 mirrors what windows does */
287 fd = HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATAW));
288 while (parms && strcmpW(inputparms->dirName, parms->dirName) == 0) {
291 /* Work out the full path + filename */
292 strcpyW(real_path, parms->dirName);
293 strcatW(real_path, parms->fileName);
295 /* Load all files into an in memory structure */
296 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
297 hff = FindFirstFileW(real_path, (fd+entry_count));
298 if (hff != INVALID_HANDLE_VALUE) {
300 /* Skip any which are filtered out by attribute */
301 if (((fd+entry_count)->dwFileAttributes & attrsbits) != showattrs) continue;
305 /* Keep running track of longest filename for wide output */
306 if (wide || orderByCol) {
307 int tmpLen = strlenW((fd+(entry_count-1))->cFileName) + 3;
308 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
309 if (tmpLen > widest) widest = tmpLen;
312 fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATAW));
315 WINE_ERR("Out of memory\n");
319 } while (FindNextFileW(hff, (fd+entry_count)) != 0);
323 /* Work out the actual current directory name without a trailing \ */
324 strcpyW(real_path, parms->dirName);
325 real_path[strlenW(parms->dirName)-1] = 0x00;
327 /* Output the results */
329 if (level != 0 && (entry_count > 0)) WCMD_output_asis (newline);
330 if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
331 static const WCHAR headerW[] = {'D','i','r','e','c','t','o','r','y',' ','o','f',
332 ' ','%','s','\n','\n','\0'};
333 WCMD_output (headerW, real_path);
338 /* Move to next parm */
342 /* Handle case where everything is filtered out */
343 if (entry_count > 0) {
345 /* Sort the list of files */
346 qsort (fd, entry_count, sizeof(WIN32_FIND_DATAW), WCMD_dir_sort);
348 /* Work out the number of columns */
349 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
350 if (wide || orderByCol) {
351 numCols = max(1, (int)max_width / widest);
352 numRows = entry_count / numCols;
353 if (entry_count % numCols) numRows++;
356 numRows = entry_count;
358 WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
360 for (rows=0; rows<numRows; rows++) {
361 BOOL addNewLine = TRUE;
362 for (cols=0; cols<numCols; cols++) {
365 /* Work out the index of the entry being pointed to */
367 i = (cols * numRows) + rows;
368 if (i >= entry_count) continue;
370 i = (rows * numCols) + cols;
371 if (i >= entry_count) continue;
374 /* /L convers all names to lower case */
376 WCHAR *p = (fd+i)->cFileName;
377 while ( (*p = tolower(*p)) ) ++p;
380 /* /Q gets file ownership information */
382 strcpyW (string, inputparms->dirName);
383 strcatW (string, (fd+i)->cFileName);
384 WCMD_getfileowner(string, username, sizeof(username)/sizeof(WCHAR));
387 if (dirTime == Written) {
388 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
389 } else if (dirTime == Access) {
390 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
392 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
394 FileTimeToSystemTime (&ft, &st);
395 GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring,
396 sizeof(datestring)/sizeof(WCHAR));
397 GetTimeFormatW(0, TIME_NOSECONDS, &st,
398 NULL, timestring, sizeof(timestring)/sizeof(WCHAR));
402 tmp_width = cur_width;
403 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
404 static const WCHAR fmt[] = {'[','%','s',']','\0'};
405 WCMD_output (fmt, (fd+i)->cFileName);
407 tmp_width = tmp_width + strlenW((fd+i)->cFileName) + 2;
409 static const WCHAR fmt[] = {'%','s','\0'};
410 WCMD_output (fmt, (fd+i)->cFileName);
411 tmp_width = tmp_width + strlenW((fd+i)->cFileName) ;
413 file_size.u.LowPart = (fd+i)->nFileSizeLow;
414 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
415 byte_count.QuadPart += file_size.QuadPart;
417 cur_width = cur_width + widest;
419 if ((cur_width + widest) > max_width) {
422 int padding = cur_width - tmp_width;
426 /* Note: WCMD_output uses wvsprintf which does not allow %*
427 so manually pad with spaces to appropriate width */
428 strcpyW(temp, nullW);
429 while (padding > 0) {
430 strcatW(&temp[toWrite], space);
433 WCMD_output_asis(temp);
435 strcpyW(temp, nullW);
439 WCMD_output_asis(temp);
442 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
446 WCMD_output (fmtDir, datestring, timestring);
447 if (shortname) WCMD_output (fmt2, (fd+i)->cAlternateFileName);
448 if (usernames) WCMD_output (fmt3, username);
449 WCMD_output(fmt4,(fd+i)->cFileName);
451 if (!((strcmpW((fd+i)->cFileName, dotW) == 0) ||
452 (strcmpW((fd+i)->cFileName, dotdotW) == 0))) {
453 WCMD_output (fmt5, recurse?inputparms->dirName:nullW, (fd+i)->cFileName);
461 file_size.u.LowPart = (fd+i)->nFileSizeLow;
462 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
463 byte_count.QuadPart += file_size.QuadPart;
465 WCMD_output (fmtFile, datestring, timestring,
466 WCMD_filesize64(file_size.QuadPart));
467 if (shortname) WCMD_output (fmt2, (fd+i)->cAlternateFileName);
468 if (usernames) WCMD_output (fmt3, username);
469 WCMD_output(fmt4,(fd+i)->cFileName);
471 WCMD_output (fmt5, recurse?inputparms->dirName:nullW, (fd+i)->cFileName);
475 if (addNewLine) WCMD_output_asis (newline);
480 if (file_count == 1) {
481 static const WCHAR fmt[] = {' ',' ',' ',' ',' ',' ',' ','1',' ','f','i','l','e',' ',
482 '%','2','5','s',' ','b','y','t','e','s','\n','\0'};
483 WCMD_output (fmt, WCMD_filesize64 (byte_count.QuadPart));
486 static const WCHAR fmt[] = {'%','8','d',' ','f','i','l','e','s',' ','%','2','4','s',
487 ' ','b','y','t','e','s','\n','\0'};
488 WCMD_output (fmt, file_count, WCMD_filesize64 (byte_count.QuadPart));
491 byte_total = byte_total + byte_count.QuadPart;
492 file_total = file_total + file_count;
493 dir_total = dir_total + dir_count;
495 if (!bare && !recurse) {
496 if (dir_count == 1) {
497 static const WCHAR fmt[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','y',
498 ' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
499 WCMD_output (fmt, 1);
501 static const WCHAR fmt[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i',
503 WCMD_output (fmt, dir_count);
507 HeapFree(GetProcessHeap(),0,fd);
509 /* When recursing, look in all subdirectories for matches */
511 DIRECTORY_STACK *dirStack = NULL;
512 DIRECTORY_STACK *lastEntry = NULL;
513 WIN32_FIND_DATAW finddata;
515 /* Build path to search */
516 strcpyW(string, inputparms->dirName);
517 strcatW(string, starW);
519 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
520 hff = FindFirstFileW(string, &finddata);
521 if (hff != INVALID_HANDLE_VALUE) {
523 if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
524 (strcmpW(finddata.cFileName, dotdotW) != 0) &&
525 (strcmpW(finddata.cFileName, dotW) != 0)) {
527 DIRECTORY_STACK *thisDir;
528 int dirsToCopy = concurrentDirs;
530 /* Loop creating list of subdirs for all concurrent entries */
532 while (dirsToCopy > 0) {
535 /* Work out search parameter in sub dir */
536 strcpyW (string, inputparms->dirName);
537 strcatW (string, finddata.cFileName);
538 strcatW (string, slashW);
539 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
541 /* Allocate memory, add to list */
542 thisDir = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
543 if (dirStack == NULL) dirStack = thisDir;
544 if (lastEntry != NULL) lastEntry->next = thisDir;
546 thisDir->next = NULL;
547 thisDir->dirName = HeapAlloc(GetProcessHeap(),0,
548 sizeof(WCHAR) * (strlenW(string)+1));
549 strcpyW(thisDir->dirName, string);
550 thisDir->fileName = HeapAlloc(GetProcessHeap(),0,
551 sizeof(WCHAR) * (strlenW(parms->fileName)+1));
552 strcpyW(thisDir->fileName, parms->fileName);
556 } while (FindNextFileW(hff, &finddata) != 0);
559 while (dirStack != NULL) {
560 DIRECTORY_STACK *thisDir = dirStack;
561 dirStack = WCMD_list_directory (thisDir, 1);
562 while (thisDir != dirStack) {
563 DIRECTORY_STACK *tempDir = thisDir->next;
564 HeapFree(GetProcessHeap(),0,thisDir->dirName);
565 HeapFree(GetProcessHeap(),0,thisDir->fileName);
566 HeapFree(GetProcessHeap(),0,thisDir);
573 /* Handle case where everything is filtered out */
574 if ((file_total + dir_total == 0) && (level == 0)) {
575 SetLastError (ERROR_FILE_NOT_FOUND);
583 /*****************************************************************************
586 * Print out the trailer for the supplied drive letter
588 static void WCMD_dir_trailer(WCHAR drive) {
589 ULARGE_INTEGER avail, total, freebytes;
591 WCHAR driveName[4] = {'c',':','\\','\0'};
593 driveName[0] = drive;
594 status = GetDiskFreeSpaceExW(driveName, &avail, &total, &freebytes);
595 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName),
596 status, GetLastError());
598 if (errorlevel==0 && !bare) {
600 static const WCHAR fmt1[] = {'\n',' ',' ',' ',' ',' ','T','o','t','a','l',' ','f','i','l','e','s',
601 ' ','l','i','s','t','e','d',':','\n','%','8','d',' ','f','i','l','e',
602 's','%','2','5','s',' ','b','y','t','e','s','\n','\0'};
603 static const WCHAR fmt2[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i','e','s',' ','%',
604 '1','8','s',' ','b','y','t','e','s',' ','f','r','e','e','\n','\n',
606 WCMD_output (fmt1, file_total, WCMD_filesize64 (byte_total));
607 WCMD_output (fmt2, dir_total, WCMD_filesize64 (freebytes.QuadPart));
609 static const WCHAR fmt[] = {' ','%','1','8','s',' ','b','y','t','e','s',' ','f','r','e','e',
611 WCMD_output (fmt, WCMD_filesize64 (freebytes.QuadPart));
616 /*****************************************************************************
619 * List a file directory.
623 void WCMD_directory (WCHAR *cmd)
625 WCHAR path[MAX_PATH], cwd[MAX_PATH];
627 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
629 WCHAR string[MAXSTRING];
633 BOOL trailerReqd = FALSE;
634 DIRECTORY_STACK *fullParms = NULL;
635 DIRECTORY_STACK *prevEntry = NULL;
636 DIRECTORY_STACK *thisEntry = NULL;
639 WCHAR fname[MAX_PATH];
641 static const WCHAR dircmdW[] = {'D','I','R','C','M','D','\0'};
645 /* Prefill quals with (uppercased) DIRCMD env var */
646 if (GetEnvironmentVariableW(dircmdW, string, sizeof(string)/sizeof(WCHAR))) {
648 while ( (*p = toupper(*p)) ) ++p;
649 strcatW(string,quals);
650 strcpyW(quals, string);
654 file_total = dir_total = 0;
656 /* Initialize all flags to their defaults as if no DIRCMD or quals */
668 orderReverse = FALSE;
669 orderGroupDirs = FALSE;
670 orderGroupDirsReverse = FALSE;
672 attrsbits = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
674 /* Handle args - Loop through so right most is the effective one */
675 /* Note: /- appears to be a negate rather than an off, eg. dir
676 /-W is wide, or dir /w /-w /-w is also wide */
678 while (*p && (*p=='/' || *p==' ')) {
680 if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
687 WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
689 case 'P': if (negate) paged_mode = !paged_mode;
690 else paged_mode = TRUE;
692 case 'S': if (negate) recurse = !recurse;
695 case 'W': if (negate) wide = !wide;
698 case 'B': if (negate) bare = !bare;
701 case 'L': if (negate) lower = !lower;
704 case 'X': if (negate) shortname = !shortname;
705 else shortname = TRUE;
707 case 'Q': if (negate) usernames = !usernames;
708 else usernames = TRUE;
710 case 'D': if (negate) orderByCol = !orderByCol;
711 else orderByCol = TRUE;
713 case 'C': if (negate) separator = !separator;
714 else separator = TRUE;
717 if (*p==':') p++; /* Skip optional : */
719 if (*p == 'A') dirTime = Access;
720 else if (*p == 'C') dirTime = Creation;
721 else if (*p == 'W') dirTime = Written;
723 /* Support /T and /T: with no parms, default to written */
724 else if (*p == 0x00 || *p == '/') {
726 p = p - 1; /* So when step on, move to '/' */
728 SetLastError(ERROR_INVALID_PARAMETER);
735 if (*p==':') p++; /* Skip optional : */
736 while (*p && *p != '/') {
737 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
739 case 'N': dirOrder = Name; break;
740 case 'E': dirOrder = Extension; break;
741 case 'S': dirOrder = Size; break;
742 case 'D': dirOrder = Date; break;
743 case '-': if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
744 else orderReverse = TRUE;
746 case 'G': orderGroupDirs = TRUE; break;
748 SetLastError(ERROR_INVALID_PARAMETER);
755 p = p - 1; /* So when step on, move to '/' */
760 if (*p==':') p++; /* Skip optional : */
761 while (*p && *p != '/') {
762 BOOL anegate = FALSE;
765 /* Note /A: - options are 'offs' not toggles */
771 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
773 case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
774 case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
775 case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
776 case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
777 case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
779 SetLastError(ERROR_INVALID_PARAMETER);
785 /* Keep running list of bits we care about */
788 /* Mask shows what MUST be in the bits we care about */
789 if (anegate) showattrs = showattrs & ~mask;
790 else showattrs |= mask;
794 p = p - 1; /* So when step on, move to '/' */
795 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs, attrsbits);
798 SetLastError(ERROR_INVALID_PARAMETER);
806 /* Handle conflicting args and initialization */
807 if (bare || shortname) wide = FALSE;
808 if (bare) shortname = FALSE;
809 if (wide) usernames = FALSE;
810 if (orderByCol) wide = TRUE;
813 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
814 max_width = consoleInfo.dwSize.X;
819 WCMD_enter_paged_mode(NULL);
824 GetCurrentDirectoryW(MAX_PATH, cwd);
825 strcatW(cwd, slashW);
827 /* Loop through all args, calculating full effective directory */
831 WCHAR fullname[MAXSTRING];
832 WCHAR *thisArg = WCMD_parameter(cmd, argno++, &argN, NULL);
833 if (argN && argN[0] != '/') {
835 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
836 if (thisArg[1] == ':' && thisArg[2] == '\\') {
837 strcpyW(fullname, thisArg);
838 } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
840 static const WCHAR envFmt[] = {'=','%','c',':','\0'};
841 wsprintfW(envvar, envFmt, thisArg[0]);
842 if (!GetEnvironmentVariableW(envvar, fullname, MAX_PATH)) {
843 static const WCHAR noEnvFmt[] = {'%','c',':','\0'};
844 wsprintfW(fullname, noEnvFmt, thisArg[0]);
846 strcatW(fullname, slashW);
847 strcatW(fullname, &thisArg[2]);
848 } else if (thisArg[0] == '\\') {
849 memcpy(fullname, cwd, 2 * sizeof(WCHAR));
850 strcpyW(fullname+2, thisArg);
852 strcpyW(fullname, cwd);
853 strcatW(fullname, thisArg);
855 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
857 status = GetFullPathNameW(fullname, sizeof(path)/sizeof(WCHAR), path, NULL);
860 * If the path supplied does not include a wildcard, and the endpoint of the
861 * path references a directory, we need to list the *contents* of that
862 * directory not the directory file itself.
864 if ((strchrW(path, '*') == NULL) && (strchrW(path, '%') == NULL)) {
865 status = GetFileAttributesW(path);
866 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
867 if (path[strlenW(path)-1] == '\\') {
868 strcatW (path, starW);
871 static const WCHAR slashStarW[] = {'\\','*','\0'};
872 strcatW (path, slashStarW);
876 /* Special case wildcard search with no extension (ie parameters ending in '.') as
877 GetFullPathName strips off the additional '.' */
878 if (fullname[strlenW(fullname)-1] == '.') strcatW(path, dotW);
881 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
882 thisEntry = HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
883 if (fullParms == NULL) fullParms = thisEntry;
884 if (prevEntry != NULL) prevEntry->next = thisEntry;
885 prevEntry = thisEntry;
886 thisEntry->next = NULL;
888 /* Split into components */
889 WCMD_splitpath(path, drive, dir, fname, ext);
890 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
891 wine_dbgstr_w(drive), wine_dbgstr_w(dir),
892 wine_dbgstr_w(fname), wine_dbgstr_w(ext));
894 thisEntry->dirName = HeapAlloc(GetProcessHeap(),0,
895 sizeof(WCHAR) * (strlenW(drive)+strlenW(dir)+1));
896 strcpyW(thisEntry->dirName, drive);
897 strcatW(thisEntry->dirName, dir);
899 thisEntry->fileName = HeapAlloc(GetProcessHeap(),0,
900 sizeof(WCHAR) * (strlenW(fname)+strlenW(ext)+1));
901 strcpyW(thisEntry->fileName, fname);
902 strcatW(thisEntry->fileName, ext);
907 /* If just 'dir' entered, a '*' parameter is assumed */
908 if (fullParms == NULL) {
909 WINE_TRACE("Inserting default '*'\n");
910 fullParms = HeapAlloc(GetProcessHeap(),0, sizeof(DIRECTORY_STACK));
911 fullParms->next = NULL;
912 fullParms->dirName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * (strlenW(cwd)+1));
913 strcpyW(fullParms->dirName, cwd);
914 fullParms->fileName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * 2);
915 strcpyW(fullParms->fileName, starW);
920 thisEntry = fullParms;
923 while (thisEntry != NULL) {
925 /* Output disk free (trailer) and volume information (header) if the drive
927 if (lastDrive != toupper(thisEntry->dirName[0])) {
929 /* Trailer Information */
930 if (lastDrive != '?') {
932 WCMD_dir_trailer(prevEntry->dirName[0]);
935 lastDrive = toupper(thisEntry->dirName[0]);
940 WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
941 memcpy(drive, thisEntry->dirName, 2 * sizeof(WCHAR));
943 status = WCMD_volume (0, drive);
951 static const WCHAR newLine2[] = {'\n','\n','\0'};
952 if (!bare) WCMD_output_asis (newLine2);
955 /* Clear any errors from previous invocations, and process it */
957 prevEntry = thisEntry;
958 thisEntry = WCMD_list_directory (thisEntry, 0);
961 /* Trailer Information */
963 WCMD_dir_trailer(prevEntry->dirName[0]);
967 if (paged_mode) WCMD_leave_paged_mode();
969 /* Free storage allocated for parms */
970 while (fullParms != NULL) {
971 prevEntry = fullParms;
972 fullParms = prevEntry->next;
973 HeapFree(GetProcessHeap(),0,prevEntry->dirName);
974 HeapFree(GetProcessHeap(),0,prevEntry->fileName);
975 HeapFree(GetProcessHeap(),0,prevEntry);