ULARGE_INTEGER may have a dummy struct name.
[wine] / programs / wcmd / directory.c
1 /*
2  * WCMD - Wine-compatible command line interface - Directory functions.
3  *
4  * (C) 1999 D A Pickles
5  *
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.
9  */
10
11 /*
12  * FIXME:
13  * - DIR /S fails if the starting directory is not the current default.
14  */
15
16 #include "wcmd.h"
17
18 int WCMD_dir_sort (const void *a, const void *b);
19 void WCMD_list_directory (char *path, int level);
20 char * WCMD_filesize64 (__int64 free);
21 char * WCMD_strrev (char *buff);
22
23
24 extern char nyi[];
25 extern char newline[];
26 extern char version_string[];
27 extern char anykey[];
28 extern int echo_mode;
29 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
30 extern DWORD errorlevel;
31
32 int file_total, dir_total, line_count, page_mode, recurse;
33 __int64 byte_total;
34
35 /*****************************************************************************
36  * WCMD_directory
37  *
38  * List a file directory.
39  * FIXME: /S switch only works for the current directory
40  *
41  */
42
43 void WCMD_directory () {
44
45 char path[MAX_PATH], drive[8];
46 int status;
47 ULARGE_INTEGER avail, total, free;
48
49   line_count = 5;
50   page_mode = (strstr(quals, "/P") != NULL);
51   recurse = (strstr(quals, "/S") != NULL);
52   if (param1[0] == '\0') strcpy (param1, ".");
53   status = GetFullPathName (param1, sizeof(path), path, NULL);
54   if (!status) {
55     WCMD_print_error();
56     return;
57   }
58   lstrcpyn (drive, path, 3);
59   status = WCMD_volume (0, drive);
60   if (!status) {
61     return;
62   }
63   WCMD_list_directory (path, 0);
64   lstrcpyn (drive, path, 4);
65   GetDiskFreeSpaceEx (drive, &avail, &total, &free);
66   WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
67   if (recurse) {
68     WCMD_output ("Total files listed:\n%8d files%25s bytes\n%8d directories\n\n",
69          file_total, WCMD_filesize64 (byte_total), dir_total);
70   }
71 }
72
73 /*****************************************************************************
74  * WCMD_list_directory
75  *
76  * List a single file directory. This function (and those below it) can be called
77  * recursively when the /S switch is used.
78  *
79  * FIXME: Entries sorted by name only. Should we support DIRCMD??
80  * FIXME: Assumes 24-line display for the /P qualifier.
81  * FIXME: Other command qualifiers not supported.
82  * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level. 
83  */
84
85 void WCMD_list_directory (char *search_path, int level) {
86
87 char string[1024], datestring[32], timestring[32];
88 char mem_err[] = "Memory Allocation Error";
89 char *p;
90 DWORD count;
91 WIN32_FIND_DATA *fd;
92 FILETIME ft;
93 SYSTEMTIME st;
94 HANDLE hff;
95 int status, dir_count, file_count, entry_count, i;
96 ULARGE_INTEGER byte_count, file_size;
97
98   dir_count = 0;
99   file_count = 0;
100   entry_count = 0;
101   byte_count.QuadPart = 0;
102
103 /*
104  *  If the path supplied does not include a wildcard, and the endpoint of the
105  *  path references a directory, we need to list the *contents* of that
106  *  directory not the directory file itself.
107  */
108
109   if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
110     status = GetFileAttributes (search_path);
111     if ((status != -1) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
112       if (search_path[strlen(search_path)-1] == '\\') {
113         strcat (search_path, "*");
114       }
115       else {
116         strcat (search_path, "\\*");
117       }
118     }
119   }
120
121   fd = malloc (sizeof(WIN32_FIND_DATA));
122   hff = FindFirstFile (search_path, fd);
123   if (hff == INVALID_HANDLE_VALUE) {
124     SetLastError (ERROR_FILE_NOT_FOUND);
125     WCMD_print_error ();
126     free (fd);
127     return;
128   }
129   do {
130     entry_count++;
131     fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
132     if (fd == NULL) {
133       FindClose (hff);
134       WCMD_output (mem_err);
135        return;
136     }
137   } while (FindNextFile(hff, (fd+entry_count)) != 0);
138   FindClose (hff);
139   qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
140   if (level != 0) WCMD_output ("\n\n");
141   WCMD_output ("Directory of %s\n\n", search_path);
142   if (page_mode) {
143     line_count += 2;
144     if (line_count > 23) {
145       line_count = 0;
146       WCMD_output (anykey);
147       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
148     }
149   }
150   for (i=0; i<entry_count; i++) {
151     FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
152     FileTimeToSystemTime (&ft, &st);
153     GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
154                 sizeof(datestring));
155     GetTimeFormat (0, TIME_NOSECONDS, &st,
156                 NULL, timestring, sizeof(timestring));
157     if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
158       dir_count++;
159       WCMD_output ("%8s  %8s   <DIR>        %s\n",
160           datestring, timestring, (fd+i)->cFileName);
161     }
162     else {
163       file_count++;
164 #ifndef NONAMELESSSTRUCT
165       file_size.LowPart = (fd+i)->nFileSizeLow;
166       file_size.HighPart = (fd+i)->nFileSizeHigh;
167 #else
168       file_size.s.LowPart = (fd+i)->nFileSizeLow;
169       file_size.s.HighPart = (fd+i)->nFileSizeHigh;
170 #endif
171       byte_count.QuadPart += file_size.QuadPart;
172       WCMD_output ("%8s  %8s    %10s  %s\n",
173           datestring, timestring,
174           WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
175     }
176     if (page_mode) {
177       if (++line_count > 23) {
178         line_count = 0;
179         WCMD_output (anykey);
180         ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
181       }
182     }
183   }
184   if (file_count == 1) {
185     WCMD_output ("       1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
186   }
187   else {
188     WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
189   }
190   if (page_mode) {
191     if (++line_count > 23) {
192       line_count = 0;
193       WCMD_output (anykey);
194       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
195     }
196   }
197   byte_total = byte_total + byte_count.QuadPart;
198   file_total = file_total + file_count;
199   dir_total = dir_total + dir_count;
200   if (dir_count == 1) WCMD_output ("1 directory         ");
201   else WCMD_output ("%8d directories", dir_count);
202   if (page_mode) {
203     if (++line_count > 23) {
204       line_count = 0;
205       WCMD_output (anykey);
206       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
207     }
208   }
209   for (i=0; i<entry_count; i++) {
210     if ((recurse) &&
211           ((fd+i)->cFileName[0] != '.') &&
212           ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
213 #if 0
214       GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
215 #endif
216       p = strrchr (search_path, '\\');
217       lstrcpyn (string, search_path, (p-search_path+2));
218       lstrcat (string, (fd+i)->cFileName);
219       lstrcat (string, p);
220       WCMD_list_directory (string, 1);
221     }
222   }
223   free (fd);
224   return;
225 }
226
227 /*****************************************************************************
228  * WCMD_filesize64
229  *
230  * Convert a 64-bit number into a character string, with commas every three digits.
231  * Result is returned in a static string overwritten with each call.
232  * FIXME: There must be a better algorithm!
233  */
234
235 char * WCMD_filesize64 (__int64 n) {
236
237 __int64 q;
238 int r, i;
239 char *p;
240 static char buff[32];
241
242   p = buff;
243   i = -3;
244   do {
245     if ((++i)%3 == 1) *p++ = ',';
246     q = n / 10;
247     r = n - (q * 10);
248     *p++ = r + '0';
249     *p = '\0';
250     n = q;
251   } while (n != 0);
252   WCMD_strrev (buff);
253   return buff;
254 }
255
256 /*****************************************************************************
257  * WCMD_strrev
258  *
259  * Reverse a character string in-place (strrev() is not available under unixen :-( ).
260  */
261
262 char * WCMD_strrev (char *buff) {
263
264 int r, i;
265 char b;
266
267   r = lstrlen (buff);
268   for (i=0; i<r/2; i++) {
269     b = buff[i];
270     buff[i] = buff[r-i-1];
271     buff[r-i-1] = b;
272   }
273   return (buff);
274 }
275
276
277 int WCMD_dir_sort (const void *a, const void *b) {
278
279   return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName,
280         ((WIN32_FIND_DATA *)b)->cFileName));
281 }
282