blob: ce0127e53eee5c6a07317eb1cf7968f84fba12d7 [file] [log] [blame]
Dave Pickles74f440e1999-06-06 15:24:04 +00001/*
2 * WCMD - Wine-compatible command line interface - Directory functions.
3 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00004 * Copyright (C) 1999 D A Pickles
Dave Pickles74f440e1999-06-06 15:24:04 +00005 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 * 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.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * NOTES:
Dave Pickles74f440e1999-06-06 15:24:04 +000023 * 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.
26 */
27
Dave Pickles74f440e1999-06-06 15:24:04 +000028#include "wcmd.h"
29
30int WCMD_dir_sort (const void *a, const void *b);
31void WCMD_list_directory (char *path, int level);
Alexandre Julliarddd0bdbe2002-09-12 17:29:12 +000032char * WCMD_filesize64 (ULONGLONG free);
Dave Pickles74f440e1999-06-06 15:24:04 +000033char * WCMD_strrev (char *buff);
34
35
Dave Pickles74f440e1999-06-06 15:24:04 +000036extern char nyi[];
37extern char newline[];
38extern char version_string[];
39extern char anykey[];
40extern int echo_mode;
41extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
Dave Picklesebecf502000-08-01 22:02:18 +000042extern DWORD errorlevel;
Dave Pickles74f440e1999-06-06 15:24:04 +000043
Vincent Béron9a624912002-05-31 23:06:46 +000044int file_total, dir_total, line_count, page_mode, recurse, wide, bare,
Jason Edmeadesa8828952002-04-29 23:33:09 +000045 max_width;
Alexandre Julliarddd0bdbe2002-09-12 17:29:12 +000046ULONGLONG byte_total;
Dave Pickles74f440e1999-06-06 15:24:04 +000047
48/*****************************************************************************
49 * WCMD_directory
50 *
51 * List a file directory.
Dave Pickles74f440e1999-06-06 15:24:04 +000052 *
53 */
54
55void WCMD_directory () {
56
57char path[MAX_PATH], drive[8];
58int status;
Dave Pickles64fba1c2001-06-04 02:55:38 +000059ULARGE_INTEGER avail, total, free;
Jason Edmeadesa8828952002-04-29 23:33:09 +000060CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
Dave Pickles74f440e1999-06-06 15:24:04 +000061
62 line_count = 5;
Dave Picklesa9d02092001-06-20 22:52:12 +000063 byte_total = 0;
64 file_total = dir_total = 0;
Jason Edmeadesa8828952002-04-29 23:33:09 +000065
66 /* Handle args */
Dave Pickles74f440e1999-06-06 15:24:04 +000067 page_mode = (strstr(quals, "/P") != NULL);
68 recurse = (strstr(quals, "/S") != NULL);
Jason Edmeadesa8828952002-04-29 23:33:09 +000069 wide = (strstr(quals, "/W") != NULL);
70 bare = (strstr(quals, "/B") != NULL);
71
72 /* Handle conflicting args and initialization */
73 if (bare) wide = FALSE;
74
75 if (wide) {
76 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
77 max_width = consoleInfo.dwSize.X;
78 }
79
Dave Pickles74f440e1999-06-06 15:24:04 +000080 if (param1[0] == '\0') strcpy (param1, ".");
Dave Picklesebecf502000-08-01 22:02:18 +000081 status = GetFullPathName (param1, sizeof(path), path, NULL);
82 if (!status) {
83 WCMD_print_error();
84 return;
85 }
Dave Pickles74f440e1999-06-06 15:24:04 +000086 lstrcpyn (drive, path, 3);
Jason Edmeadesa8828952002-04-29 23:33:09 +000087
88 if (!bare) {
89 status = WCMD_volume (0, drive);
90 if (!status) {
91 return;
92 }
Dave Pickles74f440e1999-06-06 15:24:04 +000093 }
Jason Edmeadesa8828952002-04-29 23:33:09 +000094
Dave Pickles74f440e1999-06-06 15:24:04 +000095 WCMD_list_directory (path, 0);
96 lstrcpyn (drive, path, 4);
Dave Pickles64fba1c2001-06-04 02:55:38 +000097 GetDiskFreeSpaceEx (drive, &avail, &total, &free);
Jason Edmeadesa8828952002-04-29 23:33:09 +000098
99 if (!bare) {
100 if (recurse) {
101 WCMD_output ("\n\n Total files listed:\n%8d files%25s bytes\n",
102 file_total, WCMD_filesize64 (byte_total));
Vincent Béron9a624912002-05-31 23:06:46 +0000103 WCMD_output ("%8d directories %18s bytes free\n\n",
Jason Edmeadesa8828952002-04-29 23:33:09 +0000104 dir_total, WCMD_filesize64 (free.QuadPart));
105 } else {
106 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
107 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000108 }
109}
110
111/*****************************************************************************
112 * WCMD_list_directory
113 *
114 * List a single file directory. This function (and those below it) can be called
115 * recursively when the /S switch is used.
116 *
Dave Pickles74f440e1999-06-06 15:24:04 +0000117 * FIXME: Entries sorted by name only. Should we support DIRCMD??
118 * FIXME: Assumes 24-line display for the /P qualifier.
119 * FIXME: Other command qualifiers not supported.
Vincent Béron9a624912002-05-31 23:06:46 +0000120 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
Dave Pickles74f440e1999-06-06 15:24:04 +0000121 */
122
123void WCMD_list_directory (char *search_path, int level) {
124
125char string[1024], datestring[32], timestring[32];
126char mem_err[] = "Memory Allocation Error";
127char *p;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000128char real_path[MAX_PATH];
Dave Pickles74f440e1999-06-06 15:24:04 +0000129DWORD count;
130WIN32_FIND_DATA *fd;
131FILETIME ft;
132SYSTEMTIME st;
133HANDLE hff;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000134int status, dir_count, file_count, entry_count, i, widest, linesout, cur_width, tmp_width;
Dave Pickles64fba1c2001-06-04 02:55:38 +0000135ULARGE_INTEGER byte_count, file_size;
Dave Pickles74f440e1999-06-06 15:24:04 +0000136
137 dir_count = 0;
138 file_count = 0;
139 entry_count = 0;
Dave Pickles64fba1c2001-06-04 02:55:38 +0000140 byte_count.QuadPart = 0;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000141 widest = 0;
142 linesout = 0;
143 cur_width = 0;
Dave Pickles74f440e1999-06-06 15:24:04 +0000144
145/*
146 * If the path supplied does not include a wildcard, and the endpoint of the
147 * path references a directory, we need to list the *contents* of that
148 * directory not the directory file itself.
149 */
150
151 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
152 status = GetFileAttributes (search_path);
153 if ((status != -1) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
154 if (search_path[strlen(search_path)-1] == '\\') {
155 strcat (search_path, "*");
156 }
157 else {
158 strcat (search_path, "\\*");
159 }
160 }
161 }
162
Jason Edmeadesa8828952002-04-29 23:33:09 +0000163 /* Work out the actual current directory name */
164 p = strrchr (search_path, '\\');
165 memset(real_path, 0x00, sizeof(real_path));
166 lstrcpyn (real_path, search_path, (p-search_path+2));
167
168 /* Load all files into an in memory structure */
Dave Pickles74f440e1999-06-06 15:24:04 +0000169 fd = malloc (sizeof(WIN32_FIND_DATA));
170 hff = FindFirstFile (search_path, fd);
171 if (hff == INVALID_HANDLE_VALUE) {
Dave Picklesebecf502000-08-01 22:02:18 +0000172 SetLastError (ERROR_FILE_NOT_FOUND);
173 WCMD_print_error ();
Dave Pickles74f440e1999-06-06 15:24:04 +0000174 free (fd);
175 return;
176 }
177 do {
178 entry_count++;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000179
180 /* Keep running track of longest filename for wide output */
181 if (wide) {
182 int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
183 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
184 if (tmpLen > widest) widest = tmpLen;
185 }
186
Dave Pickles74f440e1999-06-06 15:24:04 +0000187 fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
188 if (fd == NULL) {
189 FindClose (hff);
190 WCMD_output (mem_err);
191 return;
192 }
193 } while (FindNextFile(hff, (fd+entry_count)) != 0);
194 FindClose (hff);
Jason Edmeadesa8828952002-04-29 23:33:09 +0000195
196 /* Sort the list of files */
Dave Pickles74f440e1999-06-06 15:24:04 +0000197 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
Jason Edmeadesa8828952002-04-29 23:33:09 +0000198
199 /* Output the results */
200 if (!bare) {
201 if (level != 0) WCMD_output ("\n\n");
202 WCMD_output ("Directory of %s\n\n", real_path);
203 if (page_mode) {
204 line_count += 2;
205 if (line_count > 23) {
206 line_count = 0;
207 WCMD_output (anykey);
208 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
209 }
210 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000211 }
Jason Edmeadesa8828952002-04-29 23:33:09 +0000212
Dave Pickles74f440e1999-06-06 15:24:04 +0000213 for (i=0; i<entry_count; i++) {
214 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
215 FileTimeToSystemTime (&ft, &st);
216 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
217 sizeof(datestring));
218 GetTimeFormat (0, TIME_NOSECONDS, &st,
219 NULL, timestring, sizeof(timestring));
Jason Edmeadesa8828952002-04-29 23:33:09 +0000220
221 if (wide) {
222
223 tmp_width = cur_width;
224 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
225 WCMD_output ("[");
226 WCMD_output ("%s", (fd+i)->cFileName);
227 WCMD_output ("]");
228 dir_count++;
229 tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
230 } else {
231 WCMD_output ("%s", (fd+i)->cFileName);
232 tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
233 file_count++;
234#ifndef NONAMELESSSTRUCT
235 file_size.LowPart = (fd+i)->nFileSizeLow;
236 file_size.HighPart = (fd+i)->nFileSizeHigh;
237#else
238 file_size.s.LowPart = (fd+i)->nFileSizeLow;
239 file_size.s.HighPart = (fd+i)->nFileSizeHigh;
240#endif
241 byte_count.QuadPart += file_size.QuadPart;
242 }
243 cur_width = cur_width + widest;
244
245 if ((cur_width + widest) > max_width) {
246 WCMD_output ("\n");
247 cur_width = 0;
248 linesout++;
249 } else {
250 WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
251 }
252
253 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Dave Pickles74f440e1999-06-06 15:24:04 +0000254 dir_count++;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000255
256 if (!bare) {
257 WCMD_output ("%8s %8s <DIR> %s\n",
258 datestring, timestring, (fd+i)->cFileName);
259 linesout++;
260 } else {
Vincent Béron9a624912002-05-31 23:06:46 +0000261 if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
Jason Edmeadesa8828952002-04-29 23:33:09 +0000262 (strcmp((fd+i)->cFileName, "..") == 0))) {
263 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
264 linesout++;
265 }
266 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000267 }
268 else {
269 file_count++;
Francois Gougetf860ded2001-06-08 18:59:03 +0000270#ifndef NONAMELESSSTRUCT
Dave Pickles64fba1c2001-06-04 02:55:38 +0000271 file_size.LowPart = (fd+i)->nFileSizeLow;
272 file_size.HighPart = (fd+i)->nFileSizeHigh;
Francois Gougetf860ded2001-06-08 18:59:03 +0000273#else
274 file_size.s.LowPart = (fd+i)->nFileSizeLow;
275 file_size.s.HighPart = (fd+i)->nFileSizeHigh;
276#endif
Dave Pickles64fba1c2001-06-04 02:55:38 +0000277 byte_count.QuadPart += file_size.QuadPart;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000278 if (!bare) {
279 WCMD_output ("%8s %8s %10s %s\n",
280 datestring, timestring,
281 WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
282 linesout++;
283 } else {
284 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
285 linesout++;
286 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000287 }
288 if (page_mode) {
Jason Edmeadesa8828952002-04-29 23:33:09 +0000289 line_count = line_count + linesout;
290 linesout = 0;
291 if (line_count > 23) {
Dave Pickles74f440e1999-06-06 15:24:04 +0000292 line_count = 0;
293 WCMD_output (anykey);
Dave Pickles036a9f71999-07-10 11:36:45 +0000294 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
Dave Pickles74f440e1999-06-06 15:24:04 +0000295 }
296 }
297 }
Jason Edmeadesa8828952002-04-29 23:33:09 +0000298
299 if (wide && cur_width>0) {
300 WCMD_output ("\n");
301 if (page_mode) {
302 if (++line_count > 23) {
303 line_count = 0;
304 WCMD_output (anykey);
305 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
306 }
307 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000308 }
Jason Edmeadesa8828952002-04-29 23:33:09 +0000309
310 if (!bare) {
311 if (file_count == 1) {
312 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
313 }
314 else {
315 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
316 }
317 if (page_mode) {
318 if (++line_count > 23) {
319 line_count = 0;
320 WCMD_output (anykey);
321 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
322 }
323 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000324 }
Dave Pickles64fba1c2001-06-04 02:55:38 +0000325 byte_total = byte_total + byte_count.QuadPart;
Dave Pickles74f440e1999-06-06 15:24:04 +0000326 file_total = file_total + file_count;
327 dir_total = dir_total + dir_count;
Jason Edmeadesa8828952002-04-29 23:33:09 +0000328
329 if (!bare) {
330 if (dir_count == 1) WCMD_output ("1 directory ");
331 else WCMD_output ("%8d directories", dir_count);
332 if (page_mode) {
333 if (++line_count > 23) {
334 line_count = 0;
335 WCMD_output (anykey);
336 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
337 }
338 }
Dave Pickles74f440e1999-06-06 15:24:04 +0000339 }
340 for (i=0; i<entry_count; i++) {
341 if ((recurse) &&
342 ((fd+i)->cFileName[0] != '.') &&
343 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
Patrik Stridvall205721e2000-03-24 20:40:41 +0000344#if 0
345 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
346#endif
Dave Pickles74f440e1999-06-06 15:24:04 +0000347 p = strrchr (search_path, '\\');
348 lstrcpyn (string, search_path, (p-search_path+2));
349 lstrcat (string, (fd+i)->cFileName);
350 lstrcat (string, p);
351 WCMD_list_directory (string, 1);
352 }
353 }
354 free (fd);
355 return;
356}
357
358/*****************************************************************************
359 * WCMD_filesize64
360 *
361 * Convert a 64-bit number into a character string, with commas every three digits.
362 * Result is returned in a static string overwritten with each call.
363 * FIXME: There must be a better algorithm!
364 */
365
Alexandre Julliarddd0bdbe2002-09-12 17:29:12 +0000366char * WCMD_filesize64 (ULONGLONG n) {
Dave Pickles74f440e1999-06-06 15:24:04 +0000367
Alexandre Julliarddd0bdbe2002-09-12 17:29:12 +0000368ULONGLONG q;
Gregg Mattinsond0f8bf32002-07-05 22:47:56 +0000369uint r, i;
Dave Pickles74f440e1999-06-06 15:24:04 +0000370char *p;
371static char buff[32];
372
373 p = buff;
374 i = -3;
375 do {
376 if ((++i)%3 == 1) *p++ = ',';
377 q = n / 10;
378 r = n - (q * 10);
379 *p++ = r + '0';
380 *p = '\0';
381 n = q;
382 } while (n != 0);
383 WCMD_strrev (buff);
384 return buff;
385}
386
387/*****************************************************************************
Dave Pickles74f440e1999-06-06 15:24:04 +0000388 * WCMD_strrev
389 *
390 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
391 */
392
393char * WCMD_strrev (char *buff) {
394
395int r, i;
396char b;
397
398 r = lstrlen (buff);
399 for (i=0; i<r/2; i++) {
400 b = buff[i];
401 buff[i] = buff[r-i-1];
402 buff[r-i-1] = b;
403 }
404 return (buff);
405}
406
407
408int WCMD_dir_sort (const void *a, const void *b) {
409
410 return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName,
411 ((WIN32_FIND_DATA *)b)->cFileName));
412}