ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / programs / wcmd / directory.c
1 /*
2  * WCMD - Wine-compatible command line interface - Directory functions.
3  *
4  * Copyright (C) 1999 D A Pickles
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * NOTES:
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.
26  */
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "wcmd.h"
31
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);
36
37
38 extern char nyi[];
39 extern char newline[];
40 extern char version_string[];
41 extern char anykey[];
42 extern int echo_mode;
43 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
44 extern DWORD errorlevel;
45
46 int file_total, dir_total, line_count, page_mode, recurse, wide, bare,
47     max_width;
48 ULONGLONG byte_total;
49
50 /*****************************************************************************
51  * WCMD_directory
52  *
53  * List a file directory.
54  *
55  */
56
57 void WCMD_directory () {
58
59 char path[MAX_PATH], drive[8];
60 int status;
61 ULARGE_INTEGER avail, total, free;
62 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
63
64   line_count = 5;
65   byte_total = 0;
66   file_total = dir_total = 0;
67
68   /* Handle args */
69   page_mode = (strstr(quals, "/P") != NULL);
70   recurse = (strstr(quals, "/S") != NULL);
71   wide    = (strstr(quals, "/W") != NULL);
72   bare    = (strstr(quals, "/B") != NULL);
73
74   /* Handle conflicting args and initialization */
75   if (bare) wide = FALSE;
76
77   if (wide) {
78      GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo);
79      max_width = consoleInfo.dwSize.X;
80   }
81
82   if (param1[0] == '\0') strcpy (param1, ".");
83   status = GetFullPathName (param1, sizeof(path), path, NULL);
84   if (!status) {
85     WCMD_print_error();
86     return;
87   }
88   lstrcpyn (drive, path, 3);
89
90   if (!bare) {
91      status = WCMD_volume (0, drive);
92      if (!status) {
93        return;
94      }
95   }
96
97   WCMD_list_directory (path, 0);
98   lstrcpyn (drive, path, 4);
99   GetDiskFreeSpaceEx (drive, &avail, &total, &free);
100
101   if (!bare) {
102      if (recurse) {
103        WCMD_output ("\n\n     Total files listed:\n%8d files%25s bytes\n",
104             file_total, WCMD_filesize64 (byte_total));
105        WCMD_output ("%8d directories %18s bytes free\n\n",
106             dir_total, WCMD_filesize64 (free.QuadPart));
107      } else {
108        WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
109      }
110   }
111 }
112
113 /*****************************************************************************
114  * WCMD_list_directory
115  *
116  * List a single file directory. This function (and those below it) can be called
117  * recursively when the /S switch is used.
118  *
119  * FIXME: Entries sorted by name only. Should we support DIRCMD??
120  * FIXME: Assumes 24-line display for the /P qualifier.
121  * FIXME: Other command qualifiers not supported.
122  * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
123  */
124
125 void WCMD_list_directory (char *search_path, int level) {
126
127 char string[1024], datestring[32], timestring[32];
128 char mem_err[] = "Memory Allocation Error";
129 char *p;
130 char real_path[MAX_PATH];
131 DWORD count;
132 WIN32_FIND_DATA *fd;
133 FILETIME ft;
134 SYSTEMTIME st;
135 HANDLE hff;
136 int status, dir_count, file_count, entry_count, i, widest, linesout, cur_width, tmp_width;
137 ULARGE_INTEGER byte_count, file_size;
138
139   dir_count = 0;
140   file_count = 0;
141   entry_count = 0;
142   byte_count.QuadPart = 0;
143   widest = 0;
144   linesout = 0;
145   cur_width = 0;
146
147 /*
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.
151  */
152
153   if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
154     status = GetFileAttributes (search_path);
155     if ((status != -1) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
156       if (search_path[strlen(search_path)-1] == '\\') {
157         strcat (search_path, "*");
158       }
159       else {
160         strcat (search_path, "\\*");
161       }
162     }
163   }
164
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));
169
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);
175     WCMD_print_error ();
176     free (fd);
177     return;
178   }
179   do {
180     entry_count++;
181
182     /* Keep running track of longest filename for wide output */
183     if (wide) {
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;
187     }
188
189     fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
190     if (fd == NULL) {
191       FindClose (hff);
192       WCMD_output (mem_err);
193        return;
194     }
195   } while (FindNextFile(hff, (fd+entry_count)) != 0);
196   FindClose (hff);
197
198   /* Sort the list of files */
199   qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
200
201   /* Output the results */
202   if (!bare) {
203      if (level != 0) WCMD_output ("\n\n");
204      WCMD_output ("Directory of %s\n\n", real_path);
205      if (page_mode) {
206        line_count += 2;
207        if (line_count > 23) {
208          line_count = 0;
209          WCMD_output (anykey);
210          ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
211        }
212      }
213   }
214
215   for (i=0; i<entry_count; i++) {
216     FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
217     FileTimeToSystemTime (&ft, &st);
218     GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
219                 sizeof(datestring));
220     GetTimeFormat (0, TIME_NOSECONDS, &st,
221                 NULL, timestring, sizeof(timestring));
222
223     if (wide) {
224
225       tmp_width = cur_width;
226       if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
227           WCMD_output ("[");
228           WCMD_output ("%s", (fd+i)->cFileName);
229           WCMD_output ("]");
230           dir_count++;
231           tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
232       } else {
233           WCMD_output ("%s", (fd+i)->cFileName);
234           tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
235           file_count++;
236 #ifndef NONAMELESSSTRUCT
237           file_size.LowPart = (fd+i)->nFileSizeLow;
238           file_size.HighPart = (fd+i)->nFileSizeHigh;
239 #else
240           file_size.s.LowPart = (fd+i)->nFileSizeLow;
241           file_size.s.HighPart = (fd+i)->nFileSizeHigh;
242 #endif
243       byte_count.QuadPart += file_size.QuadPart;
244       }
245       cur_width = cur_width + widest;
246
247       if ((cur_width + widest) > max_width) {
248           WCMD_output ("\n");
249           cur_width = 0;
250           linesout++;
251       } else {
252           WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
253       }
254
255     } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
256       dir_count++;
257
258       if (!bare) {
259          WCMD_output ("%10s  %8s  <DIR>         %s\n",
260              datestring, timestring, (fd+i)->cFileName);
261          linesout++;
262       } else {
263          if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
264                (strcmp((fd+i)->cFileName, "..") == 0))) {
265             WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
266             linesout++;
267          }
268       }
269     }
270     else {
271       file_count++;
272 #ifndef NONAMELESSSTRUCT
273       file_size.LowPart = (fd+i)->nFileSizeLow;
274       file_size.HighPart = (fd+i)->nFileSizeHigh;
275 #else
276       file_size.s.LowPart = (fd+i)->nFileSizeLow;
277       file_size.s.HighPart = (fd+i)->nFileSizeHigh;
278 #endif
279       byte_count.QuadPart += file_size.QuadPart;
280           if (!bare) {
281          WCMD_output ("%10s  %8s    %10s  %s\n",
282              datestring, timestring,
283                  WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
284          linesout++;
285       } else {
286          WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
287          linesout++;
288       }
289     }
290     if (page_mode) {
291       line_count = line_count + linesout;
292       linesout = 0;
293       if (line_count > 23) {
294         line_count = 0;
295         WCMD_output (anykey);
296         ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
297       }
298     }
299   }
300
301   if (wide && cur_width>0) {
302       WCMD_output ("\n");
303       if (page_mode) {
304         if (++line_count > 23) {
305           line_count = 0;
306           WCMD_output (anykey);
307           ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
308         }
309       }
310   }
311
312   if (!bare) {
313      if (file_count == 1) {
314        WCMD_output ("       1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
315      }
316      else {
317        WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
318      }
319      if (page_mode) {
320        if (++line_count > 23) {
321          line_count = 0;
322          WCMD_output (anykey);
323          ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
324        }
325      }
326   }
327   byte_total = byte_total + byte_count.QuadPart;
328   file_total = file_total + file_count;
329   dir_total = dir_total + dir_count;
330
331   if (!bare) {
332      if (dir_count == 1) WCMD_output ("1 directory         ");
333      else WCMD_output ("%8d directories", dir_count);
334      if (page_mode) {
335        if (++line_count > 23) {
336          line_count = 0;
337          WCMD_output (anykey);
338          ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
339        }
340      }
341   }
342   for (i=0; i<entry_count; i++) {
343     if ((recurse) &&
344           ((fd+i)->cFileName[0] != '.') &&
345           ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
346 #if 0
347       GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
348 #endif
349       p = strrchr (search_path, '\\');
350       lstrcpyn (string, search_path, (p-search_path+2));
351       lstrcat (string, (fd+i)->cFileName);
352       lstrcat (string, p);
353       WCMD_list_directory (string, 1);
354     }
355   }
356   free (fd);
357   return;
358 }
359
360 /*****************************************************************************
361  * WCMD_filesize64
362  *
363  * Convert a 64-bit number into a character string, with commas every three digits.
364  * Result is returned in a static string overwritten with each call.
365  * FIXME: There must be a better algorithm!
366  */
367
368 char * WCMD_filesize64 (ULONGLONG n) {
369
370 ULONGLONG q;
371 unsigned int r, i;
372 char *p;
373 static char buff[32];
374
375   p = buff;
376   i = -3;
377   do {
378     if ((++i)%3 == 1) *p++ = ',';
379     q = n / 10;
380     r = n - (q * 10);
381     *p++ = r + '0';
382     *p = '\0';
383     n = q;
384   } while (n != 0);
385   WCMD_strrev (buff);
386   return buff;
387 }
388
389 /*****************************************************************************
390  * WCMD_strrev
391  *
392  * Reverse a character string in-place (strrev() is not available under unixen :-( ).
393  */
394
395 char * WCMD_strrev (char *buff) {
396
397 int r, i;
398 char b;
399
400   r = lstrlen (buff);
401   for (i=0; i<r/2; i++) {
402     b = buff[i];
403     buff[i] = buff[r-i-1];
404     buff[r-i-1] = b;
405   }
406   return (buff);
407 }
408
409
410 int WCMD_dir_sort (const void *a, const void *b) {
411
412   return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName,
413         ((WIN32_FIND_DATA *)b)->cFileName));
414 }