2 * CMD - Wine-compatible command line interface - Directory functions.
4 * Copyright (C) 1999 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * On entry, global variables quals, param1, param2 contain
24 * the qualifiers (uppercased and concatenated) and parameters entered, with
25 * environment-variable and batch parameter substitution already done.
28 #define WIN32_LEAN_AND_MEAN
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(cmd);
35 int WCMD_dir_sort (const void *a, const void *b);
36 WCHAR * WCMD_filesize64 (ULONGLONG free);
37 WCHAR * WCMD_strrev (WCHAR *buff);
38 static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen);
39 static void WCMD_dir_trailer(WCHAR drive);
42 extern WCHAR quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
43 extern DWORD errorlevel;
45 typedef enum _DISPLAYTIME
52 typedef enum _DISPLAYORDER
60 static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *parms, int level);
61 static int file_total, dir_total, recurse, wide, bare, max_width, lower;
62 static int shortname, usernames;
63 static ULONGLONG byte_total;
64 static DISPLAYTIME dirTime;
65 static DISPLAYORDER dirOrder;
66 static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
67 static BOOL separator;
68 static ULONG showattrs, attrsbits;
70 static const WCHAR dotW[] = {'.','\0'};
71 static const WCHAR dotdotW[] = {'.','.','\0'};
72 static const WCHAR starW[] = {'*','\0'};
73 static const WCHAR slashW[] = {'\\','\0'};
74 static const WCHAR emptyW[] = {'\0'};
76 /*****************************************************************************
79 * List a file directory.
83 void WCMD_directory (WCHAR *cmd) {
85 WCHAR path[MAX_PATH], cwd[MAX_PATH];
86 int status, paged_mode;
87 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
89 WCHAR string[MAXSTRING];
91 int argsProcessed = 0;
94 BOOL trailerReqd = FALSE;
95 DIRECTORY_STACK *fullParms = NULL;
96 DIRECTORY_STACK *prevEntry = NULL;
97 DIRECTORY_STACK *thisEntry = NULL;
100 WCHAR fname[MAX_PATH];
102 static const WCHAR dircmdW[] = {'D','I','R','C','M','D','\0'};
106 /* Prefill Quals with (uppercased) DIRCMD env var */
107 if (GetEnvironmentVariable (dircmdW, string, sizeof(string)/sizeof(WCHAR))) {
109 while ( (*p = toupper(*p)) ) ++p;
110 strcatW(string,quals);
111 strcpyW(quals, string);
115 file_total = dir_total = 0;
117 /* Initialize all flags to their defaults as if no DIRCMD or quals */
129 orderReverse = FALSE;
130 orderGroupDirs = FALSE;
131 orderGroupDirsReverse = FALSE;
135 /* Handle args - Loop through so right most is the effective one */
136 /* Note: /- appears to be a negate rather than an off, eg. dir
137 /-W is wide, or dir /w /-w /-w is also wide */
139 while (*p && (*p=='/' || *p==' ')) {
141 if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
148 WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
150 case 'P': if (negate) paged_mode = !paged_mode;
151 else paged_mode = TRUE;
153 case 'S': if (negate) recurse = !recurse;
156 case 'W': if (negate) wide = !wide;
159 case 'B': if (negate) bare = !bare;
162 case 'L': if (negate) lower = !lower;
165 case 'X': if (negate) shortname = !shortname;
166 else shortname = TRUE;
168 case 'Q': if (negate) usernames = !usernames;
169 else usernames = TRUE;
171 case 'D': if (negate) orderByCol = !orderByCol;
172 else orderByCol = TRUE;
174 case 'C': if (negate) separator = !separator;
175 else separator = TRUE;
178 if (*p==':') p++; /* Skip optional : */
180 if (*p == 'A') dirTime = Access;
181 else if (*p == 'C') dirTime = Creation;
182 else if (*p == 'W') dirTime = Written;
184 /* Support /T and /T: with no parms, default to written */
185 else if (*p == 0x00 || *p == '/') {
187 p = p - 1; /* So when step on, move to '/' */
189 SetLastError(ERROR_INVALID_PARAMETER);
196 if (*p==':') p++; /* Skip optional : */
197 while (*p && *p != '/') {
198 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
200 case 'N': dirOrder = Name; break;
201 case 'E': dirOrder = Extension; break;
202 case 'S': dirOrder = Size; break;
203 case 'D': dirOrder = Date; break;
204 case '-': if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
205 else orderReverse = TRUE;
207 case 'G': orderGroupDirs = TRUE; break;
209 SetLastError(ERROR_INVALID_PARAMETER);
216 p = p - 1; /* So when step on, move to '/' */
221 if (*p==':') p++; /* Skip optional : */
222 while (*p && *p != '/') {
223 BOOL anegate = FALSE;
226 /* Note /A: - options are 'offs' not toggles */
232 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
234 case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
235 case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
236 case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
237 case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
238 case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
240 SetLastError(ERROR_INVALID_PARAMETER);
246 /* Keep running list of bits we care about */
249 /* Mask shows what MUST be in the bits we care about */
250 if (anegate) showattrs = showattrs & ~mask;
251 else showattrs |= mask;
255 p = p - 1; /* So when step on, move to '/' */
256 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs, attrsbits);
259 SetLastError(ERROR_INVALID_PARAMETER);
267 /* Handle conflicting args and initialization */
268 if (bare || shortname) wide = FALSE;
269 if (bare) shortname = FALSE;
270 if (wide) usernames = FALSE;
271 if (orderByCol) wide = TRUE;
274 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
275 max_width = consoleInfo.dwSize.X;
280 WCMD_enter_paged_mode(NULL);
286 GetCurrentDirectory (MAX_PATH, cwd);
287 strcatW(cwd, slashW);
289 /* Loop through all args, calculating full effective directory */
293 WCHAR fullname[MAXSTRING];
294 WCHAR *thisArg = WCMD_parameter (cmd, argno++, &argN);
295 if (argN && argN[0] != '/') {
297 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
298 if (thisArg[1] == ':' && thisArg[2] == '\\') {
299 strcpyW(fullname, thisArg);
300 } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
302 static const WCHAR envFmt[] = {'=','%','c',':','\0'};
303 wsprintf(envvar, envFmt, thisArg[0]);
304 if (!GetEnvironmentVariable(envvar, fullname, MAX_PATH)) {
305 static const WCHAR noEnvFmt[] = {'%','c',':','\0'};
306 wsprintf(fullname, noEnvFmt, thisArg[0]);
308 strcatW(fullname, slashW);
309 strcatW(fullname, &thisArg[2]);
310 } else if (thisArg[0] == '\\') {
311 memcpy(fullname, cwd, 2 * sizeof(WCHAR));
312 strcpyW(fullname+2, thisArg);
314 strcpyW(fullname, cwd);
315 strcatW(fullname, thisArg);
317 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
319 status = GetFullPathName (fullname, sizeof(path)/sizeof(WCHAR), path, NULL);
322 * If the path supplied does not include a wildcard, and the endpoint of the
323 * path references a directory, we need to list the *contents* of that
324 * directory not the directory file itself.
326 if ((strchrW(path, '*') == NULL) && (strchrW(path, '%') == NULL)) {
327 status = GetFileAttributes (path);
328 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
329 if (path[strlenW(path)-1] == '\\') {
330 strcatW (path, starW);
333 const WCHAR slashStarW[] = {'\\','*','\0'};
334 strcatW (path, slashStarW);
338 /* Special case wildcard search with no extension (ie parameters ending in '.') as
339 GetFullPathName strips off the additional '.' */
340 if (fullname[strlenW(fullname)-1] == '.') strcatW(path, dotW);
343 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
344 thisEntry = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
345 if (fullParms == NULL) fullParms = thisEntry;
346 if (prevEntry != NULL) prevEntry->next = thisEntry;
347 prevEntry = thisEntry;
348 thisEntry->next = NULL;
350 /* Split into components */
351 WCMD_splitpath(path, drive, dir, fname, ext);
352 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
353 wine_dbgstr_w(drive), wine_dbgstr_w(dir),
354 wine_dbgstr_w(fname), wine_dbgstr_w(ext));
356 thisEntry->dirName = HeapAlloc(GetProcessHeap(),0,
357 sizeof(WCHAR) * (strlenW(drive)+strlenW(dir)+1));
358 strcpyW(thisEntry->dirName, drive);
359 strcatW(thisEntry->dirName, dir);
361 thisEntry->fileName = HeapAlloc(GetProcessHeap(),0,
362 sizeof(WCHAR) * (strlenW(fname)+strlenW(ext)+1));
363 strcpyW(thisEntry->fileName, fname);
364 strcatW(thisEntry->fileName, ext);
369 /* If just 'dir' entered, a '*' parameter is assumed */
370 if (fullParms == NULL) {
371 WINE_TRACE("Inserting default '*'\n");
372 fullParms = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0, sizeof(DIRECTORY_STACK));
373 fullParms->next = NULL;
374 fullParms->dirName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * (strlenW(cwd)+1));
375 strcpyW(fullParms->dirName, cwd);
376 fullParms->fileName = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR) * 2);
377 strcpyW(fullParms->fileName, starW);
382 thisEntry = fullParms;
385 while (thisEntry != NULL) {
387 /* Output disk free (trailer) and volume information (header) if the drive
389 if (lastDrive != toupper(thisEntry->dirName[0])) {
391 /* Trailer Information */
392 if (lastDrive != '?') {
394 WCMD_dir_trailer(prevEntry->dirName[0]);
397 lastDrive = toupper(thisEntry->dirName[0]);
402 WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
403 memcpy(drive, thisEntry->dirName, 2 * sizeof(WCHAR));
405 status = WCMD_volume (0, drive);
413 static const WCHAR newLine2[] = {'\n','\n','\0'};
414 if (!bare) WCMD_output (newLine2);
417 /* Clear any errors from previous invocations, and process it */
419 prevEntry = thisEntry;
420 thisEntry = WCMD_list_directory (thisEntry, 0);
423 /* Trailer Information */
425 WCMD_dir_trailer(prevEntry->dirName[0]);
429 if (paged_mode) WCMD_leave_paged_mode();
431 /* Free storage allocated for parms */
432 while (fullParms != NULL) {
433 prevEntry = fullParms;
434 fullParms = prevEntry->next;
435 HeapFree(GetProcessHeap(),0,prevEntry->dirName);
436 HeapFree(GetProcessHeap(),0,prevEntry->fileName);
437 HeapFree(GetProcessHeap(),0,prevEntry);
441 /*****************************************************************************
442 * WCMD_list_directory
444 * List a single file directory. This function (and those below it) can be called
445 * recursively when the /S switch is used.
447 * FIXME: Assumes 24-line display for the /P qualifier.
450 static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int level) {
452 WCHAR string[1024], datestring[32], timestring[32];
453 WCHAR real_path[MAX_PATH];
458 int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
459 int numCols, numRows;
461 ULARGE_INTEGER byte_count, file_size;
462 DIRECTORY_STACK *parms;
463 int concurrentDirs = 0;
464 BOOL done_header = FALSE;
466 static const WCHAR fmtDir[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
467 '<','D','I','R','>',' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
468 static const WCHAR fmtFile[] = {'%','1','0','s',' ',' ','%','8','s',' ',' ',
469 ' ',' ','%','1','0','s',' ',' ','\0'};
470 static const WCHAR fmt2[] = {'%','-','1','3','s','\0'};
471 static const WCHAR fmt3[] = {'%','-','2','3','s','\0'};
472 static const WCHAR fmt4[] = {'%','s','\0'};
473 static const WCHAR fmt5[] = {'%','s','%','s','\0'};
478 byte_count.QuadPart = 0;
482 /* Loop merging all the files from consecutive parms which relate to the
483 same directory. Note issuing a directory header with no contents
484 mirrors what windows does */
486 fd = HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATA));
487 while (parms && strcmpW(inputparms->dirName, parms->dirName) == 0) {
490 /* Work out the full path + filename */
491 strcpyW(real_path, parms->dirName);
492 strcatW(real_path, parms->fileName);
494 /* Load all files into an in memory structure */
495 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
496 hff = FindFirstFile (real_path, (fd+entry_count));
497 if (hff != INVALID_HANDLE_VALUE) {
499 /* Skip any which are filtered out by attribute */
500 if (((fd+entry_count)->dwFileAttributes & attrsbits) != showattrs) continue;
504 /* Keep running track of longest filename for wide output */
505 if (wide || orderByCol) {
506 int tmpLen = strlenW((fd+(entry_count-1))->cFileName) + 3;
507 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
508 if (tmpLen > widest) widest = tmpLen;
511 fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATA));
514 WINE_ERR ("Out of memory");
518 } while (FindNextFile(hff, (fd+entry_count)) != 0);
522 /* Work out the actual current directory name without a trailing \ */
523 strcpyW(real_path, parms->dirName);
524 real_path[strlenW(parms->dirName)-1] = 0x00;
526 /* Output the results */
528 if (level != 0 && (entry_count > 0)) WCMD_output (newline);
529 if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
530 static const WCHAR headerW[] = {'D','i','r','e','c','t','o','r','y',' ','o','f',
531 ' ','%','s','\n','\n','\0'};
532 WCMD_output (headerW, real_path);
537 /* Move to next parm */
541 /* Handle case where everything is filtered out */
542 if (entry_count > 0) {
544 /* Sort the list of files */
545 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
547 /* Work out the number of columns */
548 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
549 if (wide || orderByCol) {
550 numCols = max(1, (int)max_width / widest);
551 numRows = entry_count / numCols;
552 if (entry_count % numCols) numRows++;
555 numRows = entry_count;
557 WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
559 for (rows=0; rows<numRows; rows++) {
560 BOOL addNewLine = TRUE;
561 for (cols=0; cols<numCols; cols++) {
564 /* Work out the index of the entry being pointed to */
566 i = (cols * numRows) + rows;
567 if (i >= entry_count) continue;
569 i = (rows * numCols) + cols;
570 if (i >= entry_count) continue;
573 /* /L convers all names to lower case */
575 WCHAR *p = (fd+i)->cFileName;
576 while ( (*p = tolower(*p)) ) ++p;
579 /* /Q gets file ownership information */
581 strcpyW (string, inputparms->dirName);
582 strcatW (string, (fd+i)->cFileName);
583 WCMD_getfileowner(string, username, sizeof(username)/sizeof(WCHAR));
586 if (dirTime == Written) {
587 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
588 } else if (dirTime == Access) {
589 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
591 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
593 FileTimeToSystemTime (&ft, &st);
594 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
595 sizeof(datestring)/sizeof(WCHAR));
596 GetTimeFormat (0, TIME_NOSECONDS, &st,
597 NULL, timestring, sizeof(timestring)/sizeof(WCHAR));
601 tmp_width = cur_width;
602 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
603 static const WCHAR fmt[] = {'[','%','s',']','\0'};
604 WCMD_output (fmt, (fd+i)->cFileName);
606 tmp_width = tmp_width + strlenW((fd+i)->cFileName) + 2;
608 static const WCHAR fmt[] = {'%','s','\0'};
609 WCMD_output (fmt, (fd+i)->cFileName);
610 tmp_width = tmp_width + strlenW((fd+i)->cFileName) ;
612 file_size.u.LowPart = (fd+i)->nFileSizeLow;
613 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
614 byte_count.QuadPart += file_size.QuadPart;
616 cur_width = cur_width + widest;
618 if ((cur_width + widest) > max_width) {
621 static const WCHAR fmt[] = {'%','*','.','s','\0'};
622 WCMD_output (fmt, (tmp_width - cur_width), emptyW);
625 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
629 WCMD_output (fmtDir, datestring, timestring);
630 if (shortname) WCMD_output (fmt2, (fd+i)->cAlternateFileName);
631 if (usernames) WCMD_output (fmt3, username);
632 WCMD_output(fmt4,(fd+i)->cFileName);
634 if (!((strcmpW((fd+i)->cFileName, dotW) == 0) ||
635 (strcmpW((fd+i)->cFileName, dotdotW) == 0))) {
636 WCMD_output (fmt5, recurse?inputparms->dirName:emptyW, (fd+i)->cFileName);
644 file_size.u.LowPart = (fd+i)->nFileSizeLow;
645 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
646 byte_count.QuadPart += file_size.QuadPart;
648 WCMD_output (fmtFile, datestring, timestring,
649 WCMD_filesize64(file_size.QuadPart));
650 if (shortname) WCMD_output (fmt2, (fd+i)->cAlternateFileName);
651 if (usernames) WCMD_output (fmt3, username);
652 WCMD_output(fmt4,(fd+i)->cFileName);
654 WCMD_output (fmt5, recurse?inputparms->dirName:emptyW, (fd+i)->cFileName);
658 if (addNewLine) WCMD_output (newline);
663 if (file_count == 1) {
664 static const WCHAR fmt[] = {' ',' ',' ',' ',' ',' ',' ','1',' ','f','i','l','e',' ',
665 '%','2','5','s',' ','b','y','t','e','s','\n','\0'};
666 WCMD_output (fmt, WCMD_filesize64 (byte_count.QuadPart));
669 static const WCHAR fmt[] = {'%','8','d',' ','f','i','l','e','s',' ','%','2','4','s',
670 ' ','b','y','t','e','s','\n','\0'};
671 WCMD_output (fmt, file_count, WCMD_filesize64 (byte_count.QuadPart));
674 byte_total = byte_total + byte_count.QuadPart;
675 file_total = file_total + file_count;
676 dir_total = dir_total + dir_count;
678 if (!bare && !recurse) {
679 if (dir_count == 1) {
680 static const WCHAR fmt[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','y',
681 ' ',' ',' ',' ',' ',' ',' ',' ',' ','\0'};
682 WCMD_output (fmt, 1);
684 static const WCHAR fmt[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i',
686 WCMD_output (fmt, dir_count);
690 HeapFree(GetProcessHeap(),0,fd);
692 /* When recursing, look in all subdirectories for matches */
694 DIRECTORY_STACK *dirStack = NULL;
695 DIRECTORY_STACK *lastEntry = NULL;
696 WIN32_FIND_DATA finddata;
698 /* Build path to search */
699 strcpyW(string, inputparms->dirName);
700 strcatW(string, starW);
702 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
703 hff = FindFirstFile (string, &finddata);
704 if (hff != INVALID_HANDLE_VALUE) {
706 if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
707 (strcmpW(finddata.cFileName, dotdotW) != 0) &&
708 (strcmpW(finddata.cFileName, dotW) != 0)) {
710 DIRECTORY_STACK *thisDir;
711 int dirsToCopy = concurrentDirs;
713 /* Loop creating list of subdirs for all concurrent entries */
715 while (dirsToCopy > 0) {
718 /* Work out search parameter in sub dir */
719 strcpyW (string, inputparms->dirName);
720 strcatW (string, finddata.cFileName);
721 strcatW (string, slashW);
722 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
724 /* Allocate memory, add to list */
725 thisDir = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
726 if (dirStack == NULL) dirStack = thisDir;
727 if (lastEntry != NULL) lastEntry->next = thisDir;
729 thisDir->next = NULL;
730 thisDir->dirName = HeapAlloc(GetProcessHeap(),0,
731 sizeof(WCHAR) * (strlenW(string)+1));
732 strcpyW(thisDir->dirName, string);
733 thisDir->fileName = HeapAlloc(GetProcessHeap(),0,
734 sizeof(WCHAR) * (strlenW(parms->fileName)+1));
735 strcpyW(thisDir->fileName, parms->fileName);
739 } while (FindNextFile(hff, &finddata) != 0);
742 while (dirStack != NULL) {
743 DIRECTORY_STACK *thisDir = dirStack;
744 dirStack = WCMD_list_directory (thisDir, 1);
745 while (thisDir != dirStack) {
746 DIRECTORY_STACK *tempDir = thisDir->next;
747 HeapFree(GetProcessHeap(),0,thisDir->dirName);
748 HeapFree(GetProcessHeap(),0,thisDir->fileName);
749 HeapFree(GetProcessHeap(),0,thisDir);
756 /* Handle case where everything is filtered out */
757 if ((file_total + dir_total == 0) && (level == 0)) {
758 SetLastError (ERROR_FILE_NOT_FOUND);
766 /*****************************************************************************
769 * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
770 * Result is returned in a static string overwritten with each call.
771 * FIXME: There must be a better algorithm!
774 WCHAR * WCMD_filesize64 (ULONGLONG n) {
779 static WCHAR buff[32];
784 if (separator && ((++i)%3 == 1)) *p++ = ',';
795 /*****************************************************************************
798 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
801 WCHAR * WCMD_strrev (WCHAR *buff) {
807 for (i=0; i<r/2; i++) {
809 buff[i] = buff[r-i-1];
816 /*****************************************************************************
819 * Sort based on the /O options supplied on the command line
821 int WCMD_dir_sort (const void *a, const void *b)
823 WIN32_FIND_DATA *filea = (WIN32_FIND_DATA *)a;
824 WIN32_FIND_DATA *fileb = (WIN32_FIND_DATA *)b;
827 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
828 requested sort order for the directory components */
829 if (orderGroupDirs &&
830 ((filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
831 (fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
833 BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
834 if (aDir) result = -1;
836 if (orderGroupDirsReverse) result = -result;
840 } else if (dirOrder == Name) {
841 result = lstrcmpiW(filea->cFileName, fileb->cFileName);
844 } else if (dirOrder == Size) {
845 ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
846 ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
847 if( sizea < sizeb ) result = -1;
848 else if( sizea == sizeb ) result = 0;
851 /* Order by Date: (Takes into account which date (/T option) */
852 } else if (dirOrder == Date) {
855 ULONG64 timea, timeb;
857 if (dirTime == Written) {
858 ft = &filea->ftLastWriteTime;
859 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
860 ft = &fileb->ftLastWriteTime;
861 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
862 } else if (dirTime == Access) {
863 ft = &filea->ftLastAccessTime;
864 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
865 ft = &fileb->ftLastAccessTime;
866 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
868 ft = &filea->ftCreationTime;
869 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
870 ft = &fileb->ftCreationTime;
871 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
873 if( timea < timeb ) result = -1;
874 else if( timea == timeb ) result = 0;
877 /* Order by Extension: (Takes into account which date (/T option) */
878 } else if (dirOrder == Extension) {
881 WCHAR fname[MAX_PATH];
882 WCHAR extA[MAX_PATH];
883 WCHAR extB[MAX_PATH];
885 /* Split into components */
886 WCMD_splitpath(filea->cFileName, drive, dir, fname, extA);
887 WCMD_splitpath(fileb->cFileName, drive, dir, fname, extB);
888 result = lstrcmpiW(extA, extB);
891 if (orderReverse) result = -result;
895 /*****************************************************************************
898 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
900 void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) {
902 ULONG sizeNeeded = 0;
904 WCHAR name[MAXSTRING];
905 WCHAR domain[MAXSTRING];
907 /* In case of error, return empty string */
910 /* Find out how much space we need for the owner security descritpor */
911 GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
914 if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
918 BOOL defaulted = FALSE;
919 ULONG nameLen = MAXSTRING;
920 ULONG domainLen = MAXSTRING;
921 SID_NAME_USE nameuse;
923 secBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(),0,sizeNeeded * sizeof(BYTE));
924 if(!secBuffer) return;
926 /* Get the owners security descriptor */
927 if(!GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, secBuffer,
928 sizeNeeded, &sizeNeeded)) {
929 HeapFree(GetProcessHeap(),0,secBuffer);
933 /* Get the SID from the SD */
934 if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
935 HeapFree(GetProcessHeap(),0,secBuffer);
939 /* Convert to a username */
940 if (LookupAccountSid(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
941 static const WCHAR fmt[] = {'%','s','%','c','%','s','\0'};
942 snprintfW(owner, ownerlen, fmt, domain, '\\', name);
944 HeapFree(GetProcessHeap(),0,secBuffer);
949 /*****************************************************************************
952 * Print out the trailer for the supplied drive letter
954 static void WCMD_dir_trailer(WCHAR drive) {
955 ULARGE_INTEGER avail, total, freebytes;
957 WCHAR driveName[4] = {'c',':','\\','\0'};
959 driveName[0] = drive;
960 status = GetDiskFreeSpaceEx (driveName, &avail, &total, &freebytes);
961 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName),
962 status, GetLastError());
964 if (errorlevel==0 && !bare) {
966 static const WCHAR fmt1[] = {'\n',' ',' ',' ',' ',' ','T','o','t','a','l',' ','f','i','l','e','s',
967 ' ','l','i','s','t','e','d',':','\n','%','8','d',' ','f','i','l','e',
968 's','%','2','5','s',' ','b','y','t','e','s','\n','\0'};
969 static const WCHAR fmt2[] = {'%','8','d',' ','d','i','r','e','c','t','o','r','i','e','s',' ','%',
970 '1','8','s',' ','b','y','t','e','s',' ','f','r','e','e','\n','\n',
972 WCMD_output (fmt1, file_total, WCMD_filesize64 (byte_total));
973 WCMD_output (fmt2, dir_total, WCMD_filesize64 (freebytes.QuadPart));
975 static const WCHAR fmt[] = {' ','%','1','8','s',' ','b','y','t','e','s',' ','f','r','e','e',
977 WCMD_output (fmt, WCMD_filesize64 (freebytes.QuadPart));