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 static void WCMD_list_directory (DIRECTORY_STACK *parms, int level);
37 char * WCMD_filesize64 (ULONGLONG free);
38 char * WCMD_strrev (char *buff);
39 static void WCMD_getfileowner(char *filename, char *owner, int ownerlen);
40 static void WCMD_dir_trailer(char drive);
43 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
44 extern DWORD errorlevel;
46 typedef enum _DISPLAYTIME
53 typedef enum _DISPLAYORDER
61 typedef struct _DIRECTORY_STACK
63 struct _DIRECTORY_STACK *next;
68 static int file_total, dir_total, recurse, wide, bare, max_width, lower;
69 static int shortname, usernames;
70 static ULONGLONG byte_total;
71 static DISPLAYTIME dirTime;
72 static DISPLAYORDER dirOrder;
73 static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
74 static BOOL separator;
75 static ULONG showattrs, attrsbits;
77 /*****************************************************************************
80 * List a file directory.
84 void WCMD_directory (char *cmd) {
86 char path[MAX_PATH], cwd[MAX_PATH];
87 int status, paged_mode;
88 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
90 char string[MAXSTRING];
92 int argsProcessed = 0;
95 BOOL trailerReqd = FALSE;
96 DIRECTORY_STACK *fullParms = NULL;
97 DIRECTORY_STACK *prevEntry = NULL;
98 DIRECTORY_STACK *thisEntry = NULL;
101 char fname[MAX_PATH];
106 /* Prefill Quals with (uppercased) DIRCMD env var */
107 if (GetEnvironmentVariable ("DIRCMD", string, sizeof(string))) {
109 while ( (*p = toupper(*p)) ) ++p;
110 strcat(string,quals);
111 strcpy(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, 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, 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, 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();
286 GetCurrentDirectory (1024, cwd);
289 /* Loop through all args, calculating full effective directory */
293 char fullname[MAXSTRING];
294 char *thisArg = WCMD_parameter (cmd, argno++, &argN);
295 if (argN && argN[0] != '/') {
297 WINE_TRACE("Found parm '%s'\n", thisArg);
298 if (thisArg[1] == ':' && thisArg[2] == '\\') {
299 strcpy(fullname, thisArg);
300 } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
302 sprintf(envvar, "=%c:", thisArg[0]);
303 if (!GetEnvironmentVariable(envvar, fullname, MAX_PATH)) {
304 sprintf(fullname, "%c:", thisArg[0]);
306 strcat(fullname, "\\");
307 strcat(fullname, &thisArg[2]);
308 } else if (thisArg[0] == '\\') {
309 strncpy(fullname, cwd, 2);
311 strcat((fullname+2), thisArg);
313 strcpy(fullname, cwd);
314 strcat(fullname, thisArg);
316 WINE_TRACE("Using location '%s'\n", fullname);
318 status = GetFullPathName (fullname, sizeof(path), path, NULL);
319 WINE_TRACE("Using path '%s'\n", path);
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 ((strchr(path, '*') == NULL) && (strchr(path, '%') == NULL)) {
327 status = GetFileAttributes (path);
328 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
329 if (path[strlen(path)-1] == '\\') {
333 strcat (path, "\\*");
338 thisEntry = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
339 if (fullParms == NULL) fullParms = thisEntry;
340 if (prevEntry != NULL) prevEntry->next = thisEntry;
341 prevEntry = thisEntry;
342 thisEntry->next = NULL;
344 /* Split into components */
345 WCMD_splitpath(path, drive, dir, fname, ext);
346 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
347 drive, dir, fname, ext);
349 thisEntry->dirName = HeapAlloc(GetProcessHeap(),0,strlen(drive)+strlen(dir)+1);
350 strcpy(thisEntry->dirName, drive);
351 strcat(thisEntry->dirName, dir);
353 thisEntry->fileName = HeapAlloc(GetProcessHeap(),0,strlen(fname)+strlen(ext)+1);
354 strcpy(thisEntry->fileName, fname);
355 strcat(thisEntry->fileName, ext);
360 /* If just 'dir' entered, a '*' parameter is assumed */
361 if (fullParms == NULL) {
362 WINE_TRACE("Inserting default '*'\n");
363 fullParms = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0, sizeof(DIRECTORY_STACK));
364 fullParms->next = NULL;
365 fullParms->dirName = HeapAlloc(GetProcessHeap(),0,(strlen(cwd)+1));
366 strcpy(fullParms->dirName, cwd);
367 fullParms->fileName = HeapAlloc(GetProcessHeap(),0,2);
368 strcpy(fullParms->fileName, "*");
373 thisEntry = fullParms;
376 while (thisEntry != NULL) {
378 /* Output disk free (trailer) and volume information (header) if the drive
380 if (lastDrive != toupper(thisEntry->dirName[0])) {
382 /* Trailer Information */
383 if (lastDrive != '?') {
385 WCMD_dir_trailer(prevEntry->dirName[0]);
388 lastDrive = toupper(thisEntry->dirName[0]);
393 WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
394 strncpy(drive, thisEntry->dirName, 2);
396 status = WCMD_volume (0, drive);
404 if (!bare) WCMD_output ("\n\n");
407 /* Clear any errors from previous invocations, and process it */
409 WCMD_list_directory (thisEntry, 0);
411 /* Step to next parm */
412 prevEntry = thisEntry;
413 thisEntry = thisEntry->next;
416 /* Trailer Information */
418 WCMD_dir_trailer(prevEntry->dirName[0]);
422 if (paged_mode) WCMD_leave_paged_mode();
424 /* Free storage allocated for parms */
425 while (fullParms != NULL) {
426 prevEntry = fullParms;
427 fullParms = prevEntry->next;
428 HeapFree(GetProcessHeap(),0,prevEntry->dirName);
429 HeapFree(GetProcessHeap(),0,prevEntry->fileName);
430 HeapFree(GetProcessHeap(),0,prevEntry);
434 /*****************************************************************************
435 * WCMD_list_directory
437 * List a single file directory. This function (and those below it) can be called
438 * recursively when the /S switch is used.
440 * FIXME: Assumes 24-line display for the /P qualifier.
443 static void WCMD_list_directory (DIRECTORY_STACK *parms, int level) {
445 char string[1024], datestring[32], timestring[32];
446 char real_path[MAX_PATH];
451 int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
452 int numCols, numRows;
454 ULARGE_INTEGER byte_count, file_size;
459 byte_count.QuadPart = 0;
463 /* Work out the full path + filename */
464 strcpy(real_path, parms->dirName);
465 strcat(real_path, parms->fileName);
467 /* Load all files into an in memory structure */
468 fd = HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATA));
469 WINE_TRACE("Looking for matches to '%s'\n", real_path);
470 hff = FindFirstFile (real_path, fd);
471 if (hff == INVALID_HANDLE_VALUE) {
475 /* Skip any which are filtered out by attribute */
476 if (((fd+entry_count)->dwFileAttributes & attrsbits) != showattrs) continue;
480 /* Keep running track of longest filename for wide output */
481 if (wide || orderByCol) {
482 int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
483 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
484 if (tmpLen > widest) widest = tmpLen;
487 fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATA));
490 WCMD_output ("Memory Allocation Error");
494 } while (FindNextFile(hff, (fd+entry_count)) != 0);
498 /* Work out the actual current directory name without a trailing \ */
499 strcpy(real_path, parms->dirName);
500 real_path[strlen(parms->dirName)-1] = 0x00;
502 /* Output the results */
504 if (level != 0 && (entry_count > 0)) WCMD_output ("\n");
505 if ((entry_count > 0) || (!recurse && level == 0)) WCMD_output ("Directory of %s\n\n", real_path);
508 /* Handle case where everything is filtered out */
509 if (entry_count > 0) {
511 /* Sort the list of files */
512 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
514 /* Work out the number of columns */
515 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
516 if (wide || orderByCol) {
517 numCols = max(1, (int)max_width / widest);
518 numRows = entry_count / numCols;
519 if (entry_count % numCols) numRows++;
522 numRows = entry_count;
524 WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
526 for (rows=0; rows<numRows; rows++) {
527 BOOL addNewLine = TRUE;
528 for (cols=0; cols<numCols; cols++) {
531 /* Work out the index of the entry being pointed to */
533 i = (cols * numRows) + rows;
534 if (i >= entry_count) continue;
536 i = (rows * numCols) + cols;
537 if (i >= entry_count) continue;
540 /* /L convers all names to lower case */
542 char *p = (fd+i)->cFileName;
543 while ( (*p = tolower(*p)) ) ++p;
546 /* /Q gets file ownership information */
548 lstrcpy (string, parms->dirName);
549 lstrcat (string, (fd+i)->cFileName);
550 WCMD_getfileowner(string, username, sizeof(username));
553 if (dirTime == Written) {
554 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
555 } else if (dirTime == Access) {
556 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
558 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
560 FileTimeToSystemTime (&ft, &st);
561 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
563 GetTimeFormat (0, TIME_NOSECONDS, &st,
564 NULL, timestring, sizeof(timestring));
568 tmp_width = cur_width;
569 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
570 WCMD_output ("[%s]", (fd+i)->cFileName);
572 tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
574 WCMD_output ("%s", (fd+i)->cFileName);
575 tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
577 file_size.u.LowPart = (fd+i)->nFileSizeLow;
578 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
579 byte_count.QuadPart += file_size.QuadPart;
581 cur_width = cur_width + widest;
583 if ((cur_width + widest) > max_width) {
586 WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
589 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
593 WCMD_output ("%10s %8s <DIR> ", datestring, timestring);
594 if (shortname) WCMD_output ("%-13s", (fd+i)->cAlternateFileName);
595 if (usernames) WCMD_output ("%-23s", username);
596 WCMD_output("%s",(fd+i)->cFileName);
598 if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
599 (strcmp((fd+i)->cFileName, "..") == 0))) {
600 WCMD_output ("%s%s", recurse?parms->dirName:"", (fd+i)->cFileName);
608 file_size.u.LowPart = (fd+i)->nFileSizeLow;
609 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
610 byte_count.QuadPart += file_size.QuadPart;
612 WCMD_output ("%10s %8s %10s ", datestring, timestring,
613 WCMD_filesize64(file_size.QuadPart));
614 if (shortname) WCMD_output ("%-13s", (fd+i)->cAlternateFileName);
615 if (usernames) WCMD_output ("%-23s", username);
616 WCMD_output("%s",(fd+i)->cFileName);
618 WCMD_output ("%s%s", recurse?parms->dirName:"", (fd+i)->cFileName);
622 if (addNewLine) WCMD_output ("\n");
627 if (file_count == 1) {
628 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
631 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
634 byte_total = byte_total + byte_count.QuadPart;
635 file_total = file_total + file_count;
636 dir_total = dir_total + dir_count;
638 if (!bare && !recurse) {
639 if (dir_count == 1) WCMD_output ("%8d directory ", 1);
640 else WCMD_output ("%8d directories", dir_count);
643 HeapFree(GetProcessHeap(),0,fd);
645 /* When recursing, look in all subdirectories for matches */
647 DIRECTORY_STACK *dirStack = NULL;
648 DIRECTORY_STACK *lastEntry = NULL;
649 WIN32_FIND_DATA finddata;
651 /* Build path to search */
652 strcpy(string, parms->dirName);
655 WINE_TRACE("Recursive, looking for '%s'\n", string);
656 hff = FindFirstFile (string, &finddata);
657 if (hff != INVALID_HANDLE_VALUE) {
659 if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
660 (strcmp(finddata.cFileName, "..") != 0) &&
661 (strcmp(finddata.cFileName, ".") != 0)) {
663 DIRECTORY_STACK *thisDir;
665 /* Work out search parameter in sub dir */
666 strcpy (string, parms->dirName);
667 strcat (string, finddata.cFileName);
668 strcat (string, "\\");
669 WINE_TRACE("Recursive, Adding to search list '%s'\n", string);
671 /* Allocate memory, add to list */
672 thisDir = (DIRECTORY_STACK *) HeapAlloc(GetProcessHeap(),0,sizeof(DIRECTORY_STACK));
673 if (dirStack == NULL) dirStack = thisDir;
674 if (lastEntry != NULL) lastEntry->next = thisDir;
676 thisDir->next = NULL;
677 thisDir->dirName = HeapAlloc(GetProcessHeap(),0,(strlen(string)+1));
678 strcpy(thisDir->dirName, string);
679 thisDir->fileName = HeapAlloc(GetProcessHeap(),0,(strlen(parms->fileName)+1));
680 strcpy(thisDir->fileName, parms->fileName);
682 } while (FindNextFile(hff, &finddata) != 0);
685 while (dirStack != NULL) {
686 DIRECTORY_STACK *thisDir = dirStack;
687 dirStack = thisDir->next;
689 WCMD_list_directory (thisDir, 1);
690 HeapFree(GetProcessHeap(),0,thisDir->dirName);
691 HeapFree(GetProcessHeap(),0,thisDir->fileName);
692 HeapFree(GetProcessHeap(),0,thisDir);
697 /* Handle case where everything is filtered out */
698 if ((file_total + dir_total == 0) && (level == 0)) {
699 SetLastError (ERROR_FILE_NOT_FOUND);
705 /*****************************************************************************
708 * Convert a 64-bit number into a character string, with commas every three digits.
709 * Result is returned in a static string overwritten with each call.
710 * FIXME: There must be a better algorithm!
713 char * WCMD_filesize64 (ULONGLONG n) {
718 static char buff[32];
723 if (separator && ((++i)%3 == 1)) *p++ = ',';
734 /*****************************************************************************
737 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
740 char * WCMD_strrev (char *buff) {
746 for (i=0; i<r/2; i++) {
748 buff[i] = buff[r-i-1];
755 /*****************************************************************************
758 * Sort based on the /O options supplied on the command line
760 int WCMD_dir_sort (const void *a, const void *b)
762 WIN32_FIND_DATA *filea = (WIN32_FIND_DATA *)a;
763 WIN32_FIND_DATA *fileb = (WIN32_FIND_DATA *)b;
766 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
767 requested sort order for the directory components */
768 if (orderGroupDirs &&
769 ((filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
770 (fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
772 BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
773 if (aDir) result = -1;
775 if (orderGroupDirsReverse) result = -result;
779 } else if (dirOrder == Name) {
780 result = lstrcmpi(filea->cFileName, fileb->cFileName);
783 } else if (dirOrder == Size) {
784 ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
785 ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
786 if( sizea < sizeb ) result = -1;
787 else if( sizea == sizeb ) result = 0;
790 /* Order by Date: (Takes into account which date (/T option) */
791 } else if (dirOrder == Date) {
794 ULONG64 timea, timeb;
796 if (dirTime == Written) {
797 ft = &filea->ftLastWriteTime;
798 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
799 ft = &fileb->ftLastWriteTime;
800 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
801 } else if (dirTime == Access) {
802 ft = &filea->ftLastAccessTime;
803 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
804 ft = &fileb->ftLastAccessTime;
805 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
807 ft = &filea->ftCreationTime;
808 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
809 ft = &fileb->ftCreationTime;
810 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
812 if( timea < timeb ) result = -1;
813 else if( timea == timeb ) result = 0;
816 /* Order by Extension: (Takes into account which date (/T option) */
817 } else if (dirOrder == Extension) {
820 char fname[MAX_PATH];
824 /* Split into components */
825 WCMD_splitpath(filea->cFileName, drive, dir, fname, extA);
826 WCMD_splitpath(fileb->cFileName, drive, dir, fname, extB);
827 result = lstrcmpi(extA, extB);
830 if (orderReverse) result = -result;
834 /*****************************************************************************
837 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
839 void WCMD_getfileowner(char *filename, char *owner, int ownerlen) {
841 ULONG sizeNeeded = 0;
843 char name[MAXSTRING];
844 char domain[MAXSTRING];
846 /* In case of error, return empty string */
849 /* Find out how much space we need for the owner security descritpor */
850 GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
853 if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
857 BOOL defaulted = FALSE;
858 ULONG nameLen = MAXSTRING;
859 ULONG domainLen = MAXSTRING;
860 SID_NAME_USE nameuse;
862 secBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(),0,sizeNeeded * sizeof(BYTE));
863 if(!secBuffer) return;
865 /* Get the owners security descriptor */
866 if(!GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, secBuffer,
867 sizeNeeded, &sizeNeeded)) {
868 HeapFree(GetProcessHeap(),0,secBuffer);
872 /* Get the SID from the SD */
873 if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
874 HeapFree(GetProcessHeap(),0,secBuffer);
878 /* Convert to a username */
879 if (LookupAccountSid(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
880 snprintf(owner, ownerlen, "%s%c%s", domain, '\\', name);
882 HeapFree(GetProcessHeap(),0,secBuffer);
887 /*****************************************************************************
890 * Print out the trailer for the supplied drive letter
892 static void WCMD_dir_trailer(char drive) {
893 ULARGE_INTEGER avail, total, freebytes;
895 char driveName[4] = "c:\\";
897 driveName[0] = drive;
898 status = GetDiskFreeSpaceEx (driveName, &avail, &total, &freebytes);
899 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", driveName, status, GetLastError());
901 if (errorlevel==0 && !bare) {
903 WCMD_output ("\n Total files listed:\n%8d files%25s bytes\n",
904 file_total, WCMD_filesize64 (byte_total));
905 WCMD_output ("%8d directories %18s bytes free\n\n",
906 dir_total, WCMD_filesize64 (freebytes.QuadPart));
908 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (freebytes.QuadPart));