2 * CMD - Wine-compatible command line interface - Directory functions.
4 * Copyright (C) 1999 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * On entry, global variables quals, param1, param2 contain
24 * the qualifiers (uppercased and concatenated) and parameters entered, with
25 * environment-variable and batch parameter substitution already done.
28 #define WIN32_LEAN_AND_MEAN
32 int WCMD_dir_sort (const void *a, const void *b);
33 void WCMD_list_directory (char *path, int level);
34 char * WCMD_filesize64 (ULONGLONG free);
35 char * WCMD_strrev (char *buff);
39 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
40 extern DWORD errorlevel;
42 typedef enum _DISPLAYTIME
49 static int file_total, dir_total, recurse, wide, bare, max_width, lower;
50 static ULONGLONG byte_total;
51 static DISPLAYTIME dirTime;
53 /*****************************************************************************
56 * List a file directory.
60 void WCMD_directory (void) {
62 char path[MAX_PATH], drive[8];
63 int status, paged_mode;
64 ULARGE_INTEGER avail, total, free;
65 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
69 file_total = dir_total = 0;
73 paged_mode = (strstr(quals, "/P") != NULL);
74 recurse = (strstr(quals, "/S") != NULL);
75 wide = (strstr(quals, "/W") != NULL);
76 bare = (strstr(quals, "/B") != NULL);
77 lower = (strstr(quals, "/L") != NULL);
79 if ((p = strstr(quals, "/T")) != NULL) {
81 if (*p==':') p++; /* Skip optional : */
83 if (*p == 'A') dirTime = Access;
84 else if (*p == 'C') dirTime = Creation;
85 else if (*p == 'W') dirTime = Written;
87 /* Support /T and /T: with no parms, default to written */
88 else if (*p == '/') dirTime = Written;
90 SetLastError(ERROR_INVALID_PARAMETER);
96 /* Handle conflicting args and initialization */
97 if (bare) wide = FALSE;
100 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
101 max_width = consoleInfo.dwSize.X;
106 WCMD_enter_paged_mode();
109 if (param1[0] == '\0') strcpy (param1, ".");
110 status = GetFullPathName (param1, sizeof(path), path, NULL);
113 if (paged_mode) WCMD_leave_paged_mode();
116 lstrcpyn (drive, path, 3);
119 status = WCMD_volume (0, drive);
121 if (paged_mode) WCMD_leave_paged_mode();
126 WCMD_list_directory (path, 0);
127 lstrcpyn (drive, path, 4);
128 GetDiskFreeSpaceEx (drive, &avail, &total, &free);
132 WCMD_output ("\n\n Total files listed:\n%8d files%25s bytes\n",
133 file_total, WCMD_filesize64 (byte_total));
134 WCMD_output ("%8d directories %18s bytes free\n\n",
135 dir_total, WCMD_filesize64 (free.QuadPart));
137 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
140 if (paged_mode) WCMD_leave_paged_mode();
143 /*****************************************************************************
144 * WCMD_list_directory
146 * List a single file directory. This function (and those below it) can be called
147 * recursively when the /S switch is used.
149 * FIXME: Entries sorted by name only. Should we support DIRCMD??
150 * FIXME: Assumes 24-line display for the /P qualifier.
151 * FIXME: Other command qualifiers not supported.
152 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
155 void WCMD_list_directory (char *search_path, int level) {
157 char string[1024], datestring[32], timestring[32];
159 char real_path[MAX_PATH];
164 int status, dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
165 ULARGE_INTEGER byte_count, file_size;
170 byte_count.QuadPart = 0;
175 * If the path supplied does not include a wildcard, and the endpoint of the
176 * path references a directory, we need to list the *contents* of that
177 * directory not the directory file itself.
180 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
181 status = GetFileAttributes (search_path);
182 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
183 if (search_path[strlen(search_path)-1] == '\\') {
184 strcat (search_path, "*");
187 strcat (search_path, "\\*");
192 /* Work out the actual current directory name */
193 p = strrchr (search_path, '\\');
194 memset(real_path, 0x00, sizeof(real_path));
195 lstrcpyn (real_path, search_path, (p-search_path+2));
197 /* Load all files into an in memory structure */
198 fd = malloc (sizeof(WIN32_FIND_DATA));
199 hff = FindFirstFile (search_path, fd);
200 if (hff == INVALID_HANDLE_VALUE) {
201 SetLastError (ERROR_FILE_NOT_FOUND);
209 /* Keep running track of longest filename for wide output */
211 int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
212 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
213 if (tmpLen > widest) widest = tmpLen;
216 fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
219 WCMD_output ("Memory Allocation Error");
222 } while (FindNextFile(hff, (fd+entry_count)) != 0);
225 /* Sort the list of files */
226 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
228 /* Output the results */
230 if (level != 0) WCMD_output ("\n\n");
231 WCMD_output ("Directory of %s\n\n", real_path);
234 for (i=0; i<entry_count; i++) {
236 /* /L convers all names to lower case */
238 char *p = (fd+i)->cFileName;
239 while ( (*p = tolower(*p)) ) ++p;
242 if (dirTime == Written) {
243 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
244 } else if (dirTime == Access) {
245 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
247 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
249 FileTimeToSystemTime (&ft, &st);
250 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
252 GetTimeFormat (0, TIME_NOSECONDS, &st,
253 NULL, timestring, sizeof(timestring));
257 tmp_width = cur_width;
258 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
259 WCMD_output ("[%s]", (fd+i)->cFileName);
261 tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
263 WCMD_output ("%s", (fd+i)->cFileName);
264 tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
266 file_size.u.LowPart = (fd+i)->nFileSizeLow;
267 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
268 byte_count.QuadPart += file_size.QuadPart;
270 cur_width = cur_width + widest;
272 if ((cur_width + widest) > max_width) {
276 WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
279 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
283 WCMD_output ("%10s %8s <DIR> %s\n",
284 datestring, timestring, (fd+i)->cFileName);
286 if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
287 (strcmp((fd+i)->cFileName, "..") == 0))) {
288 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
294 file_size.u.LowPart = (fd+i)->nFileSizeLow;
295 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
296 byte_count.QuadPart += file_size.QuadPart;
298 WCMD_output ("%10s %8s %10s %s\n",
299 datestring, timestring,
300 WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
302 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
307 if (wide && cur_width>0) {
312 if (file_count == 1) {
313 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
316 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
319 byte_total = byte_total + byte_count.QuadPart;
320 file_total = file_total + file_count;
321 dir_total = dir_total + dir_count;
324 if (dir_count == 1) WCMD_output ("1 directory ");
325 else WCMD_output ("%8d directories", dir_count);
327 for (i=0; i<entry_count; i++) {
329 ((fd+i)->cFileName[0] != '.') &&
330 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
332 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
334 p = strrchr (search_path, '\\');
335 lstrcpyn (string, search_path, (p-search_path+2));
336 lstrcat (string, (fd+i)->cFileName);
338 WCMD_list_directory (string, 1);
345 /*****************************************************************************
348 * Convert a 64-bit number into a character string, with commas every three digits.
349 * Result is returned in a static string overwritten with each call.
350 * FIXME: There must be a better algorithm!
353 char * WCMD_filesize64 (ULONGLONG n) {
358 static char buff[32];
363 if ((++i)%3 == 1) *p++ = ',';
374 /*****************************************************************************
377 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
380 char * WCMD_strrev (char *buff) {
386 for (i=0; i<r/2; i++) {
388 buff[i] = buff[r-i-1];
395 int WCMD_dir_sort (const void *a, const void *b)
397 return (lstrcmpi(((const WIN32_FIND_DATA *)a)->cFileName,
398 ((const WIN32_FIND_DATA *)b)->cFileName));