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
32 int WCMD_dir_sort (const void *a, const void *b);
33 void WCMD_list_directory (char *path, int level);
34 char * WCMD_filesize64 (ULONGLONG free);
35 char * WCMD_strrev (char *buff);
36 static void WCMD_getfileowner(char *filename, char *owner, int ownerlen);
39 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
40 extern DWORD errorlevel;
42 typedef enum _DISPLAYTIME
49 static int file_total, dir_total, recurse, wide, bare, max_width, lower;
50 static int shortname, usernames;
51 static ULONGLONG byte_total;
52 static DISPLAYTIME dirTime;
54 /*****************************************************************************
57 * List a file directory.
61 void WCMD_directory (void) {
63 char path[MAX_PATH], drive[8];
64 int status, paged_mode;
65 ULARGE_INTEGER avail, total, free;
66 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
70 file_total = dir_total = 0;
74 paged_mode = (strstr(quals, "/P") != NULL);
75 recurse = (strstr(quals, "/S") != NULL);
76 wide = (strstr(quals, "/W") != NULL);
77 bare = (strstr(quals, "/B") != NULL);
78 lower = (strstr(quals, "/L") != NULL);
79 shortname = (strstr(quals, "/X") != NULL);
80 usernames = (strstr(quals, "/Q") != NULL);
82 if ((p = strstr(quals, "/T")) != NULL) {
84 if (*p==':') p++; /* Skip optional : */
86 if (*p == 'A') dirTime = Access;
87 else if (*p == 'C') dirTime = Creation;
88 else if (*p == 'W') dirTime = Written;
90 /* Support /T and /T: with no parms, default to written */
91 else if (*p == '/') dirTime = Written;
93 SetLastError(ERROR_INVALID_PARAMETER);
99 /* Handle conflicting args and initialization */
100 if (bare || shortname) wide = FALSE;
101 if (bare) shortname = FALSE;
102 if (wide) usernames = FALSE;
105 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
106 max_width = consoleInfo.dwSize.X;
111 WCMD_enter_paged_mode();
114 if (param1[0] == '\0') strcpy (param1, ".");
115 status = GetFullPathName (param1, sizeof(path), path, NULL);
118 if (paged_mode) WCMD_leave_paged_mode();
121 lstrcpyn (drive, path, 3);
124 status = WCMD_volume (0, drive);
126 if (paged_mode) WCMD_leave_paged_mode();
131 WCMD_list_directory (path, 0);
132 lstrcpyn (drive, path, 4);
133 GetDiskFreeSpaceEx (drive, &avail, &total, &free);
137 WCMD_output ("\n\n Total files listed:\n%8d files%25s bytes\n",
138 file_total, WCMD_filesize64 (byte_total));
139 WCMD_output ("%8d directories %18s bytes free\n\n",
140 dir_total, WCMD_filesize64 (free.QuadPart));
142 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
145 if (paged_mode) WCMD_leave_paged_mode();
148 /*****************************************************************************
149 * WCMD_list_directory
151 * List a single file directory. This function (and those below it) can be called
152 * recursively when the /S switch is used.
154 * FIXME: Entries sorted by name only. Should we support DIRCMD??
155 * FIXME: Assumes 24-line display for the /P qualifier.
156 * FIXME: Other command qualifiers not supported.
157 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
160 void WCMD_list_directory (char *search_path, int level) {
162 char string[1024], datestring[32], timestring[32];
164 char real_path[MAX_PATH];
169 int status, dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
170 ULARGE_INTEGER byte_count, file_size;
175 byte_count.QuadPart = 0;
180 * If the path supplied does not include a wildcard, and the endpoint of the
181 * path references a directory, we need to list the *contents* of that
182 * directory not the directory file itself.
185 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
186 status = GetFileAttributes (search_path);
187 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
188 if (search_path[strlen(search_path)-1] == '\\') {
189 strcat (search_path, "*");
192 strcat (search_path, "\\*");
197 /* Work out the actual current directory name */
198 p = strrchr (search_path, '\\');
199 memset(real_path, 0x00, sizeof(real_path));
200 lstrcpyn (real_path, search_path, (p-search_path+2));
202 /* Load all files into an in memory structure */
203 fd = HeapAlloc(GetProcessHeap(),0,sizeof(WIN32_FIND_DATA));
204 hff = FindFirstFile (search_path, fd);
205 if (hff == INVALID_HANDLE_VALUE) {
206 SetLastError (ERROR_FILE_NOT_FOUND);
208 HeapFree(GetProcessHeap(),0,fd);
214 /* Keep running track of longest filename for wide output */
216 int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
217 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
218 if (tmpLen > widest) widest = tmpLen;
221 fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATA));
224 WCMD_output ("Memory Allocation Error");
227 } while (FindNextFile(hff, (fd+entry_count)) != 0);
230 /* Sort the list of files */
231 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
233 /* Output the results */
235 if (level != 0) WCMD_output ("\n\n");
236 WCMD_output ("Directory of %s\n\n", real_path);
239 for (i=0; i<entry_count; i++) {
242 /* /L convers all names to lower case */
244 char *p = (fd+i)->cFileName;
245 while ( (*p = tolower(*p)) ) ++p;
248 /* /Q gets file ownership information */
250 p = strrchr (search_path, '\\');
251 lstrcpyn (string, search_path, (p-search_path+2));
252 lstrcat (string, (fd+i)->cFileName);
253 WCMD_getfileowner(string, username, sizeof(username));
256 if (dirTime == Written) {
257 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
258 } else if (dirTime == Access) {
259 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
261 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
263 FileTimeToSystemTime (&ft, &st);
264 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
266 GetTimeFormat (0, TIME_NOSECONDS, &st,
267 NULL, timestring, sizeof(timestring));
271 tmp_width = cur_width;
272 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
273 WCMD_output ("[%s]", (fd+i)->cFileName);
275 tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
277 WCMD_output ("%s", (fd+i)->cFileName);
278 tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
280 file_size.u.LowPart = (fd+i)->nFileSizeLow;
281 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
282 byte_count.QuadPart += file_size.QuadPart;
284 cur_width = cur_width + widest;
286 if ((cur_width + widest) > max_width) {
290 WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
293 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
297 WCMD_output ("%10s %8s <DIR> ", datestring, timestring);
298 if (shortname) WCMD_output ("%-13s", (fd+i)->cAlternateFileName);
299 if (usernames) WCMD_output ("%-23s", username);
300 WCMD_output("%s\n",(fd+i)->cFileName);
302 if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
303 (strcmp((fd+i)->cFileName, "..") == 0))) {
304 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
310 file_size.u.LowPart = (fd+i)->nFileSizeLow;
311 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
312 byte_count.QuadPart += file_size.QuadPart;
314 WCMD_output ("%10s %8s %10s ", datestring, timestring,
315 WCMD_filesize64(file_size.QuadPart));
316 if (shortname) WCMD_output ("%-13s", (fd+i)->cAlternateFileName);
317 if (usernames) WCMD_output ("%-23s", username);
318 WCMD_output("%s\n",(fd+i)->cFileName);
320 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
325 if (wide && cur_width>0) {
330 if (file_count == 1) {
331 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
334 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
337 byte_total = byte_total + byte_count.QuadPart;
338 file_total = file_total + file_count;
339 dir_total = dir_total + dir_count;
342 if (dir_count == 1) WCMD_output ("1 directory ");
343 else WCMD_output ("%8d directories", dir_count);
345 for (i=0; i<entry_count; i++) {
347 ((fd+i)->cFileName[0] != '.') &&
348 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
350 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
352 p = strrchr (search_path, '\\');
353 lstrcpyn (string, search_path, (p-search_path+2));
354 lstrcat (string, (fd+i)->cFileName);
356 WCMD_list_directory (string, 1);
359 HeapFree(GetProcessHeap(),0,fd);
363 /*****************************************************************************
366 * Convert a 64-bit number into a character string, with commas every three digits.
367 * Result is returned in a static string overwritten with each call.
368 * FIXME: There must be a better algorithm!
371 char * WCMD_filesize64 (ULONGLONG n) {
376 static char buff[32];
381 if ((++i)%3 == 1) *p++ = ',';
392 /*****************************************************************************
395 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
398 char * WCMD_strrev (char *buff) {
404 for (i=0; i<r/2; i++) {
406 buff[i] = buff[r-i-1];
413 int WCMD_dir_sort (const void *a, const void *b)
415 return (lstrcmpi(((const WIN32_FIND_DATA *)a)->cFileName,
416 ((const WIN32_FIND_DATA *)b)->cFileName));
419 /*****************************************************************************
422 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
424 void WCMD_getfileowner(char *filename, char *owner, int ownerlen) {
426 ULONG sizeNeeded = 0;
428 char name[MAXSTRING];
429 char domain[MAXSTRING];
431 /* In case of error, return empty string */
434 /* Find out how much space we need for the owner security descritpor */
435 GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
438 if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
442 BOOL defaulted = FALSE;
443 ULONG nameLen = MAXSTRING;
444 ULONG domainLen = MAXSTRING;
445 SID_NAME_USE nameuse;
447 secBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(),0,sizeNeeded * sizeof(BYTE));
448 if(!secBuffer) return;
450 /* Get the owners security descriptor */
451 if(!GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, secBuffer,
452 sizeNeeded, &sizeNeeded)) {
453 HeapFree(GetProcessHeap(),0,secBuffer);
457 /* Get the SID from the SD */
458 if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
459 HeapFree(GetProcessHeap(),0,secBuffer);
463 /* Convert to a username */
464 if (LookupAccountSid(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
465 snprintf(owner, ownerlen, "%s%c%s", domain, '\\', name);
467 HeapFree(GetProcessHeap(),0,secBuffer);