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;
51 static ULONGLONG byte_total;
52 static DISPLAYTIME dirTime;
54 /*****************************************************************************
57 * List a file directory.
61 void WCMD_directory (void) {
63 char path[MAX_PATH], drive[8];
64 int status, paged_mode;
65 ULARGE_INTEGER avail, total, free;
66 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
70 file_total = dir_total = 0;
74 paged_mode = (strstr(quals, "/P") != NULL);
75 recurse = (strstr(quals, "/S") != NULL);
76 wide = (strstr(quals, "/W") != NULL);
77 bare = (strstr(quals, "/B") != NULL);
78 lower = (strstr(quals, "/L") != NULL);
79 shortname = (strstr(quals, "/X") != NULL);
81 if ((p = strstr(quals, "/T")) != NULL) {
83 if (*p==':') p++; /* Skip optional : */
85 if (*p == 'A') dirTime = Access;
86 else if (*p == 'C') dirTime = Creation;
87 else if (*p == 'W') dirTime = Written;
89 /* Support /T and /T: with no parms, default to written */
90 else if (*p == '/') dirTime = Written;
92 SetLastError(ERROR_INVALID_PARAMETER);
98 /* Handle conflicting args and initialization */
99 if (bare || shortname) wide = FALSE;
100 if (bare) shortname = FALSE;
103 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
104 max_width = consoleInfo.dwSize.X;
109 WCMD_enter_paged_mode();
112 if (param1[0] == '\0') strcpy (param1, ".");
113 status = GetFullPathName (param1, sizeof(path), path, NULL);
116 if (paged_mode) WCMD_leave_paged_mode();
119 lstrcpyn (drive, path, 3);
122 status = WCMD_volume (0, drive);
124 if (paged_mode) WCMD_leave_paged_mode();
129 WCMD_list_directory (path, 0);
130 lstrcpyn (drive, path, 4);
131 GetDiskFreeSpaceEx (drive, &avail, &total, &free);
135 WCMD_output ("\n\n Total files listed:\n%8d files%25s bytes\n",
136 file_total, WCMD_filesize64 (byte_total));
137 WCMD_output ("%8d directories %18s bytes free\n\n",
138 dir_total, WCMD_filesize64 (free.QuadPart));
140 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
143 if (paged_mode) WCMD_leave_paged_mode();
146 /*****************************************************************************
147 * WCMD_list_directory
149 * List a single file directory. This function (and those below it) can be called
150 * recursively when the /S switch is used.
152 * FIXME: Entries sorted by name only. Should we support DIRCMD??
153 * FIXME: Assumes 24-line display for the /P qualifier.
154 * FIXME: Other command qualifiers not supported.
155 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
158 void WCMD_list_directory (char *search_path, int level) {
160 char string[1024], datestring[32], timestring[32];
162 char real_path[MAX_PATH];
167 int status, dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
168 ULARGE_INTEGER byte_count, file_size;
173 byte_count.QuadPart = 0;
178 * If the path supplied does not include a wildcard, and the endpoint of the
179 * path references a directory, we need to list the *contents* of that
180 * directory not the directory file itself.
183 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
184 status = GetFileAttributes (search_path);
185 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
186 if (search_path[strlen(search_path)-1] == '\\') {
187 strcat (search_path, "*");
190 strcat (search_path, "\\*");
195 /* Work out the actual current directory name */
196 p = strrchr (search_path, '\\');
197 memset(real_path, 0x00, sizeof(real_path));
198 lstrcpyn (real_path, search_path, (p-search_path+2));
200 /* Load all files into an in memory structure */
201 fd = malloc (sizeof(WIN32_FIND_DATA));
202 hff = FindFirstFile (search_path, fd);
203 if (hff == INVALID_HANDLE_VALUE) {
204 SetLastError (ERROR_FILE_NOT_FOUND);
212 /* Keep running track of longest filename for wide output */
214 int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
215 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
216 if (tmpLen > widest) widest = tmpLen;
219 fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
222 WCMD_output ("Memory Allocation Error");
225 } while (FindNextFile(hff, (fd+entry_count)) != 0);
228 /* Sort the list of files */
229 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
231 /* Output the results */
233 if (level != 0) WCMD_output ("\n\n");
234 WCMD_output ("Directory of %s\n\n", real_path);
237 for (i=0; i<entry_count; i++) {
239 /* /L convers all names to lower case */
241 char *p = (fd+i)->cFileName;
242 while ( (*p = tolower(*p)) ) ++p;
245 if (dirTime == Written) {
246 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
247 } else if (dirTime == Access) {
248 FileTimeToLocalFileTime (&(fd+i)->ftLastAccessTime, &ft);
250 FileTimeToLocalFileTime (&(fd+i)->ftCreationTime, &ft);
252 FileTimeToSystemTime (&ft, &st);
253 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
255 GetTimeFormat (0, TIME_NOSECONDS, &st,
256 NULL, timestring, sizeof(timestring));
260 tmp_width = cur_width;
261 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
262 WCMD_output ("[%s]", (fd+i)->cFileName);
264 tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
266 WCMD_output ("%s", (fd+i)->cFileName);
267 tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
269 file_size.u.LowPart = (fd+i)->nFileSizeLow;
270 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
271 byte_count.QuadPart += file_size.QuadPart;
273 cur_width = cur_width + widest;
275 if ((cur_width + widest) > max_width) {
279 WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
282 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
287 WCMD_output ("%10s %8s <DIR> %-13s%s\n",
288 datestring, timestring, (fd+i)->cAlternateFileName, (fd+i)->cFileName);
290 WCMD_output ("%10s %8s <DIR> %s\n",
291 datestring, timestring, (fd+i)->cFileName);
294 if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
295 (strcmp((fd+i)->cFileName, "..") == 0))) {
296 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
302 file_size.u.LowPart = (fd+i)->nFileSizeLow;
303 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
304 byte_count.QuadPart += file_size.QuadPart;
307 WCMD_output ("%10s %8s %10s %-13s%s\n",
308 datestring, timestring,
309 WCMD_filesize64(file_size.QuadPart), (fd+i)->cAlternateFileName, (fd+i)->cFileName);
311 WCMD_output ("%10s %8s %10s %s\n",
312 datestring, timestring,
313 WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
316 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
321 if (wide && cur_width>0) {
326 if (file_count == 1) {
327 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
330 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
333 byte_total = byte_total + byte_count.QuadPart;
334 file_total = file_total + file_count;
335 dir_total = dir_total + dir_count;
338 if (dir_count == 1) WCMD_output ("1 directory ");
339 else WCMD_output ("%8d directories", dir_count);
341 for (i=0; i<entry_count; i++) {
343 ((fd+i)->cFileName[0] != '.') &&
344 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
346 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
348 p = strrchr (search_path, '\\');
349 lstrcpyn (string, search_path, (p-search_path+2));
350 lstrcat (string, (fd+i)->cFileName);
352 WCMD_list_directory (string, 1);
359 /*****************************************************************************
362 * Convert a 64-bit number into a character string, with commas every three digits.
363 * Result is returned in a static string overwritten with each call.
364 * FIXME: There must be a better algorithm!
367 char * WCMD_filesize64 (ULONGLONG n) {
372 static char buff[32];
377 if ((++i)%3 == 1) *p++ = ',';
388 /*****************************************************************************
391 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
394 char * WCMD_strrev (char *buff) {
400 for (i=0; i<r/2; i++) {
402 buff[i] = buff[r-i-1];
409 int WCMD_dir_sort (const void *a, const void *b)
411 return (lstrcmpi(((const WIN32_FIND_DATA *)a)->cFileName,
412 ((const WIN32_FIND_DATA *)b)->cFileName));