2 * WCMD - Wine-compatible command line interface - Directory functions.
6 * On entry, global variables quals, param1, param2 contain
7 * the qualifiers (uppercased and concatenated) and parameters entered, with
8 * environment-variable and batch parameter substitution already done.
13 int WCMD_dir_sort (const void *a, const void *b);
14 void WCMD_list_directory (char *path, int level);
15 char * WCMD_filesize64 (__int64 free);
16 char * WCMD_strrev (char *buff);
20 extern char newline[];
21 extern char version_string[];
24 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
25 extern DWORD errorlevel;
27 int file_total, dir_total, line_count, page_mode, recurse;
30 /*****************************************************************************
33 * List a file directory.
37 void WCMD_directory () {
39 char path[MAX_PATH], drive[8];
41 ULARGE_INTEGER avail, total, free;
45 file_total = dir_total = 0;
46 page_mode = (strstr(quals, "/P") != NULL);
47 recurse = (strstr(quals, "/S") != NULL);
48 if (param1[0] == '\0') strcpy (param1, ".");
49 status = GetFullPathName (param1, sizeof(path), path, NULL);
54 lstrcpyn (drive, path, 3);
55 status = WCMD_volume (0, drive);
59 WCMD_list_directory (path, 0);
60 lstrcpyn (drive, path, 4);
61 GetDiskFreeSpaceEx (drive, &avail, &total, &free);
62 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
64 WCMD_output ("Total files listed:\n%8d files%25s bytes\n%8d directories\n\n",
65 file_total, WCMD_filesize64 (byte_total), dir_total);
69 /*****************************************************************************
72 * List a single file directory. This function (and those below it) can be called
73 * recursively when the /S switch is used.
75 * FIXME: Entries sorted by name only. Should we support DIRCMD??
76 * FIXME: Assumes 24-line display for the /P qualifier.
77 * FIXME: Other command qualifiers not supported.
78 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
81 void WCMD_list_directory (char *search_path, int level) {
83 char string[1024], datestring[32], timestring[32];
84 char mem_err[] = "Memory Allocation Error";
91 int status, dir_count, file_count, entry_count, i;
92 ULARGE_INTEGER byte_count, file_size;
97 byte_count.QuadPart = 0;
100 * If the path supplied does not include a wildcard, and the endpoint of the
101 * path references a directory, we need to list the *contents* of that
102 * directory not the directory file itself.
105 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
106 status = GetFileAttributes (search_path);
107 if ((status != -1) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
108 if (search_path[strlen(search_path)-1] == '\\') {
109 strcat (search_path, "*");
112 strcat (search_path, "\\*");
117 fd = malloc (sizeof(WIN32_FIND_DATA));
118 hff = FindFirstFile (search_path, fd);
119 if (hff == INVALID_HANDLE_VALUE) {
120 SetLastError (ERROR_FILE_NOT_FOUND);
127 fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
130 WCMD_output (mem_err);
133 } while (FindNextFile(hff, (fd+entry_count)) != 0);
135 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
136 if (level != 0) WCMD_output ("\n\n");
137 WCMD_output ("Directory of %s\n\n", search_path);
140 if (line_count > 23) {
142 WCMD_output (anykey);
143 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
146 for (i=0; i<entry_count; i++) {
147 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
148 FileTimeToSystemTime (&ft, &st);
149 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
151 GetTimeFormat (0, TIME_NOSECONDS, &st,
152 NULL, timestring, sizeof(timestring));
153 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
155 WCMD_output ("%8s %8s <DIR> %s\n",
156 datestring, timestring, (fd+i)->cFileName);
160 #ifndef NONAMELESSSTRUCT
161 file_size.LowPart = (fd+i)->nFileSizeLow;
162 file_size.HighPart = (fd+i)->nFileSizeHigh;
164 file_size.s.LowPart = (fd+i)->nFileSizeLow;
165 file_size.s.HighPart = (fd+i)->nFileSizeHigh;
167 byte_count.QuadPart += file_size.QuadPart;
168 WCMD_output ("%8s %8s %10s %s\n",
169 datestring, timestring,
170 WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
173 if (++line_count > 23) {
175 WCMD_output (anykey);
176 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
180 if (file_count == 1) {
181 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
184 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
187 if (++line_count > 23) {
189 WCMD_output (anykey);
190 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
193 byte_total = byte_total + byte_count.QuadPart;
194 file_total = file_total + file_count;
195 dir_total = dir_total + dir_count;
196 if (dir_count == 1) WCMD_output ("1 directory ");
197 else WCMD_output ("%8d directories", dir_count);
199 if (++line_count > 23) {
201 WCMD_output (anykey);
202 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
205 for (i=0; i<entry_count; i++) {
207 ((fd+i)->cFileName[0] != '.') &&
208 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
210 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
212 p = strrchr (search_path, '\\');
213 lstrcpyn (string, search_path, (p-search_path+2));
214 lstrcat (string, (fd+i)->cFileName);
216 WCMD_list_directory (string, 1);
223 /*****************************************************************************
226 * Convert a 64-bit number into a character string, with commas every three digits.
227 * Result is returned in a static string overwritten with each call.
228 * FIXME: There must be a better algorithm!
231 char * WCMD_filesize64 (__int64 n) {
236 static char buff[32];
241 if ((++i)%3 == 1) *p++ = ',';
252 /*****************************************************************************
255 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
258 char * WCMD_strrev (char *buff) {
264 for (i=0; i<r/2; i++) {
266 buff[i] = buff[r-i-1];
273 int WCMD_dir_sort (const void *a, const void *b) {
275 return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName,
276 ((WIN32_FIND_DATA *)b)->cFileName));