cmd: Remove unneeded NONAMELESS* defines and ifdefs.
[wine] / programs / cmd / directory.c
1 /*
2  * CMD - 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 WIN32_LEAN_AND_MEAN
29
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 int echo_mode;
39 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
40 extern DWORD errorlevel;
41
42 static int file_total, dir_total, recurse, wide, bare, max_width;
43 static ULONGLONG byte_total;
44
45 /*****************************************************************************
46  * WCMD_directory
47  *
48  * List a file directory.
49  *
50  */
51
52 void WCMD_directory (void) {
53
54   char path[MAX_PATH], drive[8];
55   int status, paged_mode;
56   ULARGE_INTEGER avail, total, free;
57   CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
58
59   byte_total = 0;
60   file_total = dir_total = 0;
61
62   /* Handle args */
63   paged_mode = (strstr(quals, "/P") != NULL);
64   recurse = (strstr(quals, "/S") != NULL);
65   wide    = (strstr(quals, "/W") != NULL);
66   bare    = (strstr(quals, "/B") != NULL);
67
68   /* Handle conflicting args and initialization */
69   if (bare) wide = FALSE;
70
71   if (wide) {
72       if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo))
73           max_width = consoleInfo.dwSize.X;
74       else
75           max_width = 80;
76   }
77   if (paged_mode) {
78      WCMD_enter_paged_mode();
79   }
80
81   if (param1[0] == '\0') strcpy (param1, ".");
82   status = GetFullPathName (param1, sizeof(path), path, NULL);
83   if (!status) {
84     WCMD_print_error();
85     if (paged_mode) WCMD_leave_paged_mode();
86     return;
87   }
88   lstrcpyn (drive, path, 3);
89
90   if (!bare) {
91      status = WCMD_volume (0, drive);
92      if (!status) {
93          if (paged_mode) WCMD_leave_paged_mode();
94        return;
95      }
96   }
97
98   WCMD_list_directory (path, 0);
99   lstrcpyn (drive, path, 4);
100   GetDiskFreeSpaceEx (drive, &avail, &total, &free);
101
102   if (!bare) {
103      if (recurse) {
104        WCMD_output ("\n\n     Total files listed:\n%8d files%25s bytes\n",
105             file_total, WCMD_filesize64 (byte_total));
106        WCMD_output ("%8d directories %18s bytes free\n\n",
107             dir_total, WCMD_filesize64 (free.QuadPart));
108      } else {
109        WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free.QuadPart));
110      }
111   }
112   if (paged_mode) WCMD_leave_paged_mode();
113 }
114
115 /*****************************************************************************
116  * WCMD_list_directory
117  *
118  * List a single file directory. This function (and those below it) can be called
119  * recursively when the /S switch is used.
120  *
121  * FIXME: Entries sorted by name only. Should we support DIRCMD??
122  * FIXME: Assumes 24-line display for the /P qualifier.
123  * FIXME: Other command qualifiers not supported.
124  * FIXME: DIR /S FILENAME fails if at least one matching file is not found in the top level.
125  */
126
127 void WCMD_list_directory (char *search_path, int level) {
128
129   char string[1024], datestring[32], timestring[32];
130   char *p;
131   char real_path[MAX_PATH];
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, 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   cur_width = 0;
145
146 /*
147  *  If the path supplied does not include a wildcard, and the endpoint of the
148  *  path references a directory, we need to list the *contents* of that
149  *  directory not the directory file itself.
150  */
151
152   if ((strchr(search_path, '*') == NULL) && (strchr(search_path, '%') == NULL)) {
153     status = GetFileAttributes (search_path);
154     if ((status != INVALID_FILE_ATTRIBUTES) && (status & FILE_ATTRIBUTE_DIRECTORY)) {
155       if (search_path[strlen(search_path)-1] == '\\') {
156         strcat (search_path, "*");
157       }
158       else {
159         strcat (search_path, "\\*");
160       }
161     }
162   }
163
164   /* Work out the actual current directory name */
165   p = strrchr (search_path, '\\');
166   memset(real_path, 0x00, sizeof(real_path));
167   lstrcpyn (real_path, search_path, (p-search_path+2));
168
169   /* Load all files into an in memory structure */
170   fd = malloc (sizeof(WIN32_FIND_DATA));
171   hff = FindFirstFile (search_path, fd);
172   if (hff == INVALID_HANDLE_VALUE) {
173     SetLastError (ERROR_FILE_NOT_FOUND);
174     WCMD_print_error ();
175     free (fd);
176     return;
177   }
178   do {
179     entry_count++;
180
181     /* Keep running track of longest filename for wide output */
182     if (wide) {
183        int tmpLen = strlen((fd+(entry_count-1))->cFileName) + 3;
184        if ((fd+(entry_count-1))->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) tmpLen = tmpLen + 2;
185        if (tmpLen > widest) widest = tmpLen;
186     }
187
188     fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA));
189     if (fd == NULL) {
190       FindClose (hff);
191       WCMD_output ("Memory Allocation Error");
192        return;
193     }
194   } while (FindNextFile(hff, (fd+entry_count)) != 0);
195   FindClose (hff);
196
197   /* Sort the list of files */
198   qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort);
199
200   /* Output the results */
201   if (!bare) {
202      if (level != 0) WCMD_output ("\n\n");
203      WCMD_output ("Directory of %s\n\n", real_path);
204   }
205
206   for (i=0; i<entry_count; i++) {
207     FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft);
208     FileTimeToSystemTime (&ft, &st);
209     GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring,
210                 sizeof(datestring));
211     GetTimeFormat (0, TIME_NOSECONDS, &st,
212                 NULL, timestring, sizeof(timestring));
213
214     if (wide) {
215
216       tmp_width = cur_width;
217       if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
218           WCMD_output ("[%s]", (fd+i)->cFileName);
219           dir_count++;
220           tmp_width = tmp_width + strlen((fd+i)->cFileName) + 2;
221       } else {
222           WCMD_output ("%s", (fd+i)->cFileName);
223           tmp_width = tmp_width + strlen((fd+i)->cFileName) ;
224           file_count++;
225           file_size.u.LowPart = (fd+i)->nFileSizeLow;
226           file_size.u.HighPart = (fd+i)->nFileSizeHigh;
227       byte_count.QuadPart += file_size.QuadPart;
228       }
229       cur_width = cur_width + widest;
230
231       if ((cur_width + widest) > max_width) {
232           WCMD_output ("\n");
233           cur_width = 0;
234       } else {
235           WCMD_output ("%*.s", (tmp_width - cur_width) ,"");
236       }
237
238     } else if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
239       dir_count++;
240
241       if (!bare) {
242          WCMD_output ("%10s  %8s  <DIR>         %s\n",
243              datestring, timestring, (fd+i)->cFileName);
244       } else {
245          if (!((strcmp((fd+i)->cFileName, ".") == 0) ||
246                (strcmp((fd+i)->cFileName, "..") == 0))) {
247             WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
248          }
249       }
250     }
251     else {
252       file_count++;
253       file_size.u.LowPart = (fd+i)->nFileSizeLow;
254       file_size.u.HighPart = (fd+i)->nFileSizeHigh;
255       byte_count.QuadPart += file_size.QuadPart;
256           if (!bare) {
257          WCMD_output ("%10s  %8s    %10s  %s\n",
258              datestring, timestring,
259                  WCMD_filesize64(file_size.QuadPart), (fd+i)->cFileName);
260       } else {
261          WCMD_output ("%s%s\n", recurse?real_path:"", (fd+i)->cFileName);
262       }
263     }
264   }
265
266   if (wide && cur_width>0) {
267       WCMD_output ("\n");
268   }
269
270   if (!bare) {
271      if (file_count == 1) {
272        WCMD_output ("       1 file %25s bytes\n", WCMD_filesize64 (byte_count.QuadPart));
273      }
274      else {
275        WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count.QuadPart));
276      }
277   }
278   byte_total = byte_total + byte_count.QuadPart;
279   file_total = file_total + file_count;
280   dir_total = dir_total + dir_count;
281
282   if (!bare) {
283      if (dir_count == 1) WCMD_output ("1 directory         ");
284      else WCMD_output ("%8d directories", dir_count);
285   }
286   for (i=0; i<entry_count; i++) {
287     if ((recurse) &&
288           ((fd+i)->cFileName[0] != '.') &&
289           ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
290 #if 0
291       GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL);
292 #endif
293       p = strrchr (search_path, '\\');
294       lstrcpyn (string, search_path, (p-search_path+2));
295       lstrcat (string, (fd+i)->cFileName);
296       lstrcat (string, p);
297       WCMD_list_directory (string, 1);
298     }
299   }
300   free (fd);
301   return;
302 }
303
304 /*****************************************************************************
305  * WCMD_filesize64
306  *
307  * Convert a 64-bit number into a character string, with commas every three digits.
308  * Result is returned in a static string overwritten with each call.
309  * FIXME: There must be a better algorithm!
310  */
311
312 char * WCMD_filesize64 (ULONGLONG n) {
313
314   ULONGLONG q;
315   unsigned int r, i;
316   char *p;
317   static char buff[32];
318
319   p = buff;
320   i = -3;
321   do {
322     if ((++i)%3 == 1) *p++ = ',';
323     q = n / 10;
324     r = n - (q * 10);
325     *p++ = r + '0';
326     *p = '\0';
327     n = q;
328   } while (n != 0);
329   WCMD_strrev (buff);
330   return buff;
331 }
332
333 /*****************************************************************************
334  * WCMD_strrev
335  *
336  * Reverse a character string in-place (strrev() is not available under unixen :-( ).
337  */
338
339 char * WCMD_strrev (char *buff) {
340
341   int r, i;
342   char b;
343
344   r = lstrlen (buff);
345   for (i=0; i<r/2; i++) {
346     b = buff[i];
347     buff[i] = buff[r-i-1];
348     buff[r-i-1] = b;
349   }
350   return (buff);
351 }
352
353
354 int WCMD_dir_sort (const void *a, const void *b)
355 {
356   return (lstrcmpi(((const WIN32_FIND_DATA *)a)->cFileName,
357                    ((const WIN32_FIND_DATA *)b)->cFileName));
358 }