msado15: Implement ADORecordsetConstruction get/put Rowset.
[wine.git] / programs / cmd / directory.c
blob2d35f8eb1d1696f727a496a5e8155a0bb50468ae
1 /*
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
22 #define WIN32_LEAN_AND_MEAN
24 #include "wcmd.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(cmd);
29 typedef enum _DISPLAYTIME
31 Creation = 0,
32 Access,
33 Written
34 } DISPLAYTIME;
36 typedef enum _DISPLAYORDER
38 Name = 0,
39 Extension,
40 Size,
41 Date
42 } DISPLAYORDER;
44 static int file_total, dir_total, max_width;
45 static ULONGLONG byte_total;
46 static DISPLAYTIME dirTime;
47 static DISPLAYORDER dirOrder;
48 static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
49 static BOOL paged_mode, recurse, wide, bare, lower, shortname, usernames, separator;
50 static ULONG showattrs, attrsbits;
52 /*****************************************************************************
53 * WCMD_strrev
55 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
57 static WCHAR * WCMD_strrev (WCHAR *buff) {
59 int r, i;
60 WCHAR b;
62 r = lstrlenW (buff);
63 for (i=0; i<r/2; i++) {
64 b = buff[i];
65 buff[i] = buff[r-i-1];
66 buff[r-i-1] = b;
68 return (buff);
71 /*****************************************************************************
72 * WCMD_filesize64
74 * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
75 * Result is returned in a static string overwritten with each call.
76 * FIXME: There must be a better algorithm!
78 static WCHAR * WCMD_filesize64 (ULONGLONG n) {
80 ULONGLONG q;
81 unsigned int r, i;
82 WCHAR *p;
83 static WCHAR buff[32];
85 p = buff;
86 i = -3;
87 do {
88 if (separator && ((++i)%3 == 1)) *p++ = ',';
89 q = n / 10;
90 r = n - (q * 10);
91 *p++ = r + '0';
92 *p = '\0';
93 n = q;
94 } while (n != 0);
95 WCMD_strrev (buff);
96 return buff;
99 /*****************************************************************************
100 * WCMD_dir_sort
102 * Sort based on the /O options supplied on the command line
104 static int __cdecl WCMD_dir_sort (const void *a, const void *b)
106 const WIN32_FIND_DATAW *filea = (const WIN32_FIND_DATAW *)a;
107 const WIN32_FIND_DATAW *fileb = (const WIN32_FIND_DATAW *)b;
108 int result = 0;
110 /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
111 requested sort order for the directory components */
112 if (orderGroupDirs &&
113 ((filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
114 (fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
116 BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
117 if (aDir) result = -1;
118 else result = 1;
119 if (orderGroupDirsReverse) result = -result;
120 return result;
122 /* Order by Name: */
123 } else if (dirOrder == Name) {
124 result = lstrcmpiW(filea->cFileName, fileb->cFileName);
126 /* Order by Size: */
127 } else if (dirOrder == Size) {
128 ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
129 ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
130 if( sizea < sizeb ) result = -1;
131 else if( sizea == sizeb ) result = 0;
132 else result = 1;
134 /* Order by Date: (Takes into account which date (/T option) */
135 } else if (dirOrder == Date) {
137 const FILETIME *ft;
138 ULONG64 timea, timeb;
140 if (dirTime == Written) {
141 ft = &filea->ftLastWriteTime;
142 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
143 ft = &fileb->ftLastWriteTime;
144 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
145 } else if (dirTime == Access) {
146 ft = &filea->ftLastAccessTime;
147 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
148 ft = &fileb->ftLastAccessTime;
149 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
150 } else {
151 ft = &filea->ftCreationTime;
152 timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
153 ft = &fileb->ftCreationTime;
154 timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
156 if( timea < timeb ) result = -1;
157 else if( timea == timeb ) result = 0;
158 else result = 1;
160 /* Order by Extension: (Takes into account which date (/T option) */
161 } else if (dirOrder == Extension) {
162 WCHAR drive[10];
163 WCHAR dir[MAX_PATH];
164 WCHAR fname[MAX_PATH];
165 WCHAR extA[MAX_PATH];
166 WCHAR extB[MAX_PATH];
168 /* Split into components */
169 _wsplitpath(filea->cFileName, drive, dir, fname, extA);
170 _wsplitpath(fileb->cFileName, drive, dir, fname, extB);
171 result = lstrcmpiW(extA, extB);
174 if (orderReverse) result = -result;
175 return result;
178 /*****************************************************************************
179 * WCMD_getfileowner
181 * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
183 static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) {
185 ULONG sizeNeeded = 0;
186 DWORD rc;
187 WCHAR name[MAXSTRING];
188 WCHAR domain[MAXSTRING];
190 /* In case of error, return empty string */
191 *owner = 0x00;
193 /* Find out how much space we need for the owner security descriptor */
194 GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
195 rc = GetLastError();
197 if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
199 LPBYTE secBuffer;
200 PSID pSID = NULL;
201 BOOL defaulted = FALSE;
202 ULONG nameLen = MAXSTRING;
203 ULONG domainLen = MAXSTRING;
204 SID_NAME_USE nameuse;
206 secBuffer = heap_xalloc(sizeNeeded * sizeof(BYTE));
208 /* Get the owners security descriptor */
209 if(!GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, secBuffer,
210 sizeNeeded, &sizeNeeded)) {
211 heap_free(secBuffer);
212 return;
215 /* Get the SID from the SD */
216 if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
217 heap_free(secBuffer);
218 return;
221 /* Convert to a username */
222 if (LookupAccountSidW(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
223 swprintf(owner, ownerlen, L"%s%c%s", domain, '\\', name);
225 heap_free(secBuffer);
227 return;
230 /*****************************************************************************
231 * WCMD_list_directory
233 * List a single file directory. This function (and those below it) can be called
234 * recursively when the /S switch is used.
236 * FIXME: Assumes 24-line display for the /P qualifier.
239 static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int level) {
241 WCHAR string[1024], datestring[32], timestring[32];
242 WCHAR real_path[MAX_PATH];
243 WIN32_FIND_DATAW *fd;
244 FILETIME ft;
245 SYSTEMTIME st;
246 HANDLE hff;
247 int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
248 int numCols, numRows;
249 int rows, cols;
250 ULARGE_INTEGER byte_count, file_size;
251 DIRECTORY_STACK *parms;
252 int concurrentDirs = 0;
253 BOOL done_header = FALSE;
255 static const WCHAR fmt2[] = {'%','1','!','-','1','3','s','!','\0'};
256 static const WCHAR fmt3[] = {'%','1','!','-','2','3','s','!','\0'};
257 static const WCHAR fmt4[] = {'%','1','\0'};
258 static const WCHAR fmt5[] = {'%','1','%','2','\0'};
260 dir_count = 0;
261 file_count = 0;
262 entry_count = 0;
263 byte_count.QuadPart = 0;
264 widest = 0;
265 cur_width = 0;
267 /* Loop merging all the files from consecutive parms which relate to the
268 same directory. Note issuing a directory header with no contents
269 mirrors what windows does */
270 parms = inputparms;
271 fd = heap_xalloc(sizeof(WIN32_FIND_DATAW));
272 while (parms && lstrcmpW(inputparms->dirName, parms->dirName) == 0) {
273 concurrentDirs++;
275 /* Work out the full path + filename */
276 lstrcpyW(real_path, parms->dirName);
277 lstrcatW(real_path, parms->fileName);
279 /* Load all files into an in memory structure */
280 WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
281 hff = FindFirstFileW(real_path, &fd[entry_count]);
282 if (hff != INVALID_HANDLE_VALUE) {
283 do {
284 /* Skip any which are filtered out by attribute */
285 if ((fd[entry_count].dwFileAttributes & attrsbits) != showattrs) continue;
287 entry_count++;
289 /* Keep running track of longest filename for wide output */
290 if (wide || orderByCol) {
291 int tmpLen = lstrlenW(fd[entry_count-1].cFileName) + 3;
292 if (fd[entry_count-1].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
293 if (tmpLen > widest) widest = tmpLen;
296 fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATAW));
297 if (fd == NULL) {
298 FindClose (hff);
299 WINE_ERR("Out of memory\n");
300 errorlevel = 1;
301 return parms->next;
303 } while (FindNextFileW(hff, &fd[entry_count]) != 0);
304 FindClose (hff);
307 /* Work out the actual current directory name without a trailing \ */
308 lstrcpyW(real_path, parms->dirName);
309 real_path[lstrlenW(parms->dirName)-1] = 0x00;
311 /* Output the results */
312 if (!bare) {
313 if (level != 0 && (entry_count > 0)) WCMD_output_asis (newlineW);
314 if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
315 WCMD_output (L"Directory of %1\n\n", real_path);
316 done_header = TRUE;
320 /* Move to next parm */
321 parms = parms->next;
324 /* Handle case where everything is filtered out */
325 if (entry_count > 0) {
327 /* Sort the list of files */
328 qsort (fd, entry_count, sizeof(WIN32_FIND_DATAW), WCMD_dir_sort);
330 /* Work out the number of columns */
331 WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
332 if (wide || orderByCol) {
333 numCols = max(1, max_width / widest);
334 numRows = entry_count / numCols;
335 if (entry_count % numCols) numRows++;
336 } else {
337 numCols = 1;
338 numRows = entry_count;
340 WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
342 for (rows=0; rows<numRows; rows++) {
343 BOOL addNewLine = TRUE;
344 for (cols=0; cols<numCols; cols++) {
345 WCHAR username[24];
347 /* Work out the index of the entry being pointed to */
348 if (orderByCol) {
349 i = (cols * numRows) + rows;
350 if (i >= entry_count) continue;
351 } else {
352 i = (rows * numCols) + cols;
353 if (i >= entry_count) continue;
356 /* /L convers all names to lower case */
357 if (lower) {
358 WCHAR *p = fd[i].cFileName;
359 while ( (*p = tolower(*p)) ) ++p;
362 /* /Q gets file ownership information */
363 if (usernames) {
364 lstrcpyW (string, inputparms->dirName);
365 lstrcatW (string, fd[i].cFileName);
366 WCMD_getfileowner(string, username, ARRAY_SIZE(username));
369 if (dirTime == Written) {
370 FileTimeToLocalFileTime (&fd[i].ftLastWriteTime, &ft);
371 } else if (dirTime == Access) {
372 FileTimeToLocalFileTime (&fd[i].ftLastAccessTime, &ft);
373 } else {
374 FileTimeToLocalFileTime (&fd[i].ftCreationTime, &ft);
376 FileTimeToSystemTime (&ft, &st);
377 GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring, ARRAY_SIZE(datestring));
378 GetTimeFormatW(0, TIME_NOSECONDS, &st, NULL, timestring, ARRAY_SIZE(timestring));
380 if (wide) {
382 tmp_width = cur_width;
383 if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
384 WCMD_output (L"[%1]", fd[i].cFileName);
385 dir_count++;
386 tmp_width = tmp_width + lstrlenW(fd[i].cFileName) + 2;
387 } else {
388 WCMD_output (L"%1", fd[i].cFileName);
389 tmp_width = tmp_width + lstrlenW(fd[i].cFileName) ;
390 file_count++;
391 file_size.u.LowPart = fd[i].nFileSizeLow;
392 file_size.u.HighPart = fd[i].nFileSizeHigh;
393 byte_count.QuadPart += file_size.QuadPart;
395 cur_width = cur_width + widest;
397 if ((cur_width + widest) > max_width) {
398 cur_width = 0;
399 } else {
400 WCMD_output(L"%1!*s!", cur_width - tmp_width, nullW);
403 } else if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
404 dir_count++;
406 if (!bare) {
407 WCMD_output (L"%1!10s! %2!8s! <DIR> ", datestring, timestring);
408 if (shortname) WCMD_output (fmt2, fd[i].cAlternateFileName);
409 if (usernames) WCMD_output (fmt3, username);
410 WCMD_output(fmt4,fd[i].cFileName);
411 } else {
412 if (!((lstrcmpW(fd[i].cFileName, dotW) == 0) ||
413 (lstrcmpW(fd[i].cFileName, dotdotW) == 0))) {
414 WCMD_output (fmt5, recurse?inputparms->dirName:nullW, fd[i].cFileName);
415 } else {
416 addNewLine = FALSE;
420 else {
421 file_count++;
422 file_size.u.LowPart = fd[i].nFileSizeLow;
423 file_size.u.HighPart = fd[i].nFileSizeHigh;
424 byte_count.QuadPart += file_size.QuadPart;
425 if (!bare) {
426 WCMD_output (L"%1!10s! %2!8s! %3!10s! ", datestring, timestring,
427 WCMD_filesize64(file_size.QuadPart));
428 if (shortname) WCMD_output (fmt2, fd[i].cAlternateFileName);
429 if (usernames) WCMD_output (fmt3, username);
430 WCMD_output(fmt4,fd[i].cFileName);
431 } else {
432 WCMD_output (fmt5, recurse?inputparms->dirName:nullW, fd[i].cFileName);
436 if (addNewLine) WCMD_output_asis (newlineW);
437 cur_width = 0;
440 if (!bare) {
441 if (file_count == 1) {
442 WCMD_output (L" 1 file %1!25s! bytes\n", WCMD_filesize64 (byte_count.QuadPart));
444 else {
445 WCMD_output (L"%1!8d! files %2!24s! bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
448 byte_total = byte_total + byte_count.QuadPart;
449 file_total = file_total + file_count;
450 dir_total = dir_total + dir_count;
452 if (!bare && !recurse) {
453 if (dir_count == 1) {
454 WCMD_output (L"%1!8d! directory ", 1);
455 } else {
456 WCMD_output (L"%1!8d! directories", dir_count);
460 heap_free(fd);
462 /* When recursing, look in all subdirectories for matches */
463 if (recurse) {
464 DIRECTORY_STACK *dirStack = NULL;
465 DIRECTORY_STACK *lastEntry = NULL;
466 WIN32_FIND_DATAW finddata;
468 /* Build path to search */
469 lstrcpyW(string, inputparms->dirName);
470 lstrcatW(string, starW);
472 WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
473 hff = FindFirstFileW(string, &finddata);
474 if (hff != INVALID_HANDLE_VALUE) {
475 do {
476 if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
477 (lstrcmpW(finddata.cFileName, dotdotW) != 0) &&
478 (lstrcmpW(finddata.cFileName, dotW) != 0)) {
480 DIRECTORY_STACK *thisDir;
481 int dirsToCopy = concurrentDirs;
483 /* Loop creating list of subdirs for all concurrent entries */
484 parms = inputparms;
485 while (dirsToCopy > 0) {
486 dirsToCopy--;
488 /* Work out search parameter in sub dir */
489 lstrcpyW (string, inputparms->dirName);
490 lstrcatW (string, finddata.cFileName);
491 lstrcatW (string, slashW);
492 WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
494 /* Allocate memory, add to list */
495 thisDir = heap_xalloc(sizeof(DIRECTORY_STACK));
496 if (dirStack == NULL) dirStack = thisDir;
497 if (lastEntry != NULL) lastEntry->next = thisDir;
498 lastEntry = thisDir;
499 thisDir->next = NULL;
500 thisDir->dirName = heap_strdupW(string);
501 thisDir->fileName = heap_strdupW(parms->fileName);
502 parms = parms->next;
505 } while (FindNextFileW(hff, &finddata) != 0);
506 FindClose (hff);
508 while (dirStack != NULL) {
509 DIRECTORY_STACK *thisDir = dirStack;
510 dirStack = WCMD_list_directory (thisDir, 1);
511 while (thisDir != dirStack) {
512 DIRECTORY_STACK *tempDir = thisDir->next;
513 heap_free(thisDir->dirName);
514 heap_free(thisDir->fileName);
515 heap_free(thisDir);
516 thisDir = tempDir;
522 /* Handle case where everything is filtered out */
523 if ((file_total + dir_total == 0) && (level == 0)) {
524 SetLastError (ERROR_FILE_NOT_FOUND);
525 WCMD_print_error ();
526 errorlevel = 1;
529 return parms;
532 /*****************************************************************************
533 * WCMD_dir_trailer
535 * Print out the trailer for the supplied drive letter
537 static void WCMD_dir_trailer(WCHAR drive) {
538 ULARGE_INTEGER avail, total, freebytes;
539 DWORD status;
540 WCHAR driveName[] = L"c:\\";
542 driveName[0] = drive;
543 status = GetDiskFreeSpaceExW(driveName, &avail, &total, &freebytes);
544 WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName),
545 status, GetLastError());
547 if (errorlevel==0 && !bare) {
548 if (recurse) {
549 WCMD_output (L"\n Total files listed:\n%1!8d! files%2!25s! bytes\n", file_total, WCMD_filesize64 (byte_total));
550 WCMD_output (L"%1!8d! directories %2!18s! bytes free\n\n", dir_total, WCMD_filesize64 (freebytes.QuadPart));
551 } else {
552 WCMD_output (L" %1!18s! bytes free\n\n", WCMD_filesize64 (freebytes.QuadPart));
557 /*****************************************************************************
558 * WCMD_directory
560 * List a file directory.
564 void WCMD_directory (WCHAR *args)
566 WCHAR path[MAX_PATH], cwd[MAX_PATH];
567 DWORD status;
568 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
569 WCHAR *p;
570 WCHAR string[MAXSTRING];
571 int argno = 0;
572 WCHAR *argN = args;
573 WCHAR lastDrive;
574 BOOL trailerReqd = FALSE;
575 DIRECTORY_STACK *fullParms = NULL;
576 DIRECTORY_STACK *prevEntry = NULL;
577 DIRECTORY_STACK *thisEntry = NULL;
578 WCHAR drive[10];
579 WCHAR dir[MAX_PATH];
580 WCHAR fname[MAX_PATH];
581 WCHAR ext[MAX_PATH];
583 errorlevel = 0;
585 /* Prefill quals with (uppercased) DIRCMD env var */
586 if (GetEnvironmentVariableW(L"DIRCMD", string, ARRAY_SIZE(string))) {
587 p = string;
588 while ( (*p = toupper(*p)) ) ++p;
589 lstrcatW(string,quals);
590 lstrcpyW(quals, string);
593 byte_total = 0;
594 file_total = dir_total = 0;
596 /* Initialize all flags to their defaults as if no DIRCMD or quals */
597 paged_mode = FALSE;
598 recurse = FALSE;
599 wide = FALSE;
600 bare = FALSE;
601 lower = FALSE;
602 shortname = FALSE;
603 usernames = FALSE;
604 orderByCol = FALSE;
605 separator = TRUE;
606 dirTime = Written;
607 dirOrder = Name;
608 orderReverse = FALSE;
609 orderGroupDirs = FALSE;
610 orderGroupDirsReverse = FALSE;
611 showattrs = 0;
612 attrsbits = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
614 /* Handle args - Loop through so right most is the effective one */
615 /* Note: /- appears to be a negate rather than an off, eg. dir
616 /-W is wide, or dir /w /-w /-w is also wide */
617 p = quals;
618 while (*p && (*p=='/' || *p==' ')) {
619 BOOL negate = FALSE;
620 if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
622 if (*p=='-') {
623 negate = TRUE;
624 p++;
627 WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
628 switch (*p) {
629 case 'P': if (negate) paged_mode = !paged_mode;
630 else paged_mode = TRUE;
631 break;
632 case 'S': if (negate) recurse = !recurse;
633 else recurse = TRUE;
634 break;
635 case 'W': if (negate) wide = !wide;
636 else wide = TRUE;
637 break;
638 case 'B': if (negate) bare = !bare;
639 else bare = TRUE;
640 break;
641 case 'L': if (negate) lower = !lower;
642 else lower = TRUE;
643 break;
644 case 'X': if (negate) shortname = !shortname;
645 else shortname = TRUE;
646 break;
647 case 'Q': if (negate) usernames = !usernames;
648 else usernames = TRUE;
649 break;
650 case 'D': if (negate) orderByCol = !orderByCol;
651 else orderByCol = TRUE;
652 break;
653 case 'C': if (negate) separator = !separator;
654 else separator = TRUE;
655 break;
656 case 'T': p = p + 1;
657 if (*p==':') p++; /* Skip optional : */
659 if (*p == 'A') dirTime = Access;
660 else if (*p == 'C') dirTime = Creation;
661 else if (*p == 'W') dirTime = Written;
663 /* Support /T and /T: with no parms, default to written */
664 else if (*p == 0x00 || *p == '/') {
665 dirTime = Written;
666 p = p - 1; /* So when step on, move to '/' */
667 } else {
668 SetLastError(ERROR_INVALID_PARAMETER);
669 WCMD_print_error();
670 errorlevel = 1;
671 return;
673 break;
674 case 'O': p = p + 1;
675 if (*p==':') p++; /* Skip optional : */
676 while (*p && *p != '/') {
677 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
678 switch (*p) {
679 case 'N': dirOrder = Name; break;
680 case 'E': dirOrder = Extension; break;
681 case 'S': dirOrder = Size; break;
682 case 'D': dirOrder = Date; break;
683 case '-': if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
684 else orderReverse = TRUE;
685 break;
686 case 'G': orderGroupDirs = TRUE; break;
687 default:
688 SetLastError(ERROR_INVALID_PARAMETER);
689 WCMD_print_error();
690 errorlevel = 1;
691 return;
693 p++;
695 p = p - 1; /* So when step on, move to '/' */
696 break;
697 case 'A': p = p + 1;
698 showattrs = 0;
699 attrsbits = 0;
700 if (*p==':') p++; /* Skip optional : */
701 while (*p && *p != '/') {
702 BOOL anegate = FALSE;
703 ULONG mask;
705 /* Note /A: - options are 'offs' not toggles */
706 if (*p=='-') {
707 anegate = TRUE;
708 p++;
711 WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
712 switch (*p) {
713 case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
714 case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
715 case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
716 case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
717 case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
718 default:
719 SetLastError(ERROR_INVALID_PARAMETER);
720 WCMD_print_error();
721 errorlevel = 1;
722 return;
725 /* Keep running list of bits we care about */
726 attrsbits |= mask;
728 /* Mask shows what MUST be in the bits we care about */
729 if (anegate) showattrs = showattrs & ~mask;
730 else showattrs |= mask;
732 p++;
734 p = p - 1; /* So when step on, move to '/' */
735 WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs, attrsbits);
736 break;
737 default:
738 SetLastError(ERROR_INVALID_PARAMETER);
739 WCMD_print_error();
740 errorlevel = 1;
741 return;
743 p = p + 1;
746 /* Handle conflicting args and initialization */
747 if (bare || shortname) wide = FALSE;
748 if (bare) shortname = FALSE;
749 if (wide) usernames = FALSE;
750 if (orderByCol) wide = TRUE;
752 if (wide) {
753 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
754 max_width = consoleInfo.dwSize.X;
755 else
756 max_width = 80;
758 if (paged_mode) {
759 WCMD_enter_paged_mode(NULL);
762 argno = 0;
763 argN = args;
764 GetCurrentDirectoryW(MAX_PATH, cwd);
765 lstrcatW(cwd, slashW);
767 /* Loop through all args, calculating full effective directory */
768 fullParms = NULL;
769 prevEntry = NULL;
770 while (argN) {
771 WCHAR fullname[MAXSTRING];
772 WCHAR *thisArg = WCMD_parameter(args, argno++, &argN, FALSE, FALSE);
773 if (argN && argN[0] != '/') {
775 WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
776 if (thisArg[1] == ':' && thisArg[2] == '\\') {
777 lstrcpyW(fullname, thisArg);
778 } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
779 WCHAR envvar[4];
780 wsprintfW(envvar, L"=%c:", thisArg[0]);
781 if (!GetEnvironmentVariableW(envvar, fullname, MAX_PATH)) {
782 wsprintfW(fullname, L"%c:", thisArg[0]);
784 lstrcatW(fullname, slashW);
785 lstrcatW(fullname, &thisArg[2]);
786 } else if (thisArg[0] == '\\') {
787 memcpy(fullname, cwd, 2 * sizeof(WCHAR));
788 lstrcpyW(fullname+2, thisArg);
789 } else {
790 lstrcpyW(fullname, cwd);
791 lstrcatW(fullname, thisArg);
793 WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
795 status = GetFullPathNameW(fullname, ARRAY_SIZE(path), path, NULL);
798 * If the path supplied does not include a wildcard, and the endpoint of the
799 * path references a directory, we need to list the *contents* of that
800 * directory not the directory file itself.
802 if ((wcschr(path, '*') == NULL) && (wcschr(path, '%') == NULL)) {
803 status = GetFileAttributesW(path);
804 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
805 if (!ends_with_backslash( path )) lstrcatW( path, slashW );
806 lstrcatW (path, starW);
808 } else {
809 /* Special case wildcard search with no extension (ie parameters ending in '.') as
810 GetFullPathName strips off the additional '.' */
811 if (fullname[lstrlenW(fullname)-1] == '.') lstrcatW(path, dotW);
814 WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
815 thisEntry = heap_xalloc(sizeof(DIRECTORY_STACK));
816 if (fullParms == NULL) fullParms = thisEntry;
817 if (prevEntry != NULL) prevEntry->next = thisEntry;
818 prevEntry = thisEntry;
819 thisEntry->next = NULL;
821 /* Split into components */
822 _wsplitpath(path, drive, dir, fname, ext);
823 WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
824 wine_dbgstr_w(drive), wine_dbgstr_w(dir),
825 wine_dbgstr_w(fname), wine_dbgstr_w(ext));
827 thisEntry->dirName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(drive)+lstrlenW(dir)+1));
828 lstrcpyW(thisEntry->dirName, drive);
829 lstrcatW(thisEntry->dirName, dir);
831 thisEntry->fileName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(fname)+lstrlenW(ext)+1));
832 lstrcpyW(thisEntry->fileName, fname);
833 lstrcatW(thisEntry->fileName, ext);
838 /* If just 'dir' entered, a '*' parameter is assumed */
839 if (fullParms == NULL) {
840 WINE_TRACE("Inserting default '*'\n");
841 fullParms = heap_xalloc(sizeof(DIRECTORY_STACK));
842 fullParms->next = NULL;
843 fullParms->dirName = heap_strdupW(cwd);
844 fullParms->fileName = heap_strdupW(starW);
847 lastDrive = '?';
848 prevEntry = NULL;
849 thisEntry = fullParms;
850 trailerReqd = FALSE;
852 while (thisEntry != NULL) {
854 /* Output disk free (trailer) and volume information (header) if the drive
855 letter changes */
856 if (lastDrive != toupper(thisEntry->dirName[0])) {
858 /* Trailer Information */
859 if (lastDrive != '?') {
860 trailerReqd = FALSE;
861 WCMD_dir_trailer(prevEntry->dirName[0]);
864 lastDrive = toupper(thisEntry->dirName[0]);
866 if (!bare) {
867 WCHAR drive[3];
869 WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
870 memcpy(drive, thisEntry->dirName, 2 * sizeof(WCHAR));
871 drive[2] = 0x00;
872 status = WCMD_volume (0, drive);
873 trailerReqd = TRUE;
874 if (!status) {
875 errorlevel = 1;
876 goto exit;
879 } else {
880 if (!bare) WCMD_output_asis (L"\n\n");
883 /* Clear any errors from previous invocations, and process it */
884 errorlevel = 0;
885 prevEntry = thisEntry;
886 thisEntry = WCMD_list_directory (thisEntry, 0);
889 /* Trailer Information */
890 if (trailerReqd) {
891 WCMD_dir_trailer(prevEntry->dirName[0]);
894 exit:
895 if (paged_mode) WCMD_leave_paged_mode();
897 /* Free storage allocated for parms */
898 while (fullParms != NULL) {
899 prevEntry = fullParms;
900 fullParms = prevEntry->next;
901 heap_free(prevEntry->dirName);
902 heap_free(prevEntry->fileName);
903 heap_free(prevEntry);