1 /* fast dired replacement routines for mswindows.
2 Copyright (C) 1998 Darryl Okahata
3 Portions Copyright (C) 1992, 1994 by Sebastian Kremer <sk@thp.uni-koeln.de>
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: Not in FSF. */
25 * Parts of this code (& comments) were taken from ls-lisp.el
26 * Author: Sebastian Kremer <sk@thp.uni-koeln.de>
31 * - must insert _exactly_one_line_ describing FILE if WILDCARD and
32 * FULL-DIRECTORY-P is nil.
33 * The single line of output must display FILE's name as it was
34 * given, namely, an absolute path name.
35 * - must insert exactly one line for each file if WILDCARD or
36 * FULL-DIRECTORY-P is t, plus one optional "total" line
37 * before the file lines, plus optional text after the file lines.
38 * Lines are delimited by "\n", so filenames containing "\n" are not
40 * File lines should display the basename.
41 * - must be consistent with
42 * - functions dired-move-to-filename, (these two define what a file line is)
43 * dired-move-to-end-of-filename,
44 * dired-between-files, (shortcut for (not (dired-move-to-filename)))
45 * dired-insert-headerline
46 * dired-after-subdir-garbage (defines what a "total" line is)
47 * - variable dired-subdir-regexp
51 * Insert directory listing for FILE, formatted according to SWITCHES.
52 * Leaves point after the inserted text.
53 * SWITCHES may be a string of options, or a list of strings.
54 * Optional third arg WILDCARD means treat FILE as shell wildcard.
55 * Optional fourth arg FULL-DIRECTORY-P means file is a directory and
56 * switches do not contain `d', so that a full listing is expected.
58 * This works by running a directory listing program
59 * whose name is in the variable `insert-directory-program'.
60 * If WILDCARD, it also runs the shell specified by `shell-file-name'."
64 * Set INDENT_LISTING to non-zero if the inserted text should be shifted
67 #define INDENT_LISTING 0
69 #define ROUND_FILE_SIZES 4096
85 #include <winsock.h> /* To make nt.h happy */
86 #include "nt.h" /* For prototypes */
88 #if ROUND_FILE_SIZES > 0
89 #include <math.h> /* for floor() */
93 static int mswindows_ls_sort_case_insensitive;
94 static int mswindows_ls_round_file_size;
96 Lisp_Object Qmswindows_insert_directory;
98 extern Lisp_Object Vmswindows_downcase_file_names; /* in device-msw.c */
102 enum mswindows_sortby {
103 MSWINDOWS_SORT_BY_NAME,
104 MSWINDOWS_SORT_BY_NAME_NOCASE,
105 MSWINDOWS_SORT_BY_MOD_DATE,
106 MSWINDOWS_SORT_BY_SIZE
110 static enum mswindows_sortby mswindows_sort_method;
111 static int mswindows_reverse_sort;
114 #define CMPDWORDS(t1a, t1b, t2a, t2b) \
115 (((t1a) == (t2a)) ? (((t1b) == (t2b)) ? 0 : (((t1b) < (t2b)) ? -1 : 1)) \
116 : (((t1a) < (t2a)) ? -1 : 1))
120 mswindows_ls_sort_fcn (const void *elem1, const void *elem2)
122 WIN32_FIND_DATA *e1, *e2;
125 e1 = *(WIN32_FIND_DATA **)elem1;
126 e2 = *(WIN32_FIND_DATA **)elem2;
127 switch (mswindows_sort_method)
129 case MSWINDOWS_SORT_BY_NAME:
130 status = strcmp(e1->cFileName, e2->cFileName);
132 case MSWINDOWS_SORT_BY_NAME_NOCASE:
133 status = _stricmp(e1->cFileName, e2->cFileName);
135 case MSWINDOWS_SORT_BY_MOD_DATE:
136 status = CMPDWORDS(e1->ftLastWriteTime.dwHighDateTime,
137 e1->ftLastWriteTime.dwLowDateTime,
138 e2->ftLastWriteTime.dwHighDateTime,
139 e2->ftLastWriteTime.dwLowDateTime);
141 case MSWINDOWS_SORT_BY_SIZE:
142 status = CMPDWORDS(e1->nFileSizeHigh, e1->nFileSizeLow,
143 e2->nFileSizeHigh, e2->nFileSizeLow);
149 if (mswindows_reverse_sort)
158 mswindows_sort_files (WIN32_FIND_DATA **files, int nfiles,
159 enum mswindows_sortby sort_by, int reverse)
161 mswindows_sort_method = sort_by;
162 mswindows_reverse_sort = reverse;
163 qsort(files, nfiles, sizeof(WIN32_FIND_DATA *), mswindows_ls_sort_fcn);
167 static WIN32_FIND_DATA *
168 mswindows_get_files (char *dirfile, int nowild, Lisp_Object pattern,
169 int hide_dot, int hide_system, int *nfiles)
171 WIN32_FIND_DATA *files;
173 struct re_pattern_buffer *bufp = NULL;
175 char win32pattern[MAXNAMLEN+3];
179 * Much of the following code and comments were taken from dired.c.
180 * Yes, this is something of a waste, but we want speed, speed, SPEED.
183 array_size = *nfiles = 0;
188 /* PATTERN might be a flawed regular expression. Rather than
189 catching and signalling our own errors, we just call
190 compile_pattern to do the work for us. */
191 bufp = compile_pattern (pattern, 0, 0, 0, ERROR_ME);
193 /* Now *bufp is the compiled form of PATTERN; don't call anything
194 which might compile a new regexp until we're done with the loop! */
196 /* Initialize file info array */
197 array_size = 100; /* initial size */
198 files = xmalloc(array_size * sizeof (WIN32_FIND_DATA));
200 /* for Win32, we need to insure that the pathname ends with "\*". */
201 strcpy (win32pattern, dirfile);
204 len = strlen (win32pattern) - 1;
205 if (!IS_DIRECTORY_SEP (win32pattern[len]))
206 strcat (win32pattern, "\\");
207 strcat (win32pattern, "*");
211 * Here, we use FindFirstFile()/FindNextFile() instead of opendir(),
212 * stat(), & friends, because stat() is VERY expensive in terms of
213 * time. Hence, we take the time to write complicated Win32-specific
214 * code, instead of simple Unix-style stuff.
217 fh = INVALID_HANDLE_VALUE;
225 if (fh == INVALID_HANDLE_VALUE)
227 fh = FindFirstFile(win32pattern, &files[findex]);
228 if (fh == INVALID_HANDLE_VALUE)
230 report_file_error ("Opening directory",
231 list1(build_string(dirfile)));
236 if (!FindNextFile(fh, &files[findex]))
238 if (GetLastError() == ERROR_NO_MORE_FILES)
243 report_file_error ("Reading directory",
244 list1(build_string(dirfile)));
248 filename = files[findex].cFileName;
249 if (!NILP(Vmswindows_downcase_file_names))
253 len = strlen(filename);
254 result = (NILP(pattern)
255 || (0 <= re_search (bufp, filename,
259 if ( ! (filename[0] == '.' &&
260 ((hide_system && (filename[1] == '\0' ||
261 (filename[1] == '.' &&
262 filename[2] == '\0'))) ||
265 if (++findex >= array_size)
267 array_size = findex * 2;
268 files = xrealloc(files,
269 array_size * sizeof(WIN32_FIND_DATA));
274 if (fh != INVALID_HANDLE_VALUE)
286 mswindows_format_file (WIN32_FIND_DATA *file, char *buf, int display_size,
294 len = strlen(file->cFileName);
296 file->nFileSizeHigh * (double)UINT_MAX + file->nFileSizeLow;
304 sprintf(cptr, "%6d ", (int)((file_size + 1023.) / 1024.));
307 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
313 cptr[0] = cptr[3] = cptr[6] = 'r';
314 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
316 cptr[1] = cptr[4] = cptr[7] = '-';
318 cptr[1] = cptr[4] = cptr[7] = 'w';
320 if ((file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
322 (_stricmp(&file->cFileName[len - 4], ".exe") == 0
323 || _stricmp(&file->cFileName[len - 4], ".com") == 0
324 || _stricmp(&file->cFileName[len - 4], ".bat") == 0
326 || _stricmp(&file->cFileName[len - 4], ".pif") == 0
330 cptr[2] = cptr[5] = cptr[8] = 'x';
332 cptr[2] = cptr[5] = cptr[8] = '-';
335 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
342 luser = Fuser_login_name(Qnil);
345 sprintf(cptr, "%-9d", 0);
349 str = XSTRING_DATA(luser);
350 sprintf(cptr, "%-8s ", str);
356 sprintf(cptr, "%-8d ", getgid());
358 if (file_size > 99999999.0)
360 file_size = (file_size + 1023.0) / 1024.;
361 if (file_size > 999999.0)
363 sprintf(cptr, "%6.0fMB ", (file_size + 1023.0) / 1024.);
365 sprintf(cptr, "%6.0fKB ", file_size);
368 sprintf(cptr, "%8.0f ", file_size);
377 extern char *sys_ctime(const time_t *t); /* in nt.c */
383 * This code should be correct ...
385 FileTimeToLocalFileTime(&file->ftLastWriteTime, &localtime) &&
386 ((t = convert_time(localtime)) != 0) &&
389 * But this code "works" ...
391 ((t = convert_time(file->ftLastWriteTime)) != 0) &&
393 ((ctimebuf = sys_ctime(&t)) != NULL))
395 memcpy(cptr, &ctimebuf[4], 7);
397 if (now - t > (365. / 2.0) * 86400.)
399 /* more than 6 months */
401 memcpy(&cptr[8], &ctimebuf[20], 4);
403 /* less than 6 months */
404 memcpy(&cptr[7], &ctimebuf[11], 5);
412 sprintf(cptr, "%s\n", file->cFileName);
416 strcpy(cptr, file->cFileName);
421 DEFUN ("mswindows-insert-directory", Fmswindows_insert_directory, 2, 4, 0, /*
422 Insert directory listing for FILE, formatted according to SWITCHES.
423 Leaves point after the inserted text.
424 SWITCHES may be a string of options, or a list of strings.
425 Optional third arg WILDCARD means treat FILE as shell wildcard.
426 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
427 switches do not contain `d', so that a full listing is expected.
429 (file, switches, wildcard, full_directory_p))
431 Lisp_Object result, handler, wildpat, fns, basename;
435 int hide_system, hide_dot, reverse, display_size;
436 WIN32_FIND_DATA *files, **sorted_files;
437 enum mswindows_sortby sort_by;
438 char fmtbuf[MAXNAMLEN+100]; /* larger than necessary */
439 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
445 GCPRO5(result, file, wildpat, fns, basename);
452 sort_by = (mswindows_ls_sort_case_insensitive
453 ? MSWINDOWS_SORT_BY_NAME_NOCASE
454 : MSWINDOWS_SORT_BY_NAME);
458 handler = Ffind_file_name_handler (file, Qmswindows_insert_directory);
461 result = call5(handler, Qmswindows_insert_directory, file, switches,
462 wildcard, full_directory_p);
470 CHECK_STRING (switches);
471 switchstr = XSTRING_DATA(switches);
472 for (cptr = switchstr; *cptr; ++cptr)
490 sort_by = MSWINDOWS_SORT_BY_SIZE;
493 sort_by = MSWINDOWS_SORT_BY_MOD_DATE;
500 * Sometimes we get ".../foo* /" as FILE (without the space).
501 * While the shell and `ls' don't mind, we certainly do,
502 * because it makes us think there is no wildcard, only a
505 if (!NILP(Fstring_match(build_string("[[?*]"), file, Qnil, Qnil)))
508 filename = XSTRING_DATA(file);
509 len = strlen(filename);
510 if (len > 0 && (filename[len - 1] == '\\' ||
511 filename[len - 1] == '/'))
513 filename[len - 1] = '\0';
515 file = build_string(filename);
521 basename = Ffile_name_nondirectory(file);
522 fns = intern("wildcard-to-regexp");
523 wildpat = call1(fns, basename);
524 newfile = Ffile_name_directory(file);
527 /* Ffile_name_directory() can GC */
528 newfile = Ffile_name_directory(Fexpand_file_name(file, Qnil));
532 if (!NILP(wildcard) || !NILP(full_directory_p))
537 CHECK_STRING(wildpat);
540 files = mswindows_get_files(XSTRING_DATA(file), FALSE, wildpat,
541 hide_dot, hide_system, &nfiles);
542 if (files == NULL || nfiles == 0)
549 files = mswindows_get_files(XSTRING_DATA(file), TRUE, wildpat,
550 hide_dot, hide_system, &nfiles);
552 if ((sorted_files = xmalloc(nfiles * sizeof(WIN32_FIND_DATA *)))
557 for (i = 0; i < nfiles; ++i)
559 sorted_files[i] = &files[i];
563 mswindows_sort_files(sorted_files, nfiles, sort_by, reverse);
565 if (!NILP(wildcard) || !NILP(full_directory_p))
568 * By using doubles, we can handle files up to 2^53 bytes in
569 * size (IEEE doubles have 53 bits of resolution). However,
570 * as we divide by 1024 (or 2^10), the total size is
571 * accurate up to 2^(53+10) --> 2^63 bytes.
573 * Hopefully, we won't have to handle these file sizes anytime
576 double total_size, file_size, block_size;
578 if ((block_size = mswindows_ls_round_file_size) <= 0)
583 for (i = 0; i < nfiles; ++i)
586 sorted_files[i]->nFileSizeHigh * (double)UINT_MAX +
587 sorted_files[i]->nFileSizeLow;
591 * Round file_size up to the next nearest block size.
594 floor((file_size + block_size - 1) / block_size)
597 /* Here, we round to the nearest 1K */
598 total_size += floor((file_size + 512.) / 1024.);
602 /* ANSI C compilers auto-concatenate adjacent strings */
605 "total %.0f\n", total_size);
606 buffer_insert1(current_buffer, build_string(fmtbuf));
608 for (i = 0; i < nfiles; ++i)
610 mswindows_format_file(sorted_files[i], fmtbuf, display_size, TRUE);
611 buffer_insert1(current_buffer, build_string(fmtbuf));
625 /************************************************************************/
627 /************************************************************************/
630 syms_of_dired_mswindows (void)
632 defsymbol (&Qmswindows_insert_directory, "mswindows-insert-directory");
634 DEFSUBR (Fmswindows_insert_directory);
639 vars_of_dired_mswindows (void)
641 DEFVAR_BOOL ("mswindows-ls-sort-case-insensitive", &mswindows_ls_sort_case_insensitive /*
642 *Non-nil means filenames are sorted in a case-insensitive fashion.
643 Nil means filenames are sorted in a case-sensitive fashion, just like Unix.
645 mswindows_ls_sort_case_insensitive = 1;
647 DEFVAR_INT ("mswindows-ls-round-file-size", &mswindows_ls_round_file_size /*
648 *If non-zero, file sizes are rounded in terms of this block size when
649 the file totals are being calculated. This is useful for getting a more
650 accurate estimate of allocated disk space. Note that this only affects
651 the total size calculation; the individual displayed file sizes are not
652 changed. This block size should also be a power of 2 (but this is not
653 enforced), as filesystem block (cluster) sizes are typically powers-of-2.
656 * Here, we choose 4096 because it's the cluster size for both FAT32
657 * and NTFS (?). This is probably much too small for people using
658 * plain FAT, but, hopefully, plain FAT will go away someday.
660 * We should allow something like a alist here, to make the size
661 * dependent on the drive letter, etc..
663 mswindows_ls_round_file_size = 4096;