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 static int file_total, dir_total, recurse, wide, bare, max_width, lower;
43 static ULONGLONG byte_total;
45 /*****************************************************************************
48 * List a file directory.
52 void WCMD_directory (void) {
54 char path[MAX_PATH], drive[8];
55 int status, paged_mode;
56 ULARGE_INTEGER avail, total, free;
57 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
60 file_total = dir_total = 0;
63 paged_mode = (strstr(quals, "/P") != NULL);
64 recurse = (strstr(quals, "/S") != NULL);
65 wide = (strstr(quals, "/W") != NULL);
66 bare = (strstr(quals, "/B") != NULL);
67 lower = (strstr(quals, "/L") != NULL);
69 /* Handle conflicting args and initialization */
70 if (bare) wide = FALSE;
73 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
74 max_width = consoleInfo.dwSize.X;
79 WCMD_enter_paged_mode();
82 if (param1[0] == '\0') strcpy (param1, ".");
83 status = GetFullPathName (param1, sizeof(path), path, NULL);
86 if (paged_mode) WCMD_leave_paged_mode();
89 lstrcpyn (drive, path, 3);
92 status = WCMD_volume (0, drive);
94 if (paged_mode) WCMD_leave_paged_mode();
99 WCMD_list_directory (path, 0);
100 lstrcpyn (drive, path, 4);
101 GetDiskFreeSpaceEx (drive, &avail, &total, &free);
105 WCMD_output ("\n\n Total files listed:\n%8d files%25s bytes\n",
106 file_total, WCMD_filesize64 (byte_total));
107 WCMD_output ("%8d directories %18s bytes free\n\n",
108 dir_total, WCMD_filesize64 (free.QuadPart));
110 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
113 if (paged_mode) WCMD_leave_paged_mode();
116 /*****************************************************************************
117 * WCMD_list_directory
119 * List a single file directory. This function (and those below it) can be called
120 * recursively when the /S switch is used.
122 * FIXME: Entries sorted by name only. Should we support DIRCMD??
123 * FIXME: Assumes 24-line display for the /P qualifier.
124 * FIXME: Other command qualifiers not supported.
125 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
128 void WCMD_list_directory (char *search_path, int level) {
130 char string[1024], datestring[32], timestring[32];
132 char real_path[MAX_PATH];
137 int status, dir_count, file_count, entry_count, i, widest, cur_width, tmp_width;
138 ULARGE_INTEGER byte_count, file_size;
143 byte_count.QuadPart = 0;
148 * If the path supplied does not include a wildcard, and the endpoint of the
149 * path references a directory, we need to list the *contents* of that
150 * directory not the directory file itself.
153 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
154 status = GetFileAttributes (search_path);
155 if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
156 if (search_path[strlen(search_path)-1] == '\\') {
157 strcat (search_path, "*");
160 strcat (search_path, "\\*");
165 /* Work out the actual current directory name */
166 p = strrchr (search_path, '\\');
167 memset(real_path, 0x00, sizeof(real_path));
168 lstrcpyn (real_path, search_path, (p-search_path+2));
170 /* Load all files into an in memory structure */
171 fd = malloc (sizeof(WIN32_FIND_DATA));
172 hff = FindFirstFile (search_path, fd);
173 if (hff == INVALID_HANDLE_VALUE) {
174 SetLastError (ERROR_FILE_NOT_FOUND);
182 /* Keep running track of longest filename for wide output */
184 int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
185 if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
186 if (tmpLen > widest) widest = tmpLen;
189 fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
192 WCMD_output ("Memory Allocation Error");
195 } while (FindNextFile(hff, (fd+entry_count)) != 0);
198 /* Sort the list of files */
199 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
201 /* Output the results */
203 if (level != 0) WCMD_output ("\n\n");
204 WCMD_output ("Directory of %s\n\n", real_path);
207 for (i=0; i<entry_count; i++) {
209 /* /L convers all names to lower case */
211 char *p = (fd+i)->cFileName;
212 while ( (*p = tolower(*p)) ) ++p;
215 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
216 FileTimeToSystemTime (&ft, &st);
217 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
219 GetTimeFormat (0, TIME_NOSECONDS, &st,
220 NULL, timestring, sizeof(timestring));
224 tmp_width = cur_width;
225 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
226 WCMD_output ("[%s]", (fd+i)->cFileName);
228 tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
230 WCMD_output ("%s", (fd+i)->cFileName);
231 tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
233 file_size.u.LowPart = (fd+i)->nFileSizeLow;
234 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
235 byte_count.QuadPart += file_size.QuadPart;
237 cur_width = cur_width + widest;
239 if ((cur_width + widest) > max_width) {
243 WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
246 } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
250 WCMD_output ("%10s %8s <DIR> %s\n",
251 datestring, timestring, (fd+i)->cFileName);
253 if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
254 (strcmp((fd+i)->cFileName, "..") == 0))) {
255 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
261 file_size.u.LowPart = (fd+i)->nFileSizeLow;
262 file_size.u.HighPart = (fd+i)->nFileSizeHigh;
263 byte_count.QuadPart += file_size.QuadPart;
265 WCMD_output ("%10s %8s %10s %s\n",
266 datestring, timestring,
267 WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
269 WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
274 if (wide && cur_width>0) {
279 if (file_count == 1) {
280 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
283 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
286 byte_total = byte_total + byte_count.QuadPart;
287 file_total = file_total + file_count;
288 dir_total = dir_total + dir_count;
291 if (dir_count == 1) WCMD_output ("1 directory ");
292 else WCMD_output ("%8d directories", dir_count);
294 for (i=0; i<entry_count; i++) {
296 ((fd+i)->cFileName[0] != '.') &&
297 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
299 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
301 p = strrchr (search_path, '\\');
302 lstrcpyn (string, search_path, (p-search_path+2));
303 lstrcat (string, (fd+i)->cFileName);
305 WCMD_list_directory (string, 1);
312 /*****************************************************************************
315 * Convert a 64-bit number into a character string, with commas every three digits.
316 * Result is returned in a static string overwritten with each call.
317 * FIXME: There must be a better algorithm!
320 char * WCMD_filesize64 (ULONGLONG n) {
325 static char buff[32];
330 if ((++i)%3 == 1) *p++ = ',';
341 /*****************************************************************************
344 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
347 char * WCMD_strrev (char *buff) {
353 for (i=0; i<r/2; i++) {
355 buff[i] = buff[r-i-1];
362 int WCMD_dir_sort (const void *a, const void *b)
364 return (lstrcmpi(((const WIN32_FIND_DATA *)a)->cFileName,
365 ((const WIN32_FIND_DATA *)b)->cFileName));