Fixed color definition (bg and fg were swapped).
[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 #include "wcmd.h"
12
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);
17
18
19 extern char nyi[];
20 extern char newline[];
21 extern char version_string[];
22 extern char anykey[];
23 extern int echo_mode;
24 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
25 extern DWORD errorlevel;
26
27 int file_total, dir_total, line_count, page_mode, recurse;
28 __int64 byte_total;
29
30 /*****************************************************************************
31  * WCMD_directory
32  *
33  * List a file directory.
34  *
35  */
36
37 void WCMD_directory () {
38
39 char path[MAX_PATH], drive[8];
40 int status;
41 ULARGE_INTEGER avail, total, free;
42
43   line_count = 5;
44   byte_total = 0;
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);
50   if (!status) {
51     WCMD_print_error();
52     return;
53   }
54   lstrcpyn (drive, path, 3);
55   status = WCMD_volume (0, drive);
56   if (!status) {
57     return;
58   }
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));
63   if (recurse) {
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);
66   }
67 }
68
69 /*****************************************************************************
70  * WCMD_list_directory
71  *
72  * List a single file directory. This function (and those below it) can be called
73  * recursively when the /S switch is used.
74  *
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. 
79  */
80
81 void WCMD_list_directory (char *search_path, int level) {
82
83 char string[1024], datestring[32], timestring[32];
84 char mem_err[] = "Memory Allocation Error";
85 char *p;
86 DWORD count;
87 WIN32_FIND_DATA *fd;
88 FILETIME ft;
89 SYSTEMTIME st;
90 HANDLE hff;
91 int status, dir_count, file_count, entry_count, i;
92 ULARGE_INTEGER byte_count, file_size;
93
94   dir_count = 0;
95   file_count = 0;
96   entry_count = 0;
97   byte_count.QuadPart = 0;
98
99 /*
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.
103  */
104
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, "*");
110       }
111       else {
112         strcat (search_path, "\\*");
113       }
114     }
115   }
116
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);
121     WCMD_print_error ();
122     free (fd);
123     return;
124   }
125   do {
126     entry_count++;
127     fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
128     if (fd == NULL) {
129       FindClose (hff);
130       WCMD_output (mem_err);
131        return;
132     }
133   } while (FindNextFile(hff, (fd+entry_count)) != 0);
134   FindClose (hff);
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);
138   if (page_mode) {
139     line_count += 2;
140     if (line_count > 23) {
141       line_count = 0;
142       WCMD_output (anykey);
143       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
144     }
145   }
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,
150                 sizeof(datestring));
151     GetTimeFormat (0, TIME_NOSECONDS, &st,
152                 NULL, timestring, sizeof(timestring));
153     if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
154       dir_count++;
155       WCMD_output ("%8s  %8s   <DIR>        %s\n",
156           datestring, timestring, (fd+i)->cFileName);
157     }
158     else {
159       file_count++;
160 #ifndef NONAMELESSSTRUCT
161       file_size.LowPart = (fd+i)->nFileSizeLow;
162       file_size.HighPart = (fd+i)->nFileSizeHigh;
163 #else
164       file_size.s.LowPart = (fd+i)->nFileSizeLow;
165       file_size.s.HighPart = (fd+i)->nFileSizeHigh;
166 #endif
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);
171     }
172     if (page_mode) {
173       if (++line_count > 23) {
174         line_count = 0;
175         WCMD_output (anykey);
176         ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
177       }
178     }
179   }
180   if (file_count == 1) {
181     WCMD_output ("       1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
182   }
183   else {
184     WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
185   }
186   if (page_mode) {
187     if (++line_count > 23) {
188       line_count = 0;
189       WCMD_output (anykey);
190       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
191     }
192   }
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);
198   if (page_mode) {
199     if (++line_count > 23) {
200       line_count = 0;
201       WCMD_output (anykey);
202       ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
203     }
204   }
205   for (i=0; i<entry_count; i++) {
206     if ((recurse) &&
207           ((fd+i)->cFileName[0] != '.') &&
208           ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
209 #if 0
210       GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
211 #endif
212       p = strrchr (search_path, '\\');
213       lstrcpyn (string, search_path, (p-search_path+2));
214       lstrcat (string, (fd+i)->cFileName);
215       lstrcat (string, p);
216       WCMD_list_directory (string, 1);
217     }
218   }
219   free (fd);
220   return;
221 }
222
223 /*****************************************************************************
224  * WCMD_filesize64
225  *
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!
229  */
230
231 char * WCMD_filesize64 (__int64 n) {
232
233 __int64 q;
234 int r, i;
235 char *p;
236 static char buff[32];
237
238   p = buff;
239   i = -3;
240   do {
241     if ((++i)%3 == 1) *p++ = ',';
242     q = n / 10;
243     r = n - (q * 10);
244     *p++ = r + '0';
245     *p = '\0';
246     n = q;
247   } while (n != 0);
248   WCMD_strrev (buff);
249   return buff;
250 }
251
252 /*****************************************************************************
253  * WCMD_strrev
254  *
255  * Reverse a character string in-place (strrev() is not available under unixen :-( ).
256  */
257
258 char * WCMD_strrev (char *buff) {
259
260 int r, i;
261 char b;
262
263   r = lstrlen (buff);
264   for (i=0; i<r/2; i++) {
265     b = buff[i];
266     buff[i] = buff[r-i-1];
267     buff[r-i-1] = b;
268   }
269   return (buff);
270 }
271
272
273 int WCMD_dir_sort (const void *a, const void *b) {
274
275   return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName,
276         ((WIN32_FIND_DATA *)b)->cFileName));
277 }
278