Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 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 | * - 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. |
| 15 | */ |
| 16 | |
| 17 | #include "wcmd.h" |
| 18 | |
| 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); |
| 24 | |
| 25 | |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 26 | extern char nyi[]; |
| 27 | extern char newline[]; |
| 28 | extern char version_string[]; |
| 29 | extern char anykey[]; |
| 30 | extern int echo_mode; |
| 31 | extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH]; |
| 32 | |
| 33 | int file_total, dir_total, line_count, page_mode, recurse; |
| 34 | __int64 byte_total; |
| 35 | |
| 36 | /***************************************************************************** |
| 37 | * WCMD_directory |
| 38 | * |
| 39 | * List a file directory. |
| 40 | * FIXME: /S switch only works for the current directory |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | void WCMD_directory () { |
| 45 | |
| 46 | char path[MAX_PATH], drive[8]; |
| 47 | int status; |
| 48 | __int64 free_space; |
| 49 | DWORD spc, bps, fc, capacity; |
| 50 | |
| 51 | line_count = 5; |
| 52 | page_mode = (strstr(quals, "/P") != NULL); |
| 53 | recurse = (strstr(quals, "/S") != NULL); |
| 54 | if (param1[0] == '\0') strcpy (param1, "."); |
| 55 | GetFullPathName (param1, sizeof(path), path, NULL); |
| 56 | lstrcpyn (drive, path, 3); |
| 57 | status = WCMD_volume (0, drive); |
| 58 | if (!status) { |
| 59 | return; |
| 60 | } |
| 61 | WCMD_list_directory (path, 0); |
| 62 | lstrcpyn (drive, path, 4); |
| 63 | GetDiskFreeSpace (drive, &spc, &bps, &fc, &capacity); |
| 64 | free_space = bps * spc * fc; |
| 65 | WCMD_output (" %18s bytes free\n\n", WCMD_filesize64 (free_space)); |
| 66 | if (recurse) { |
| 67 | WCMD_output ("Total files listed:\n%8d files%25s bytes\n%8d directories\n\n", |
| 68 | file_total, WCMD_filesize64 (byte_total), dir_total); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /***************************************************************************** |
| 73 | * WCMD_list_directory |
| 74 | * |
| 75 | * List a single file directory. This function (and those below it) can be called |
| 76 | * recursively when the /S switch is used. |
| 77 | * |
| 78 | * FIXME: Assumes individual files are less than 2**32 bytes. |
| 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 | __int64 byte_count; |
| 97 | |
| 98 | dir_count = 0; |
| 99 | file_count = 0; |
| 100 | entry_count = 0; |
| 101 | byte_count = 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 | WCMD_output ("File Not Found\n"); |
| 125 | free (fd); |
| 126 | return; |
| 127 | } |
| 128 | do { |
| 129 | entry_count++; |
| 130 | fd = realloc (fd, (entry_count+1)*sizeof(WIN32_FIND_DATA)); |
| 131 | if (fd == NULL) { |
| 132 | FindClose (hff); |
| 133 | WCMD_output (mem_err); |
| 134 | return; |
| 135 | } |
| 136 | } while (FindNextFile(hff, (fd+entry_count)) != 0); |
| 137 | FindClose (hff); |
| 138 | qsort (fd, entry_count, sizeof(WIN32_FIND_DATA), WCMD_dir_sort); |
| 139 | if (level != 0) WCMD_output ("\n\n"); |
| 140 | WCMD_output ("Directory of %s\n\n", search_path); |
| 141 | if (page_mode) { |
| 142 | line_count += 2; |
| 143 | if (line_count > 23) { |
| 144 | line_count = 0; |
| 145 | WCMD_output (anykey); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 146 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | for (i=0; i<entry_count; i++) { |
| 150 | FileTimeToLocalFileTime (&(fd+i)->ftLastWriteTime, &ft); |
| 151 | FileTimeToSystemTime (&ft, &st); |
| 152 | GetDateFormat (0, DATE_SHORTDATE, &st, NULL, datestring, |
| 153 | sizeof(datestring)); |
| 154 | GetTimeFormat (0, TIME_NOSECONDS, &st, |
| 155 | NULL, timestring, sizeof(timestring)); |
| 156 | if ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 157 | dir_count++; |
| 158 | WCMD_output ("%8s %8s <DIR> %s\n", |
| 159 | datestring, timestring, (fd+i)->cFileName); |
| 160 | } |
| 161 | else { |
| 162 | file_count++; |
| 163 | byte_count += (fd+i)->nFileSizeLow; |
| 164 | WCMD_output ("%8s %8s %10s %s\n", |
| 165 | datestring, timestring, |
| 166 | WCMD_filesize32((fd+i)->nFileSizeLow), (fd+i)->cFileName); |
| 167 | } |
| 168 | if (page_mode) { |
| 169 | if (++line_count > 23) { |
| 170 | line_count = 0; |
| 171 | WCMD_output (anykey); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 172 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | if (file_count == 1) { |
| 177 | WCMD_output (" 1 file %25s bytes\n", WCMD_filesize64 (byte_count)); |
| 178 | } |
| 179 | else { |
| 180 | WCMD_output ("%8d files %24s bytes\n", file_count, WCMD_filesize64 (byte_count)); |
| 181 | } |
| 182 | if (page_mode) { |
| 183 | if (++line_count > 23) { |
| 184 | line_count = 0; |
| 185 | WCMD_output (anykey); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 186 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | byte_total = byte_total + byte_count; |
| 190 | file_total = file_total + file_count; |
| 191 | dir_total = dir_total + dir_count; |
| 192 | if (dir_count == 1) WCMD_output ("1 directory "); |
| 193 | else WCMD_output ("%8d directories", dir_count); |
| 194 | if (page_mode) { |
| 195 | if (++line_count > 23) { |
| 196 | line_count = 0; |
| 197 | WCMD_output (anykey); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 198 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | for (i=0; i<entry_count; i++) { |
| 202 | if ((recurse) && |
| 203 | ((fd+i)->cFileName[0] != '.') && |
| 204 | ((fd+i)->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 205 | // GetFullPathName ((fd+i)->cFileName, sizeof(string), string, NULL); |
| 206 | p = strrchr (search_path, '\\'); |
| 207 | lstrcpyn (string, search_path, (p-search_path+2)); |
| 208 | lstrcat (string, (fd+i)->cFileName); |
| 209 | lstrcat (string, p); |
| 210 | WCMD_list_directory (string, 1); |
| 211 | } |
| 212 | } |
| 213 | free (fd); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | /***************************************************************************** |
| 218 | * WCMD_filesize64 |
| 219 | * |
| 220 | * Convert a 64-bit number into a character string, with commas every three digits. |
| 221 | * Result is returned in a static string overwritten with each call. |
| 222 | * FIXME: There must be a better algorithm! |
| 223 | */ |
| 224 | |
| 225 | char * WCMD_filesize64 (__int64 n) { |
| 226 | |
| 227 | __int64 q; |
| 228 | int r, i; |
| 229 | char *p; |
| 230 | static char buff[32]; |
| 231 | |
| 232 | p = buff; |
| 233 | i = -3; |
| 234 | do { |
| 235 | if ((++i)%3 == 1) *p++ = ','; |
| 236 | q = n / 10; |
| 237 | r = n - (q * 10); |
| 238 | *p++ = r + '0'; |
| 239 | *p = '\0'; |
| 240 | n = q; |
| 241 | } while (n != 0); |
| 242 | WCMD_strrev (buff); |
| 243 | return buff; |
| 244 | } |
| 245 | |
| 246 | /***************************************************************************** |
| 247 | * WCMD_filesize32 |
| 248 | * |
| 249 | * Convert a 32-bit number into a character string, with commas every three digits. |
| 250 | * Result is returned in a static string overwritten with each call. |
| 251 | * FIXME: There must be a better algorithm! |
| 252 | */ |
| 253 | |
| 254 | char * WCMD_filesize32 (int n) { |
| 255 | |
| 256 | int r, i; |
| 257 | char *p, *q; |
| 258 | static char buff1[16], buff2[16]; |
| 259 | |
| 260 | wsprintf (buff1, "%i", n); |
| 261 | r = lstrlen (buff1); |
| 262 | WCMD_strrev (buff1); |
| 263 | p = buff1; |
| 264 | q = buff2; |
| 265 | for (i=0; i<r; i++) { |
| 266 | if ((i-2)%3 == 1) *q++ = ','; |
| 267 | *q++ = *p++; |
| 268 | } |
| 269 | *q = '\0'; |
| 270 | WCMD_strrev (buff2); |
| 271 | return buff2; |
| 272 | } |
| 273 | |
| 274 | /***************************************************************************** |
| 275 | * WCMD_strrev |
| 276 | * |
| 277 | * Reverse a character string in-place (strrev() is not available under unixen :-( ). |
| 278 | */ |
| 279 | |
| 280 | char * WCMD_strrev (char *buff) { |
| 281 | |
| 282 | int r, i; |
| 283 | char b; |
| 284 | |
| 285 | r = lstrlen (buff); |
| 286 | for (i=0; i<r/2; i++) { |
| 287 | b = buff[i]; |
| 288 | buff[i] = buff[r-i-1]; |
| 289 | buff[r-i-1] = b; |
| 290 | } |
| 291 | return (buff); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | int WCMD_dir_sort (const void *a, const void *b) { |
| 296 | |
| 297 | return (lstrcmpi(((WIN32_FIND_DATA *)a)->cFileName, |
| 298 | ((WIN32_FIND_DATA *)b)->cFileName)); |
| 299 | } |
| 300 | |