directory.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * CMD - Wine-compatible command line interface - Directory functions.
  3. *
  4. * Copyright (C) 1999 D A Pickles
  5. * Copyright (C) 2007 J Edmeades
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #define WIN32_LEAN_AND_MEAN
  22. #include "wcmd.h"
  23. #include "wine/debug.h"
  24. WINE_DEFAULT_DEBUG_CHANNEL(cmd);
  25. typedef enum _DISPLAYTIME
  26. {
  27. Creation = 0,
  28. Access,
  29. Written
  30. } DISPLAYTIME;
  31. typedef enum _DISPLAYORDER
  32. {
  33. Name = 0,
  34. Extension,
  35. Size,
  36. Date
  37. } DISPLAYORDER;
  38. static int file_total, dir_total, max_width;
  39. static ULONGLONG byte_total;
  40. static DISPLAYTIME dirTime;
  41. static DISPLAYORDER dirOrder;
  42. static BOOL orderReverse, orderGroupDirs, orderGroupDirsReverse, orderByCol;
  43. static BOOL paged_mode, recurse, wide, bare, lower, shortname, usernames, separator;
  44. static ULONG showattrs, attrsbits;
  45. /*****************************************************************************
  46. * WCMD_strrev
  47. *
  48. * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
  49. */
  50. static WCHAR * WCMD_strrev (WCHAR *buff) {
  51. int r, i;
  52. WCHAR b;
  53. r = lstrlenW (buff);
  54. for (i=0; i<r/2; i++) {
  55. b = buff[i];
  56. buff[i] = buff[r-i-1];
  57. buff[r-i-1] = b;
  58. }
  59. return (buff);
  60. }
  61. /*****************************************************************************
  62. * WCMD_filesize64
  63. *
  64. * Convert a 64-bit number into a WCHARacter string, with commas every three digits.
  65. * Result is returned in a static string overwritten with each call.
  66. * FIXME: There must be a better algorithm!
  67. */
  68. static WCHAR * WCMD_filesize64 (ULONGLONG n) {
  69. ULONGLONG q;
  70. unsigned int r, i;
  71. WCHAR *p;
  72. static WCHAR buff[32];
  73. p = buff;
  74. i = -3;
  75. do {
  76. if (separator && ((++i)%3 == 1)) *p++ = ',';
  77. q = n / 10;
  78. r = n - (q * 10);
  79. *p++ = r + '0';
  80. *p = '\0';
  81. n = q;
  82. } while (n != 0);
  83. WCMD_strrev (buff);
  84. return buff;
  85. }
  86. /*****************************************************************************
  87. * WCMD_dir_sort
  88. *
  89. * Sort based on the /O options supplied on the command line
  90. */
  91. static int __cdecl WCMD_dir_sort (const void *a, const void *b)
  92. {
  93. const WIN32_FIND_DATAW *filea = (const WIN32_FIND_DATAW *)a;
  94. const WIN32_FIND_DATAW *fileb = (const WIN32_FIND_DATAW *)b;
  95. int result = 0;
  96. /* If /OG or /O-G supplied, dirs go at the top or bottom, ignoring the
  97. requested sort order for the directory components */
  98. if (orderGroupDirs &&
  99. ((filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
  100. (fileb->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
  101. {
  102. BOOL aDir = filea->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
  103. if (aDir) result = -1;
  104. else result = 1;
  105. if (orderGroupDirsReverse) result = -result;
  106. return result;
  107. /* Order by Name: */
  108. } else if (dirOrder == Name) {
  109. result = lstrcmpiW(filea->cFileName, fileb->cFileName);
  110. /* Order by Size: */
  111. } else if (dirOrder == Size) {
  112. ULONG64 sizea = (((ULONG64)filea->nFileSizeHigh) << 32) + filea->nFileSizeLow;
  113. ULONG64 sizeb = (((ULONG64)fileb->nFileSizeHigh) << 32) + fileb->nFileSizeLow;
  114. if( sizea < sizeb ) result = -1;
  115. else if( sizea == sizeb ) result = 0;
  116. else result = 1;
  117. /* Order by Date: (Takes into account which date (/T option) */
  118. } else if (dirOrder == Date) {
  119. const FILETIME *ft;
  120. ULONG64 timea, timeb;
  121. if (dirTime == Written) {
  122. ft = &filea->ftLastWriteTime;
  123. timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
  124. ft = &fileb->ftLastWriteTime;
  125. timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
  126. } else if (dirTime == Access) {
  127. ft = &filea->ftLastAccessTime;
  128. timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
  129. ft = &fileb->ftLastAccessTime;
  130. timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
  131. } else {
  132. ft = &filea->ftCreationTime;
  133. timea = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
  134. ft = &fileb->ftCreationTime;
  135. timeb = (((ULONG64)ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
  136. }
  137. if( timea < timeb ) result = -1;
  138. else if( timea == timeb ) result = 0;
  139. else result = 1;
  140. /* Order by Extension: (Takes into account which date (/T option) */
  141. } else if (dirOrder == Extension) {
  142. WCHAR drive[10];
  143. WCHAR dir[MAX_PATH];
  144. WCHAR fname[MAX_PATH];
  145. WCHAR extA[MAX_PATH];
  146. WCHAR extB[MAX_PATH];
  147. /* Split into components */
  148. _wsplitpath(filea->cFileName, drive, dir, fname, extA);
  149. _wsplitpath(fileb->cFileName, drive, dir, fname, extB);
  150. result = lstrcmpiW(extA, extB);
  151. }
  152. if (orderReverse) result = -result;
  153. return result;
  154. }
  155. /*****************************************************************************
  156. * WCMD_getfileowner
  157. *
  158. * Reverse a WCHARacter string in-place (strrev() is not available under unixen :-( ).
  159. */
  160. static void WCMD_getfileowner(WCHAR *filename, WCHAR *owner, int ownerlen) {
  161. ULONG sizeNeeded = 0;
  162. DWORD rc;
  163. WCHAR name[MAXSTRING];
  164. WCHAR domain[MAXSTRING];
  165. /* In case of error, return empty string */
  166. *owner = 0x00;
  167. /* Find out how much space we need for the owner security descriptor */
  168. GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, 0, 0, &sizeNeeded);
  169. rc = GetLastError();
  170. if(rc == ERROR_INSUFFICIENT_BUFFER && sizeNeeded > 0) {
  171. LPBYTE secBuffer;
  172. PSID pSID = NULL;
  173. BOOL defaulted = FALSE;
  174. ULONG nameLen = MAXSTRING;
  175. ULONG domainLen = MAXSTRING;
  176. SID_NAME_USE nameuse;
  177. secBuffer = heap_xalloc(sizeNeeded * sizeof(BYTE));
  178. /* Get the owners security descriptor */
  179. if(!GetFileSecurityW(filename, OWNER_SECURITY_INFORMATION, secBuffer,
  180. sizeNeeded, &sizeNeeded)) {
  181. heap_free(secBuffer);
  182. return;
  183. }
  184. /* Get the SID from the SD */
  185. if(!GetSecurityDescriptorOwner(secBuffer, &pSID, &defaulted)) {
  186. heap_free(secBuffer);
  187. return;
  188. }
  189. /* Convert to a username */
  190. if (LookupAccountSidW(NULL, pSID, name, &nameLen, domain, &domainLen, &nameuse)) {
  191. swprintf(owner, ownerlen, L"%s%c%s", domain, '\\', name);
  192. }
  193. heap_free(secBuffer);
  194. }
  195. return;
  196. }
  197. /*****************************************************************************
  198. * WCMD_list_directory
  199. *
  200. * List a single file directory. This function (and those below it) can be called
  201. * recursively when the /S switch is used.
  202. *
  203. * FIXME: Assumes 24-line display for the /P qualifier.
  204. */
  205. static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int level) {
  206. WCHAR string[1024], datestring[32], timestring[32];
  207. WCHAR real_path[MAX_PATH];
  208. WIN32_FIND_DATAW *fd;
  209. FILETIME ft;
  210. SYSTEMTIME st;
  211. HANDLE hff;
  212. int dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
  213. int numCols, numRows;
  214. int rows, cols;
  215. ULARGE_INTEGER byte_count, file_size;
  216. DIRECTORY_STACK *parms;
  217. int concurrentDirs = 0;
  218. BOOL done_header = FALSE;
  219. dir_count = 0;
  220. file_count = 0;
  221. entry_count = 0;
  222. byte_count.QuadPart = 0;
  223. widest = 0;
  224. cur_width = 0;
  225. /* Loop merging all the files from consecutive parms which relate to the
  226. same directory. Note issuing a directory header with no contents
  227. mirrors what windows does */
  228. parms = inputparms;
  229. fd = heap_xalloc(sizeof(WIN32_FIND_DATAW));
  230. while (parms && lstrcmpW(inputparms->dirName, parms->dirName) == 0) {
  231. concurrentDirs++;
  232. /* Work out the full path + filename */
  233. lstrcpyW(real_path, parms->dirName);
  234. lstrcatW(real_path, parms->fileName);
  235. /* Load all files into an in memory structure */
  236. WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
  237. hff = FindFirstFileW(real_path, &fd[entry_count]);
  238. if (hff != INVALID_HANDLE_VALUE) {
  239. do {
  240. /* Skip any which are filtered out by attribute */
  241. if ((fd[entry_count].dwFileAttributes & attrsbits) != showattrs) continue;
  242. entry_count++;
  243. /* Keep running track of longest filename for wide output */
  244. if (wide || orderByCol) {
  245. int tmpLen = lstrlenW(fd[entry_count-1].cFileName) + 3;
  246. if (fd[entry_count-1].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
  247. if (tmpLen > widest) widest = tmpLen;
  248. }
  249. fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATAW));
  250. if (fd == NULL) {
  251. FindClose (hff);
  252. WINE_ERR("Out of memory\n");
  253. errorlevel = 1;
  254. return parms->next;
  255. }
  256. } while (FindNextFileW(hff, &fd[entry_count]) != 0);
  257. FindClose (hff);
  258. }
  259. /* Work out the actual current directory name without a trailing \ */
  260. lstrcpyW(real_path, parms->dirName);
  261. real_path[lstrlenW(parms->dirName)-1] = 0x00;
  262. /* Output the results */
  263. if (!bare) {
  264. if (level != 0 && (entry_count > 0)) WCMD_output_asis(L"\r\n");
  265. if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
  266. WCMD_output (L"Directory of %1\n\n", real_path);
  267. done_header = TRUE;
  268. }
  269. }
  270. /* Move to next parm */
  271. parms = parms->next;
  272. }
  273. /* Handle case where everything is filtered out */
  274. if (entry_count > 0) {
  275. /* Sort the list of files */
  276. qsort (fd, entry_count, sizeof(WIN32_FIND_DATAW), WCMD_dir_sort);
  277. /* Work out the number of columns */
  278. WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
  279. if (wide || orderByCol) {
  280. numCols = max(1, max_width / widest);
  281. numRows = entry_count / numCols;
  282. if (entry_count % numCols) numRows++;
  283. } else {
  284. numCols = 1;
  285. numRows = entry_count;
  286. }
  287. WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
  288. for (rows=0; rows<numRows; rows++) {
  289. BOOL addNewLine = TRUE;
  290. for (cols=0; cols<numCols; cols++) {
  291. WCHAR username[24];
  292. /* Work out the index of the entry being pointed to */
  293. if (orderByCol) {
  294. i = (cols * numRows) + rows;
  295. if (i >= entry_count) continue;
  296. } else {
  297. i = (rows * numCols) + cols;
  298. if (i >= entry_count) continue;
  299. }
  300. /* /L convers all names to lower case */
  301. if (lower) {
  302. WCHAR *p = fd[i].cFileName;
  303. while ( (*p = tolower(*p)) ) ++p;
  304. }
  305. /* /Q gets file ownership information */
  306. if (usernames) {
  307. lstrcpyW (string, inputparms->dirName);
  308. lstrcatW (string, fd[i].cFileName);
  309. WCMD_getfileowner(string, username, ARRAY_SIZE(username));
  310. }
  311. if (dirTime == Written) {
  312. FileTimeToLocalFileTime (&fd[i].ftLastWriteTime, &ft);
  313. } else if (dirTime == Access) {
  314. FileTimeToLocalFileTime (&fd[i].ftLastAccessTime, &ft);
  315. } else {
  316. FileTimeToLocalFileTime (&fd[i].ftCreationTime, &ft);
  317. }
  318. FileTimeToSystemTime (&ft, &st);
  319. GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring, ARRAY_SIZE(datestring));
  320. GetTimeFormatW(0, TIME_NOSECONDS, &st, NULL, timestring, ARRAY_SIZE(timestring));
  321. if (wide) {
  322. tmp_width = cur_width;
  323. if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  324. WCMD_output (L"[%1]", fd[i].cFileName);
  325. dir_count++;
  326. tmp_width = tmp_width + lstrlenW(fd[i].cFileName) + 2;
  327. } else {
  328. WCMD_output (L"%1", fd[i].cFileName);
  329. tmp_width = tmp_width + lstrlenW(fd[i].cFileName) ;
  330. file_count++;
  331. file_size.u.LowPart = fd[i].nFileSizeLow;
  332. file_size.u.HighPart = fd[i].nFileSizeHigh;
  333. byte_count.QuadPart += file_size.QuadPart;
  334. }
  335. cur_width = cur_width + widest;
  336. if ((cur_width + widest) > max_width) {
  337. cur_width = 0;
  338. } else {
  339. WCMD_output(L"%1!*s!", cur_width - tmp_width, L"");
  340. }
  341. } else if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  342. dir_count++;
  343. if (!bare) {
  344. WCMD_output (L"%1!10s! %2!8s! <DIR> ", datestring, timestring);
  345. if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName);
  346. if (usernames) WCMD_output(L"%1!-23s!", username);
  347. WCMD_output(L"%1",fd[i].cFileName);
  348. } else {
  349. if (!((lstrcmpW(fd[i].cFileName, L".") == 0) ||
  350. (lstrcmpW(fd[i].cFileName, L"..") == 0))) {
  351. WCMD_output(L"%1%2", recurse ? inputparms->dirName : L"", fd[i].cFileName);
  352. } else {
  353. addNewLine = FALSE;
  354. }
  355. }
  356. }
  357. else {
  358. file_count++;
  359. file_size.u.LowPart = fd[i].nFileSizeLow;
  360. file_size.u.HighPart = fd[i].nFileSizeHigh;
  361. byte_count.QuadPart += file_size.QuadPart;
  362. if (!bare) {
  363. WCMD_output (L"%1!10s! %2!8s! %3!10s! ", datestring, timestring,
  364. WCMD_filesize64(file_size.QuadPart));
  365. if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName);
  366. if (usernames) WCMD_output(L"%1!-23s!", username);
  367. WCMD_output(L"%1",fd[i].cFileName);
  368. } else {
  369. WCMD_output(L"%1%2", recurse ? inputparms->dirName : L"", fd[i].cFileName);
  370. }
  371. }
  372. }
  373. if (addNewLine) WCMD_output_asis(L"\r\n");
  374. cur_width = 0;
  375. }
  376. if (!bare) {
  377. if (file_count == 1) {
  378. WCMD_output (L" 1 file %1!25s! bytes\n", WCMD_filesize64 (byte_count.QuadPart));
  379. }
  380. else {
  381. WCMD_output (L"%1!8d! files %2!24s! bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
  382. }
  383. }
  384. byte_total = byte_total + byte_count.QuadPart;
  385. file_total = file_total + file_count;
  386. dir_total = dir_total + dir_count;
  387. if (!bare && !recurse) {
  388. if (dir_count == 1) {
  389. WCMD_output (L"%1!8d! directory ", 1);
  390. } else {
  391. WCMD_output (L"%1!8d! directories", dir_count);
  392. }
  393. }
  394. }
  395. heap_free(fd);
  396. /* When recursing, look in all subdirectories for matches */
  397. if (recurse) {
  398. DIRECTORY_STACK *dirStack = NULL;
  399. DIRECTORY_STACK *lastEntry = NULL;
  400. WIN32_FIND_DATAW finddata;
  401. /* Build path to search */
  402. lstrcpyW(string, inputparms->dirName);
  403. lstrcatW(string, L"*");
  404. WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
  405. hff = FindFirstFileW(string, &finddata);
  406. if (hff != INVALID_HANDLE_VALUE) {
  407. do {
  408. if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
  409. (lstrcmpW(finddata.cFileName, L"..") != 0) &&
  410. (lstrcmpW(finddata.cFileName, L".") != 0)) {
  411. DIRECTORY_STACK *thisDir;
  412. int dirsToCopy = concurrentDirs;
  413. /* Loop creating list of subdirs for all concurrent entries */
  414. parms = inputparms;
  415. while (dirsToCopy > 0) {
  416. dirsToCopy--;
  417. /* Work out search parameter in sub dir */
  418. lstrcpyW (string, inputparms->dirName);
  419. lstrcatW (string, finddata.cFileName);
  420. lstrcatW(string, L"\\");
  421. WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
  422. /* Allocate memory, add to list */
  423. thisDir = heap_xalloc(sizeof(DIRECTORY_STACK));
  424. if (dirStack == NULL) dirStack = thisDir;
  425. if (lastEntry != NULL) lastEntry->next = thisDir;
  426. lastEntry = thisDir;
  427. thisDir->next = NULL;
  428. thisDir->dirName = heap_strdupW(string);
  429. thisDir->fileName = heap_strdupW(parms->fileName);
  430. parms = parms->next;
  431. }
  432. }
  433. } while (FindNextFileW(hff, &finddata) != 0);
  434. FindClose (hff);
  435. while (dirStack != NULL) {
  436. DIRECTORY_STACK *thisDir = dirStack;
  437. dirStack = WCMD_list_directory (thisDir, 1);
  438. while (thisDir != dirStack) {
  439. DIRECTORY_STACK *tempDir = thisDir->next;
  440. heap_free(thisDir->dirName);
  441. heap_free(thisDir->fileName);
  442. heap_free(thisDir);
  443. thisDir = tempDir;
  444. }
  445. }
  446. }
  447. }
  448. /* Handle case where everything is filtered out */
  449. if ((file_total + dir_total == 0) && (level == 0)) {
  450. SetLastError (ERROR_FILE_NOT_FOUND);
  451. WCMD_print_error ();
  452. errorlevel = 1;
  453. }
  454. return parms;
  455. }
  456. /*****************************************************************************
  457. * WCMD_dir_trailer
  458. *
  459. * Print out the trailer for the supplied drive letter
  460. */
  461. static void WCMD_dir_trailer(WCHAR drive) {
  462. ULARGE_INTEGER avail, total, freebytes;
  463. DWORD status;
  464. WCHAR driveName[] = L"c:\\";
  465. driveName[0] = drive;
  466. status = GetDiskFreeSpaceExW(driveName, &avail, &total, &freebytes);
  467. WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName),
  468. status, GetLastError());
  469. if (errorlevel==0 && !bare) {
  470. if (recurse) {
  471. WCMD_output (L"\n Total files listed:\n%1!8d! files%2!25s! bytes\n", file_total, WCMD_filesize64 (byte_total));
  472. WCMD_output (L"%1!8d! directories %2!18s! bytes free\n\n", dir_total, WCMD_filesize64 (freebytes.QuadPart));
  473. } else {
  474. WCMD_output (L" %1!18s! bytes free\n\n", WCMD_filesize64 (freebytes.QuadPart));
  475. }
  476. }
  477. }
  478. /*****************************************************************************
  479. * WCMD_directory
  480. *
  481. * List a file directory.
  482. *
  483. */
  484. void WCMD_directory (WCHAR *args)
  485. {
  486. WCHAR path[MAX_PATH], cwd[MAX_PATH];
  487. DWORD status;
  488. CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
  489. WCHAR *p;
  490. WCHAR string[MAXSTRING];
  491. int argno = 0;
  492. WCHAR *argN = args;
  493. WCHAR lastDrive;
  494. BOOL trailerReqd = FALSE;
  495. DIRECTORY_STACK *fullParms = NULL;
  496. DIRECTORY_STACK *prevEntry = NULL;
  497. DIRECTORY_STACK *thisEntry = NULL;
  498. WCHAR drive[10];
  499. WCHAR dir[MAX_PATH];
  500. WCHAR fname[MAX_PATH];
  501. WCHAR ext[MAX_PATH];
  502. errorlevel = 0;
  503. /* Prefill quals with (uppercased) DIRCMD env var */
  504. if (GetEnvironmentVariableW(L"DIRCMD", string, ARRAY_SIZE(string))) {
  505. p = string;
  506. while ( (*p = toupper(*p)) ) ++p;
  507. lstrcatW(string,quals);
  508. lstrcpyW(quals, string);
  509. }
  510. byte_total = 0;
  511. file_total = dir_total = 0;
  512. /* Initialize all flags to their defaults as if no DIRCMD or quals */
  513. paged_mode = FALSE;
  514. recurse = FALSE;
  515. wide = FALSE;
  516. bare = FALSE;
  517. lower = FALSE;
  518. shortname = FALSE;
  519. usernames = FALSE;
  520. orderByCol = FALSE;
  521. separator = TRUE;
  522. dirTime = Written;
  523. dirOrder = Name;
  524. orderReverse = FALSE;
  525. orderGroupDirs = FALSE;
  526. orderGroupDirsReverse = FALSE;
  527. showattrs = 0;
  528. attrsbits = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
  529. /* Handle args - Loop through so right most is the effective one */
  530. /* Note: /- appears to be a negate rather than an off, eg. dir
  531. /-W is wide, or dir /w /-w /-w is also wide */
  532. p = quals;
  533. while (*p && (*p=='/' || *p==' ')) {
  534. BOOL negate = FALSE;
  535. if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
  536. if (*p=='-') {
  537. negate = TRUE;
  538. p++;
  539. }
  540. WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
  541. switch (*p) {
  542. case 'P': if (negate) paged_mode = !paged_mode;
  543. else paged_mode = TRUE;
  544. break;
  545. case 'S': if (negate) recurse = !recurse;
  546. else recurse = TRUE;
  547. break;
  548. case 'W': if (negate) wide = !wide;
  549. else wide = TRUE;
  550. break;
  551. case 'B': if (negate) bare = !bare;
  552. else bare = TRUE;
  553. break;
  554. case 'L': if (negate) lower = !lower;
  555. else lower = TRUE;
  556. break;
  557. case 'X': if (negate) shortname = !shortname;
  558. else shortname = TRUE;
  559. break;
  560. case 'Q': if (negate) usernames = !usernames;
  561. else usernames = TRUE;
  562. break;
  563. case 'D': if (negate) orderByCol = !orderByCol;
  564. else orderByCol = TRUE;
  565. break;
  566. case 'C': if (negate) separator = !separator;
  567. else separator = TRUE;
  568. break;
  569. case 'T': p = p + 1;
  570. if (*p==':') p++; /* Skip optional : */
  571. if (*p == 'A') dirTime = Access;
  572. else if (*p == 'C') dirTime = Creation;
  573. else if (*p == 'W') dirTime = Written;
  574. /* Support /T and /T: with no parms, default to written */
  575. else if (*p == 0x00 || *p == '/') {
  576. dirTime = Written;
  577. p = p - 1; /* So when step on, move to '/' */
  578. } else {
  579. SetLastError(ERROR_INVALID_PARAMETER);
  580. WCMD_print_error();
  581. errorlevel = 1;
  582. return;
  583. }
  584. break;
  585. case 'O': p = p + 1;
  586. if (*p==':') p++; /* Skip optional : */
  587. while (*p && *p != '/') {
  588. WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
  589. switch (*p) {
  590. case 'N': dirOrder = Name; break;
  591. case 'E': dirOrder = Extension; break;
  592. case 'S': dirOrder = Size; break;
  593. case 'D': dirOrder = Date; break;
  594. case '-': if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
  595. else orderReverse = TRUE;
  596. break;
  597. case 'G': orderGroupDirs = TRUE; break;
  598. default:
  599. SetLastError(ERROR_INVALID_PARAMETER);
  600. WCMD_print_error();
  601. errorlevel = 1;
  602. return;
  603. }
  604. p++;
  605. }
  606. p = p - 1; /* So when step on, move to '/' */
  607. break;
  608. case 'A': p = p + 1;
  609. showattrs = 0;
  610. attrsbits = 0;
  611. if (*p==':') p++; /* Skip optional : */
  612. while (*p && *p != '/') {
  613. BOOL anegate = FALSE;
  614. ULONG mask;
  615. /* Note /A: - options are 'offs' not toggles */
  616. if (*p=='-') {
  617. anegate = TRUE;
  618. p++;
  619. }
  620. WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
  621. switch (*p) {
  622. case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
  623. case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
  624. case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
  625. case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
  626. case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
  627. default:
  628. SetLastError(ERROR_INVALID_PARAMETER);
  629. WCMD_print_error();
  630. errorlevel = 1;
  631. return;
  632. }
  633. /* Keep running list of bits we care about */
  634. attrsbits |= mask;
  635. /* Mask shows what MUST be in the bits we care about */
  636. if (anegate) showattrs = showattrs & ~mask;
  637. else showattrs |= mask;
  638. p++;
  639. }
  640. p = p - 1; /* So when step on, move to '/' */
  641. WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs, attrsbits);
  642. break;
  643. default:
  644. SetLastError(ERROR_INVALID_PARAMETER);
  645. WCMD_print_error();
  646. errorlevel = 1;
  647. return;
  648. }
  649. p = p + 1;
  650. }
  651. /* Handle conflicting args and initialization */
  652. if (bare || shortname) wide = FALSE;
  653. if (bare) shortname = FALSE;
  654. if (wide) usernames = FALSE;
  655. if (orderByCol) wide = TRUE;
  656. if (wide) {
  657. if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
  658. max_width = consoleInfo.dwSize.X;
  659. else
  660. max_width = 80;
  661. }
  662. if (paged_mode) {
  663. WCMD_enter_paged_mode(NULL);
  664. }
  665. argno = 0;
  666. argN = args;
  667. GetCurrentDirectoryW(MAX_PATH, cwd);
  668. lstrcatW(cwd, L"\\");
  669. /* Loop through all args, calculating full effective directory */
  670. fullParms = NULL;
  671. prevEntry = NULL;
  672. while (argN) {
  673. WCHAR fullname[MAXSTRING];
  674. WCHAR *thisArg = WCMD_parameter(args, argno++, &argN, FALSE, FALSE);
  675. if (argN && argN[0] != '/') {
  676. WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
  677. if (thisArg[1] == ':' && thisArg[2] == '\\') {
  678. lstrcpyW(fullname, thisArg);
  679. } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
  680. WCHAR envvar[4];
  681. wsprintfW(envvar, L"=%c:", thisArg[0]);
  682. if (!GetEnvironmentVariableW(envvar, fullname, MAX_PATH)) {
  683. wsprintfW(fullname, L"%c:", thisArg[0]);
  684. }
  685. lstrcatW(fullname, L"\\");
  686. lstrcatW(fullname, &thisArg[2]);
  687. } else if (thisArg[0] == '\\') {
  688. memcpy(fullname, cwd, 2 * sizeof(WCHAR));
  689. lstrcpyW(fullname+2, thisArg);
  690. } else {
  691. lstrcpyW(fullname, cwd);
  692. lstrcatW(fullname, thisArg);
  693. }
  694. WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
  695. status = GetFullPathNameW(fullname, ARRAY_SIZE(path), path, NULL);
  696. /*
  697. * If the path supplied does not include a wildcard, and the endpoint of the
  698. * path references a directory, we need to list the *contents* of that
  699. * directory not the directory file itself.
  700. */
  701. if ((wcschr(path, '*') == NULL) && (wcschr(path, '%') == NULL)) {
  702. status = GetFileAttributesW(path);
  703. if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
  704. if (!ends_with_backslash(path)) lstrcatW(path, L"\\");
  705. lstrcatW(path, L"*");
  706. }
  707. } else {
  708. /* Special case wildcard search with no extension (ie parameters ending in '.') as
  709. GetFullPathName strips off the additional '.' */
  710. if (fullname[lstrlenW(fullname)-1] == '.') lstrcatW(path, L".");
  711. }
  712. WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
  713. thisEntry = heap_xalloc(sizeof(DIRECTORY_STACK));
  714. if (fullParms == NULL) fullParms = thisEntry;
  715. if (prevEntry != NULL) prevEntry->next = thisEntry;
  716. prevEntry = thisEntry;
  717. thisEntry->next = NULL;
  718. /* Split into components */
  719. _wsplitpath(path, drive, dir, fname, ext);
  720. WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
  721. wine_dbgstr_w(drive), wine_dbgstr_w(dir),
  722. wine_dbgstr_w(fname), wine_dbgstr_w(ext));
  723. thisEntry->dirName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(drive)+lstrlenW(dir)+1));
  724. lstrcpyW(thisEntry->dirName, drive);
  725. lstrcatW(thisEntry->dirName, dir);
  726. thisEntry->fileName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(fname)+lstrlenW(ext)+1));
  727. lstrcpyW(thisEntry->fileName, fname);
  728. lstrcatW(thisEntry->fileName, ext);
  729. }
  730. }
  731. /* If just 'dir' entered, a '*' parameter is assumed */
  732. if (fullParms == NULL) {
  733. WINE_TRACE("Inserting default '*'\n");
  734. fullParms = heap_xalloc(sizeof(DIRECTORY_STACK));
  735. fullParms->next = NULL;
  736. fullParms->dirName = heap_strdupW(cwd);
  737. fullParms->fileName = heap_strdupW(L"*");
  738. }
  739. lastDrive = '?';
  740. prevEntry = NULL;
  741. thisEntry = fullParms;
  742. trailerReqd = FALSE;
  743. while (thisEntry != NULL) {
  744. /* Output disk free (trailer) and volume information (header) if the drive
  745. letter changes */
  746. if (lastDrive != toupper(thisEntry->dirName[0])) {
  747. /* Trailer Information */
  748. if (lastDrive != '?') {
  749. trailerReqd = FALSE;
  750. WCMD_dir_trailer(prevEntry->dirName[0]);
  751. }
  752. lastDrive = toupper(thisEntry->dirName[0]);
  753. if (!bare) {
  754. WCHAR drive[3];
  755. WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
  756. memcpy(drive, thisEntry->dirName, 2 * sizeof(WCHAR));
  757. drive[2] = 0x00;
  758. status = WCMD_volume (0, drive);
  759. trailerReqd = TRUE;
  760. if (!status) {
  761. errorlevel = 1;
  762. goto exit;
  763. }
  764. }
  765. } else {
  766. if (!bare) WCMD_output_asis (L"\n\n");
  767. }
  768. /* Clear any errors from previous invocations, and process it */
  769. errorlevel = 0;
  770. prevEntry = thisEntry;
  771. thisEntry = WCMD_list_directory (thisEntry, 0);
  772. }
  773. /* Trailer Information */
  774. if (trailerReqd) {
  775. WCMD_dir_trailer(prevEntry->dirName[0]);
  776. }
  777. exit:
  778. if (paged_mode) WCMD_leave_paged_mode();
  779. /* Free storage allocated for parms */
  780. while (fullParms != NULL) {
  781. prevEntry = fullParms;
  782. fullParms = prevEntry->next;
  783. heap_free(prevEntry->dirName);
  784. heap_free(prevEntry->fileName);
  785. heap_free(prevEntry);
  786. }
  787. }