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 * - 32-bit limit on individual file sizes (directories and free space are 64-bit)
14 * - DIR /S fails if the starting directory is not the current default.
19 int WCMD_dir_sort (const void *a, const void *b);
20 void WCMD_list_directory (char *path, int level);
21 char * WCMD_filesize64 (__int64 n);
22 char * WCMD_filesize32 (int n);
23 char * WCMD_strrev (char *buff);
27 extern char newline[];
28 extern char version_string[];
31 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
32 extern DWORD errorlevel;
34 int file_total, dir_total, line_count, page_mode, recurse;
37 /*****************************************************************************
40 * List a file directory.
41 * FIXME: /S switch only works for the current directory
45 void WCMD_directory () {
47 char path[MAX_PATH], drive[8];
50 DWORD spc, bps, fc, capacity;
53 page_mode = (strstr(quals, "/P") != NULL);
54 recurse = (strstr(quals, "/S") != NULL);
55 if (param1[0] == '\0') strcpy (param1, ".");
56 status = GetFullPathName (param1, sizeof(path), path, NULL);
61 lstrcpyn (drive, path, 3);
62 status = WCMD_volume (0, drive);
66 WCMD_list_directory (path, 0);
67 lstrcpyn (drive, path, 4);
68 GetDiskFreeSpace (drive, &spc, &bps, &fc, &capacity);
69 free_space = bps * spc * fc;
70 WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free_space));
72 WCMD_output ("Total files listed:\n%8d files%25s bytes\n%8d directories\n\n",
73 file_total, WCMD_filesize64 (byte_total), dir_total);
77 /*****************************************************************************
80 * List a single file directory. This function (and those below it) can be called
81 * recursively when the /S switch is used.
83 * FIXME: Assumes individual files are less than 2**32 bytes.
84 * FIXME: Entries sorted by name only. Should we support DIRCMD??
85 * FIXME: Assumes 24-line display for the /P qualifier.
86 * FIXME: Other command qualifiers not supported.
87 * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
90 void WCMD_list_directory (char *search_path, int level) {
92 char string[1024], datestring[32], timestring[32];
93 char mem_err[] = "Memory Allocation Error";
100 int status, dir_count, file_count, entry_count, i;
109 * If the path supplied does not include a wildcard, and the endpoint of the
110 * path references a directory, we need to list the *contents* of that
111 * directory not the directory file itself.
114 if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
115 status = GetFileAttributes (search_path);
116 if ((status != -1) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
117 if (search_path[strlen(search_path)-1] == '\\') {
118 strcat (search_path, "*");
121 strcat (search_path, "\\*");
126 fd = malloc (sizeof(WIN32_FIND_DATA));
127 hff = FindFirstFile (search_path, fd);
128 if (hff == INVALID_HANDLE_VALUE) {
129 SetLastError (ERROR_FILE_NOT_FOUND);
136 fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
139 WCMD_output (mem_err);
142 } while (FindNextFile(hff, (fd+entry_count)) != 0);
144 qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
145 if (level != 0) WCMD_output ("\n\n");
146 WCMD_output ("Directory of %s\n\n", search_path);
149 if (line_count > 23) {
151 WCMD_output (anykey);
152 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
155 for (i=0; i<entry_count; i++) {
156 FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
157 FileTimeToSystemTime (&ft, &st);
158 GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
160 GetTimeFormat (0, TIME_NOSECONDS, &st,
161 NULL, timestring, sizeof(timestring));
162 if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
164 WCMD_output ("%8s %8s <DIR> %s\n",
165 datestring, timestring, (fd+i)->cFileName);
169 byte_count += (fd+i)->nFileSizeLow;
170 WCMD_output ("%8s %8s %10s %s\n",
171 datestring, timestring,
172 WCMD_filesize32((fd+i)->nFileSizeLow), (fd+i)->cFileName);
175 if (++line_count > 23) {
177 WCMD_output (anykey);
178 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
182 if (file_count == 1) {
183 WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count));
186 WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count));
189 if (++line_count > 23) {
191 WCMD_output (anykey);
192 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
195 byte_total = byte_total + byte_count;
196 file_total = file_total + file_count;
197 dir_total = dir_total + dir_count;
198 if (dir_count == 1) WCMD_output ("1 directory ");
199 else WCMD_output ("%8d directories", dir_count);
201 if (++line_count > 23) {
203 WCMD_output (anykey);
204 ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
207 for (i=0; i<entry_count; i++) {
209 ((fd+i)->cFileName[0] != '.') &&
210 ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
212 GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
214 p = strrchr (search_path, '\\');
215 lstrcpyn (string, search_path, (p-search_path+2));
216 lstrcat (string, (fd+i)->cFileName);
218 WCMD_list_directory (string, 1);
225 /*****************************************************************************
228 * Convert a 64-bit number into a character string, with commas every three digits.
229 * Result is returned in a static string overwritten with each call.
230 * FIXME: There must be a better algorithm!
233 char * WCMD_filesize64 (__int64 n) {
238 static char buff[32];
243 if ((++i)%3 == 1) *p++ = ',';
254 /*****************************************************************************
257 * Convert a 32-bit number into a character string, with commas every three digits.
258 * Result is returned in a static string overwritten with each call.
259 * FIXME: There must be a better algorithm!
262 char * WCMD_filesize32 (int n) {
266 static char buff1[16], buff2[16];
268 wsprintf (buff1, "%i", n);
273 for (i=0; i<r; i++) {
274 if ((i-2)%3 == 1) *q++ = ',';
282 /*****************************************************************************
285 * Reverse a character string in-place (strrev() is not available under unixen :-( ).
288 char * WCMD_strrev (char *buff) {
294 for (i=0; i<r/2; i++) {
296 buff[i] = buff[r-i-1];
303 int WCMD_dir_sort (const void *a, const void *b) {
305 return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName,
306 ((WIN32_FIND_DATA *)b)->cFileName));