directory.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. WCMD_splitpath(filea->cFileName, drive, dir, fname, extA);
  149. WCMD_splitpath(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. static const WCHAR fmt2[] = {'%','1','!','-','1','3','s','!','\0'};
  220. static const WCHAR fmt3[] = {'%','1','!','-','2','3','s','!','\0'};
  221. static const WCHAR fmt4[] = {'%','1','\0'};
  222. static const WCHAR fmt5[] = {'%','1','%','2','\0'};
  223. dir_count = 0;
  224. file_count = 0;
  225. entry_count = 0;
  226. byte_count.QuadPart = 0;
  227. widest = 0;
  228. cur_width = 0;
  229. /* Loop merging all the files from consecutive parms which relate to the
  230. same directory. Note issuing a directory header with no contents
  231. mirrors what windows does */
  232. parms = inputparms;
  233. fd = heap_xalloc(sizeof(WIN32_FIND_DATAW));
  234. while (parms && lstrcmpW(inputparms->dirName, parms->dirName) == 0) {
  235. concurrentDirs++;
  236. /* Work out the full path + filename */
  237. lstrcpyW(real_path, parms->dirName);
  238. lstrcatW(real_path, parms->fileName);
  239. /* Load all files into an in memory structure */
  240. WINE_TRACE("Looking for matches to '%s'\n", wine_dbgstr_w(real_path));
  241. hff = FindFirstFileW(real_path, &fd[entry_count]);
  242. if (hff != INVALID_HANDLE_VALUE) {
  243. do {
  244. /* Skip any which are filtered out by attribute */
  245. if ((fd[entry_count].dwFileAttributes & attrsbits) != showattrs) continue;
  246. entry_count++;
  247. /* Keep running track of longest filename for wide output */
  248. if (wide || orderByCol) {
  249. int tmpLen = lstrlenW(fd[entry_count-1].cFileName) + 3;
  250. if (fd[entry_count-1].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
  251. if (tmpLen > widest) widest = tmpLen;
  252. }
  253. fd = HeapReAlloc(GetProcessHeap(),0,fd,(entry_count+1)*sizeof(WIN32_FIND_DATAW));
  254. if (fd == NULL) {
  255. FindClose (hff);
  256. WINE_ERR("Out of memory\n");
  257. errorlevel = 1;
  258. return parms->next;
  259. }
  260. } while (FindNextFileW(hff, &fd[entry_count]) != 0);
  261. FindClose (hff);
  262. }
  263. /* Work out the actual current directory name without a trailing \ */
  264. lstrcpyW(real_path, parms->dirName);
  265. real_path[lstrlenW(parms->dirName)-1] = 0x00;
  266. /* Output the results */
  267. if (!bare) {
  268. if (level != 0 && (entry_count > 0)) WCMD_output_asis (newlineW);
  269. if (!recurse || ((entry_count > 0) && done_header==FALSE)) {
  270. WCMD_output (L"Directory of %1\n\n", real_path);
  271. done_header = TRUE;
  272. }
  273. }
  274. /* Move to next parm */
  275. parms = parms->next;
  276. }
  277. /* Handle case where everything is filtered out */
  278. if (entry_count > 0) {
  279. /* Sort the list of files */
  280. qsort (fd, entry_count, sizeof(WIN32_FIND_DATAW), WCMD_dir_sort);
  281. /* Work out the number of columns */
  282. WINE_TRACE("%d entries, maxwidth=%d, widest=%d\n", entry_count, max_width, widest);
  283. if (wide || orderByCol) {
  284. numCols = max(1, max_width / widest);
  285. numRows = entry_count / numCols;
  286. if (entry_count % numCols) numRows++;
  287. } else {
  288. numCols = 1;
  289. numRows = entry_count;
  290. }
  291. WINE_TRACE("cols=%d, rows=%d\n", numCols, numRows);
  292. for (rows=0; rows<numRows; rows++) {
  293. BOOL addNewLine = TRUE;
  294. for (cols=0; cols<numCols; cols++) {
  295. WCHAR username[24];
  296. /* Work out the index of the entry being pointed to */
  297. if (orderByCol) {
  298. i = (cols * numRows) + rows;
  299. if (i >= entry_count) continue;
  300. } else {
  301. i = (rows * numCols) + cols;
  302. if (i >= entry_count) continue;
  303. }
  304. /* /L convers all names to lower case */
  305. if (lower) {
  306. WCHAR *p = fd[i].cFileName;
  307. while ( (*p = tolower(*p)) ) ++p;
  308. }
  309. /* /Q gets file ownership information */
  310. if (usernames) {
  311. lstrcpyW (string, inputparms->dirName);
  312. lstrcatW (string, fd[i].cFileName);
  313. WCMD_getfileowner(string, username, ARRAY_SIZE(username));
  314. }
  315. if (dirTime == Written) {
  316. FileTimeToLocalFileTime (&fd[i].ftLastWriteTime, &ft);
  317. } else if (dirTime == Access) {
  318. FileTimeToLocalFileTime (&fd[i].ftLastAccessTime, &ft);
  319. } else {
  320. FileTimeToLocalFileTime (&fd[i].ftCreationTime, &ft);
  321. }
  322. FileTimeToSystemTime (&ft, &st);
  323. GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring, ARRAY_SIZE(datestring));
  324. GetTimeFormatW(0, TIME_NOSECONDS, &st, NULL, timestring, ARRAY_SIZE(timestring));
  325. if (wide) {
  326. tmp_width = cur_width;
  327. if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  328. WCMD_output (L"[%1]", fd[i].cFileName);
  329. dir_count++;
  330. tmp_width = tmp_width + lstrlenW(fd[i].cFileName) + 2;
  331. } else {
  332. WCMD_output (L"%1", fd[i].cFileName);
  333. tmp_width = tmp_width + lstrlenW(fd[i].cFileName) ;
  334. file_count++;
  335. file_size.u.LowPart = fd[i].nFileSizeLow;
  336. file_size.u.HighPart = fd[i].nFileSizeHigh;
  337. byte_count.QuadPart += file_size.QuadPart;
  338. }
  339. cur_width = cur_width + widest;
  340. if ((cur_width + widest) > max_width) {
  341. cur_width = 0;
  342. } else {
  343. WCMD_output(L"%1!*s!", cur_width - tmp_width, nullW);
  344. }
  345. } else if (fd[i].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  346. dir_count++;
  347. if (!bare) {
  348. WCMD_output (L"%1!10s! %2!8s! <DIR> ", datestring, timestring);
  349. if (shortname) WCMD_output (fmt2, fd[i].cAlternateFileName);
  350. if (usernames) WCMD_output (fmt3, username);
  351. WCMD_output(fmt4,fd[i].cFileName);
  352. } else {
  353. if (!((lstrcmpW(fd[i].cFileName, dotW) == 0) ||
  354. (lstrcmpW(fd[i].cFileName, dotdotW) == 0))) {
  355. WCMD_output (fmt5, recurse?inputparms->dirName:nullW, fd[i].cFileName);
  356. } else {
  357. addNewLine = FALSE;
  358. }
  359. }
  360. }
  361. else {
  362. file_count++;
  363. file_size.u.LowPart = fd[i].nFileSizeLow;
  364. file_size.u.HighPart = fd[i].nFileSizeHigh;
  365. byte_count.QuadPart += file_size.QuadPart;
  366. if (!bare) {
  367. WCMD_output (L"%1!10s! %2!8s! %3!10s! ", datestring, timestring,
  368. WCMD_filesize64(file_size.QuadPart));
  369. if (shortname) WCMD_output (fmt2, fd[i].cAlternateFileName);
  370. if (usernames) WCMD_output (fmt3, username);
  371. WCMD_output(fmt4,fd[i].cFileName);
  372. } else {
  373. WCMD_output (fmt5, recurse?inputparms->dirName:nullW, fd[i].cFileName);
  374. }
  375. }
  376. }
  377. if (addNewLine) WCMD_output_asis (newlineW);
  378. cur_width = 0;
  379. }
  380. if (!bare) {
  381. if (file_count == 1) {
  382. WCMD_output (L" 1 file %1!25s! bytes\n", WCMD_filesize64 (byte_count.QuadPart));
  383. }
  384. else {
  385. WCMD_output (L"%1!8d! files %2!24s! bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
  386. }
  387. }
  388. byte_total = byte_total + byte_count.QuadPart;
  389. file_total = file_total + file_count;
  390. dir_total = dir_total + dir_count;
  391. if (!bare && !recurse) {
  392. if (dir_count == 1) {
  393. WCMD_output (L"%1!8d! directory ", 1);
  394. } else {
  395. WCMD_output (L"%1!8d! directories", dir_count);
  396. }
  397. }
  398. }
  399. heap_free(fd);
  400. /* When recursing, look in all subdirectories for matches */
  401. if (recurse) {
  402. DIRECTORY_STACK *dirStack = NULL;
  403. DIRECTORY_STACK *lastEntry = NULL;
  404. WIN32_FIND_DATAW finddata;
  405. /* Build path to search */
  406. lstrcpyW(string, inputparms->dirName);
  407. lstrcatW(string, starW);
  408. WINE_TRACE("Recursive, looking for '%s'\n", wine_dbgstr_w(string));
  409. hff = FindFirstFileW(string, &finddata);
  410. if (hff != INVALID_HANDLE_VALUE) {
  411. do {
  412. if ((finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
  413. (lstrcmpW(finddata.cFileName, dotdotW) != 0) &&
  414. (lstrcmpW(finddata.cFileName, dotW) != 0)) {
  415. DIRECTORY_STACK *thisDir;
  416. int dirsToCopy = concurrentDirs;
  417. /* Loop creating list of subdirs for all concurrent entries */
  418. parms = inputparms;
  419. while (dirsToCopy > 0) {
  420. dirsToCopy--;
  421. /* Work out search parameter in sub dir */
  422. lstrcpyW (string, inputparms->dirName);
  423. lstrcatW (string, finddata.cFileName);
  424. lstrcatW (string, slashW);
  425. WINE_TRACE("Recursive, Adding to search list '%s'\n", wine_dbgstr_w(string));
  426. /* Allocate memory, add to list */
  427. thisDir = heap_xalloc(sizeof(DIRECTORY_STACK));
  428. if (dirStack == NULL) dirStack = thisDir;
  429. if (lastEntry != NULL) lastEntry->next = thisDir;
  430. lastEntry = thisDir;
  431. thisDir->next = NULL;
  432. thisDir->dirName = heap_strdupW(string);
  433. thisDir->fileName = heap_strdupW(parms->fileName);
  434. parms = parms->next;
  435. }
  436. }
  437. } while (FindNextFileW(hff, &finddata) != 0);
  438. FindClose (hff);
  439. while (dirStack != NULL) {
  440. DIRECTORY_STACK *thisDir = dirStack;
  441. dirStack = WCMD_list_directory (thisDir, 1);
  442. while (thisDir != dirStack) {
  443. DIRECTORY_STACK *tempDir = thisDir->next;
  444. heap_free(thisDir->dirName);
  445. heap_free(thisDir->fileName);
  446. heap_free(thisDir);
  447. thisDir = tempDir;
  448. }
  449. }
  450. }
  451. }
  452. /* Handle case where everything is filtered out */
  453. if ((file_total + dir_total == 0) && (level == 0)) {
  454. SetLastError (ERROR_FILE_NOT_FOUND);
  455. WCMD_print_error ();
  456. errorlevel = 1;
  457. }
  458. return parms;
  459. }
  460. /*****************************************************************************
  461. * WCMD_dir_trailer
  462. *
  463. * Print out the trailer for the supplied drive letter
  464. */
  465. static void WCMD_dir_trailer(WCHAR drive) {
  466. ULARGE_INTEGER avail, total, freebytes;
  467. DWORD status;
  468. WCHAR driveName[] = L"c:\\";
  469. driveName[0] = drive;
  470. status = GetDiskFreeSpaceExW(driveName, &avail, &total, &freebytes);
  471. WINE_TRACE("Writing trailer for '%s' gave %d(%d)\n", wine_dbgstr_w(driveName),
  472. status, GetLastError());
  473. if (errorlevel==0 && !bare) {
  474. if (recurse) {
  475. WCMD_output (L"\n Total files listed:\n%1!8d! files%2!25s! bytes\n", file_total, WCMD_filesize64 (byte_total));
  476. WCMD_output (L"%1!8d! directories %2!18s! bytes free\n\n", dir_total, WCMD_filesize64 (freebytes.QuadPart));
  477. } else {
  478. WCMD_output (L" %1!18s! bytes free\n\n", WCMD_filesize64 (freebytes.QuadPart));
  479. }
  480. }
  481. }
  482. /*****************************************************************************
  483. * WCMD_directory
  484. *
  485. * List a file directory.
  486. *
  487. */
  488. void WCMD_directory (WCHAR *args)
  489. {
  490. WCHAR path[MAX_PATH], cwd[MAX_PATH];
  491. DWORD status;
  492. CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
  493. WCHAR *p;
  494. WCHAR string[MAXSTRING];
  495. int argno = 0;
  496. WCHAR *argN = args;
  497. WCHAR lastDrive;
  498. BOOL trailerReqd = FALSE;
  499. DIRECTORY_STACK *fullParms = NULL;
  500. DIRECTORY_STACK *prevEntry = NULL;
  501. DIRECTORY_STACK *thisEntry = NULL;
  502. WCHAR drive[10];
  503. WCHAR dir[MAX_PATH];
  504. WCHAR fname[MAX_PATH];
  505. WCHAR ext[MAX_PATH];
  506. errorlevel = 0;
  507. /* Prefill quals with (uppercased) DIRCMD env var */
  508. if (GetEnvironmentVariableW(L"DIRCMD", string, ARRAY_SIZE(string))) {
  509. p = string;
  510. while ( (*p = toupper(*p)) ) ++p;
  511. lstrcatW(string,quals);
  512. lstrcpyW(quals, string);
  513. }
  514. byte_total = 0;
  515. file_total = dir_total = 0;
  516. /* Initialize all flags to their defaults as if no DIRCMD or quals */
  517. paged_mode = FALSE;
  518. recurse = FALSE;
  519. wide = FALSE;
  520. bare = FALSE;
  521. lower = FALSE;
  522. shortname = FALSE;
  523. usernames = FALSE;
  524. orderByCol = FALSE;
  525. separator = TRUE;
  526. dirTime = Written;
  527. dirOrder = Name;
  528. orderReverse = FALSE;
  529. orderGroupDirs = FALSE;
  530. orderGroupDirsReverse = FALSE;
  531. showattrs = 0;
  532. attrsbits = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM;
  533. /* Handle args - Loop through so right most is the effective one */
  534. /* Note: /- appears to be a negate rather than an off, eg. dir
  535. /-W is wide, or dir /w /-w /-w is also wide */
  536. p = quals;
  537. while (*p && (*p=='/' || *p==' ')) {
  538. BOOL negate = FALSE;
  539. if (*p++==' ') continue; /* Skip / and blanks introduced through DIRCMD */
  540. if (*p=='-') {
  541. negate = TRUE;
  542. p++;
  543. }
  544. WINE_TRACE("Processing arg '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
  545. switch (*p) {
  546. case 'P': if (negate) paged_mode = !paged_mode;
  547. else paged_mode = TRUE;
  548. break;
  549. case 'S': if (negate) recurse = !recurse;
  550. else recurse = TRUE;
  551. break;
  552. case 'W': if (negate) wide = !wide;
  553. else wide = TRUE;
  554. break;
  555. case 'B': if (negate) bare = !bare;
  556. else bare = TRUE;
  557. break;
  558. case 'L': if (negate) lower = !lower;
  559. else lower = TRUE;
  560. break;
  561. case 'X': if (negate) shortname = !shortname;
  562. else shortname = TRUE;
  563. break;
  564. case 'Q': if (negate) usernames = !usernames;
  565. else usernames = TRUE;
  566. break;
  567. case 'D': if (negate) orderByCol = !orderByCol;
  568. else orderByCol = TRUE;
  569. break;
  570. case 'C': if (negate) separator = !separator;
  571. else separator = TRUE;
  572. break;
  573. case 'T': p = p + 1;
  574. if (*p==':') p++; /* Skip optional : */
  575. if (*p == 'A') dirTime = Access;
  576. else if (*p == 'C') dirTime = Creation;
  577. else if (*p == 'W') dirTime = Written;
  578. /* Support /T and /T: with no parms, default to written */
  579. else if (*p == 0x00 || *p == '/') {
  580. dirTime = Written;
  581. p = p - 1; /* So when step on, move to '/' */
  582. } else {
  583. SetLastError(ERROR_INVALID_PARAMETER);
  584. WCMD_print_error();
  585. errorlevel = 1;
  586. return;
  587. }
  588. break;
  589. case 'O': p = p + 1;
  590. if (*p==':') p++; /* Skip optional : */
  591. while (*p && *p != '/') {
  592. WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
  593. switch (*p) {
  594. case 'N': dirOrder = Name; break;
  595. case 'E': dirOrder = Extension; break;
  596. case 'S': dirOrder = Size; break;
  597. case 'D': dirOrder = Date; break;
  598. case '-': if (*(p+1)=='G') orderGroupDirsReverse=TRUE;
  599. else orderReverse = TRUE;
  600. break;
  601. case 'G': orderGroupDirs = TRUE; break;
  602. default:
  603. SetLastError(ERROR_INVALID_PARAMETER);
  604. WCMD_print_error();
  605. errorlevel = 1;
  606. return;
  607. }
  608. p++;
  609. }
  610. p = p - 1; /* So when step on, move to '/' */
  611. break;
  612. case 'A': p = p + 1;
  613. showattrs = 0;
  614. attrsbits = 0;
  615. if (*p==':') p++; /* Skip optional : */
  616. while (*p && *p != '/') {
  617. BOOL anegate = FALSE;
  618. ULONG mask;
  619. /* Note /A: - options are 'offs' not toggles */
  620. if (*p=='-') {
  621. anegate = TRUE;
  622. p++;
  623. }
  624. WINE_TRACE("Processing subparm '%c' (in %s)\n", *p, wine_dbgstr_w(quals));
  625. switch (*p) {
  626. case 'D': mask = FILE_ATTRIBUTE_DIRECTORY; break;
  627. case 'H': mask = FILE_ATTRIBUTE_HIDDEN; break;
  628. case 'S': mask = FILE_ATTRIBUTE_SYSTEM; break;
  629. case 'R': mask = FILE_ATTRIBUTE_READONLY; break;
  630. case 'A': mask = FILE_ATTRIBUTE_ARCHIVE; break;
  631. default:
  632. SetLastError(ERROR_INVALID_PARAMETER);
  633. WCMD_print_error();
  634. errorlevel = 1;
  635. return;
  636. }
  637. /* Keep running list of bits we care about */
  638. attrsbits |= mask;
  639. /* Mask shows what MUST be in the bits we care about */
  640. if (anegate) showattrs = showattrs & ~mask;
  641. else showattrs |= mask;
  642. p++;
  643. }
  644. p = p - 1; /* So when step on, move to '/' */
  645. WINE_TRACE("Result: showattrs %x, bits %x\n", showattrs, attrsbits);
  646. break;
  647. default:
  648. SetLastError(ERROR_INVALID_PARAMETER);
  649. WCMD_print_error();
  650. errorlevel = 1;
  651. return;
  652. }
  653. p = p + 1;
  654. }
  655. /* Handle conflicting args and initialization */
  656. if (bare || shortname) wide = FALSE;
  657. if (bare) shortname = FALSE;
  658. if (wide) usernames = FALSE;
  659. if (orderByCol) wide = TRUE;
  660. if (wide) {
  661. if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
  662. max_width = consoleInfo.dwSize.X;
  663. else
  664. max_width = 80;
  665. }
  666. if (paged_mode) {
  667. WCMD_enter_paged_mode(NULL);
  668. }
  669. argno = 0;
  670. argN = args;
  671. GetCurrentDirectoryW(MAX_PATH, cwd);
  672. lstrcatW(cwd, slashW);
  673. /* Loop through all args, calculating full effective directory */
  674. fullParms = NULL;
  675. prevEntry = NULL;
  676. while (argN) {
  677. WCHAR fullname[MAXSTRING];
  678. WCHAR *thisArg = WCMD_parameter(args, argno++, &argN, FALSE, FALSE);
  679. if (argN && argN[0] != '/') {
  680. WINE_TRACE("Found parm '%s'\n", wine_dbgstr_w(thisArg));
  681. if (thisArg[1] == ':' && thisArg[2] == '\\') {
  682. lstrcpyW(fullname, thisArg);
  683. } else if (thisArg[1] == ':' && thisArg[2] != '\\') {
  684. WCHAR envvar[4];
  685. wsprintfW(envvar, L"=%c:", thisArg[0]);
  686. if (!GetEnvironmentVariableW(envvar, fullname, MAX_PATH)) {
  687. wsprintfW(fullname, L"%c:", thisArg[0]);
  688. }
  689. lstrcatW(fullname, slashW);
  690. lstrcatW(fullname, &thisArg[2]);
  691. } else if (thisArg[0] == '\\') {
  692. memcpy(fullname, cwd, 2 * sizeof(WCHAR));
  693. lstrcpyW(fullname+2, thisArg);
  694. } else {
  695. lstrcpyW(fullname, cwd);
  696. lstrcatW(fullname, thisArg);
  697. }
  698. WINE_TRACE("Using location '%s'\n", wine_dbgstr_w(fullname));
  699. status = GetFullPathNameW(fullname, ARRAY_SIZE(path), path, NULL);
  700. /*
  701. * If the path supplied does not include a wildcard, and the endpoint of the
  702. * path references a directory, we need to list the *contents* of that
  703. * directory not the directory file itself.
  704. */
  705. if ((wcschr(path, '*') == NULL) && (wcschr(path, '%') == NULL)) {
  706. status = GetFileAttributesW(path);
  707. if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
  708. if (!ends_with_backslash( path )) lstrcatW( path, slashW );
  709. lstrcatW (path, starW);
  710. }
  711. } else {
  712. /* Special case wildcard search with no extension (ie parameters ending in '.') as
  713. GetFullPathName strips off the additional '.' */
  714. if (fullname[lstrlenW(fullname)-1] == '.') lstrcatW(path, dotW);
  715. }
  716. WINE_TRACE("Using path '%s'\n", wine_dbgstr_w(path));
  717. thisEntry = heap_xalloc(sizeof(DIRECTORY_STACK));
  718. if (fullParms == NULL) fullParms = thisEntry;
  719. if (prevEntry != NULL) prevEntry->next = thisEntry;
  720. prevEntry = thisEntry;
  721. thisEntry->next = NULL;
  722. /* Split into components */
  723. WCMD_splitpath(path, drive, dir, fname, ext);
  724. WINE_TRACE("Path Parts: drive: '%s' dir: '%s' name: '%s' ext:'%s'\n",
  725. wine_dbgstr_w(drive), wine_dbgstr_w(dir),
  726. wine_dbgstr_w(fname), wine_dbgstr_w(ext));
  727. thisEntry->dirName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(drive)+lstrlenW(dir)+1));
  728. lstrcpyW(thisEntry->dirName, drive);
  729. lstrcatW(thisEntry->dirName, dir);
  730. thisEntry->fileName = heap_xalloc(sizeof(WCHAR) * (lstrlenW(fname)+lstrlenW(ext)+1));
  731. lstrcpyW(thisEntry->fileName, fname);
  732. lstrcatW(thisEntry->fileName, ext);
  733. }
  734. }
  735. /* If just 'dir' entered, a '*' parameter is assumed */
  736. if (fullParms == NULL) {
  737. WINE_TRACE("Inserting default '*'\n");
  738. fullParms = heap_xalloc(sizeof(DIRECTORY_STACK));
  739. fullParms->next = NULL;
  740. fullParms->dirName = heap_strdupW(cwd);
  741. fullParms->fileName = heap_strdupW(starW);
  742. }
  743. lastDrive = '?';
  744. prevEntry = NULL;
  745. thisEntry = fullParms;
  746. trailerReqd = FALSE;
  747. while (thisEntry != NULL) {
  748. /* Output disk free (trailer) and volume information (header) if the drive
  749. letter changes */
  750. if (lastDrive != toupper(thisEntry->dirName[0])) {
  751. /* Trailer Information */
  752. if (lastDrive != '?') {
  753. trailerReqd = FALSE;
  754. WCMD_dir_trailer(prevEntry->dirName[0]);
  755. }
  756. lastDrive = toupper(thisEntry->dirName[0]);
  757. if (!bare) {
  758. WCHAR drive[3];
  759. WINE_TRACE("Writing volume for '%c:'\n", thisEntry->dirName[0]);
  760. memcpy(drive, thisEntry->dirName, 2 * sizeof(WCHAR));
  761. drive[2] = 0x00;
  762. status = WCMD_volume (0, drive);
  763. trailerReqd = TRUE;
  764. if (!status) {
  765. errorlevel = 1;
  766. goto exit;
  767. }
  768. }
  769. } else {
  770. if (!bare) WCMD_output_asis (L"\n\n");
  771. }
  772. /* Clear any errors from previous invocations, and process it */
  773. errorlevel = 0;
  774. prevEntry = thisEntry;
  775. thisEntry = WCMD_list_directory (thisEntry, 0);
  776. }
  777. /* Trailer Information */
  778. if (trailerReqd) {
  779. WCMD_dir_trailer(prevEntry->dirName[0]);
  780. }
  781. exit:
  782. if (paged_mode) WCMD_leave_paged_mode();
  783. /* Free storage allocated for parms */
  784. while (fullParms != NULL) {
  785. prevEntry = fullParms;
  786. fullParms = prevEntry->next;
  787. heap_free(prevEntry->dirName);
  788. heap_free(prevEntry->fileName);
  789. heap_free(prevEntry);
  790. }
  791. }