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
84 #include <winsock.h> /* To make nt.h happy */
85 #include "nt.h" /* For prototypes */
87 #if ROUND_FILE_SIZES > 0
88 #include <math.h> /* for floor() */
92 static int mswindows_ls_sort_case_insensitive;
93 static Fixnum mswindows_ls_round_file_size;
95 Lisp_Object Qmswindows_insert_directory;
97 extern Lisp_Object Vmswindows_downcase_file_names; /* in device-msw.c */
101 enum mswindows_sortby {
102 MSWINDOWS_SORT_BY_NAME,
103 MSWINDOWS_SORT_BY_NAME_NOCASE,
104 MSWINDOWS_SORT_BY_MOD_DATE,
105 MSWINDOWS_SORT_BY_SIZE
109 static enum mswindows_sortby mswindows_sort_method;
110 static int mswindows_reverse_sort;
113 #define CMPDWORDS(t1a, t1b, t2a, t2b) \
114 (((t1a) == (t2a)) ? (((t1b) == (t2b)) ? 0 : (((t1b) < (t2b)) ? -1 : 1)) \
115 : (((t1a) < (t2a)) ? -1 : 1))
119 mswindows_ls_sort_fcn (const void *elem1, const void *elem2)
121 WIN32_FIND_DATA *e1, *e2;
124 e1 = *(WIN32_FIND_DATA **)elem1;
125 e2 = *(WIN32_FIND_DATA **)elem2;
126 switch (mswindows_sort_method)
128 case MSWINDOWS_SORT_BY_NAME:
129 status = strcmp(e1->cFileName, e2->cFileName);
131 case MSWINDOWS_SORT_BY_NAME_NOCASE:
132 status = _stricmp(e1->cFileName, e2->cFileName);
134 case MSWINDOWS_SORT_BY_MOD_DATE:
135 status = CMPDWORDS(e1->ftLastWriteTime.dwHighDateTime,
136 e1->ftLastWriteTime.dwLowDateTime,
137 e2->ftLastWriteTime.dwHighDateTime,
138 e2->ftLastWriteTime.dwLowDateTime);
140 case MSWINDOWS_SORT_BY_SIZE:
141 status = CMPDWORDS(e1->nFileSizeHigh, e1->nFileSizeLow,
142 e2->nFileSizeHigh, e2->nFileSizeLow);
148 if (mswindows_reverse_sort)
157 mswindows_sort_files (WIN32_FIND_DATA **files, int nfiles,
158 enum mswindows_sortby sort_by, int reverse)
160 mswindows_sort_method = sort_by;
161 mswindows_reverse_sort = reverse;
162 qsort(files, nfiles, sizeof(WIN32_FIND_DATA *), mswindows_ls_sort_fcn);
166 static WIN32_FIND_DATA *
167 mswindows_get_files (char *dirfile, int nowild, Lisp_Object pattern,
168 int hide_dot, int hide_system, int *nfiles)
170 WIN32_FIND_DATA *files;
172 struct re_pattern_buffer *bufp = NULL;
174 char win32pattern[MAXNAMLEN+3];
178 * Much of the following code and comments were taken from dired.c.
179 * Yes, this is something of a waste, but we want speed, speed, SPEED.
182 array_size = *nfiles = 0;
187 /* PATTERN might be a flawed regular expression. Rather than
188 catching and signalling our own errors, we just call
189 compile_pattern to do the work for us. */
190 bufp = compile_pattern (pattern, 0, Qnil, 0, ERROR_ME);
192 /* Now *bufp is the compiled form of PATTERN; don't call anything
193 which might compile a new regexp until we're done with the loop! */
195 /* Initialize file info array */
196 array_size = 100; /* initial size */
197 files = xmalloc(array_size * sizeof (WIN32_FIND_DATA));
199 /* for Win32, we need to insure that the pathname ends with "\*". */
200 strcpy (win32pattern, dirfile);
203 len = strlen (win32pattern) - 1;
204 if (!IS_DIRECTORY_SEP (win32pattern[len]))
205 strcat (win32pattern, "\\");
206 strcat (win32pattern, "*");
210 * Here, we use FindFirstFile()/FindNextFile() instead of opendir(),
211 * xemacs_stat(), & friends, because xemacs_stat() is VERY expensive in
212 * terms of time. Hence, we take the time to write complicated
213 * Win32-specific code, instead of simple Unix-style stuff.
216 fh = INVALID_HANDLE_VALUE;
224 if (fh == INVALID_HANDLE_VALUE)
226 fh = FindFirstFile(win32pattern, &files[findex]);
227 if (fh == INVALID_HANDLE_VALUE)
229 report_file_error ("Opening directory",
230 list1(build_string(dirfile)));
235 if (!FindNextFile(fh, &files[findex]))
237 if (GetLastError() == ERROR_NO_MORE_FILES)
242 report_file_error ("Reading directory",
243 list1(build_string(dirfile)));
247 filename = files[findex].cFileName;
248 if (!NILP(Vmswindows_downcase_file_names))
252 len = strlen(filename);
253 result = (NILP(pattern)
254 || (0 <= re_search (bufp, filename,
258 if ( ! (filename[0] == '.' &&
259 ((hide_system && (filename[1] == '\0' ||
260 (filename[1] == '.' &&
261 filename[2] == '\0'))) ||
264 if (++findex >= array_size)
266 array_size = findex * 2;
267 files = xrealloc(files,
268 array_size * sizeof(WIN32_FIND_DATA));
273 if (fh != INVALID_HANDLE_VALUE)
285 mswindows_format_file (WIN32_FIND_DATA *file, char *buf, int display_size,
293 len = strlen(file->cFileName);
295 file->nFileSizeHigh * (double)UINT_MAX + file->nFileSizeLow;
303 sprintf(cptr, "%6d ", (int)((file_size + 1023.) / 1024.));
306 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
312 cptr[0] = cptr[3] = cptr[6] = 'r';
313 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
315 cptr[1] = cptr[4] = cptr[7] = '-';
317 cptr[1] = cptr[4] = cptr[7] = 'w';
319 if ((file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
321 (_stricmp(&file->cFileName[len - 4], ".exe") == 0
322 || _stricmp(&file->cFileName[len - 4], ".com") == 0
323 || _stricmp(&file->cFileName[len - 4], ".bat") == 0
325 || _stricmp(&file->cFileName[len - 4], ".pif") == 0
329 cptr[2] = cptr[5] = cptr[8] = 'x';
331 cptr[2] = cptr[5] = cptr[8] = '-';
334 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
341 luser = Fuser_login_name(Qnil);
344 sprintf(cptr, "%-9d", 0);
348 str = XSTRING_DATA(luser);
349 sprintf(cptr, "%-8s ", str);
355 sprintf(cptr, "%-8d ", getgid());
357 if (file_size > 99999999.0)
359 file_size = (file_size + 1023.0) / 1024.;
360 if (file_size > 999999.0)
362 sprintf(cptr, "%6.0fMB ", (file_size + 1023.0) / 1024.);
364 sprintf(cptr, "%6.0fKB ", file_size);
367 sprintf(cptr, "%8.0f ", file_size);
376 extern char *sys_ctime(const time_t *t); /* in nt.c */
382 * This code should be correct ...
384 FileTimeToLocalFileTime(&file->ftLastWriteTime, &localtime) &&
385 ((t = convert_time(localtime)) != 0) &&
388 * But this code "works" ...
390 ((t = convert_time(file->ftLastWriteTime)) != 0) &&
392 ((ctimebuf = sys_ctime(&t)) != NULL))
394 memcpy(cptr, &ctimebuf[4], 7);
396 if (now - t > (365. / 2.0) * 86400.)
398 /* more than 6 months */
400 memcpy(&cptr[8], &ctimebuf[20], 4);
402 /* less than 6 months */
403 memcpy(&cptr[7], &ctimebuf[11], 5);
411 sprintf(cptr, "%s\n", file->cFileName);
415 strcpy(cptr, file->cFileName);
420 DEFUN ("mswindows-insert-directory", Fmswindows_insert_directory, 2, 4, 0, /*
421 Insert directory listing for FILE, formatted according to SWITCHES.
422 Leaves point after the inserted text.
423 SWITCHES may be a string of options, or a list of strings.
424 Optional third arg WILDCARD means treat FILE as shell wildcard.
425 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
426 switches do not contain `d', so that a full listing is expected.
428 (file, switches, wildcard, full_directory_p))
430 Lisp_Object result, handler, wildpat, fns, basename;
433 int hide_system, hide_dot, reverse, display_size;
434 WIN32_FIND_DATA *files, **sorted_files;
435 enum mswindows_sortby sort_by;
436 char fmtbuf[MAXNAMLEN+100]; /* larger than necessary */
437 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
443 GCPRO5(result, file, wildpat, fns, basename);
450 sort_by = (mswindows_ls_sort_case_insensitive
451 ? MSWINDOWS_SORT_BY_NAME_NOCASE
452 : MSWINDOWS_SORT_BY_NAME);
456 handler = Ffind_file_name_handler (file, Qmswindows_insert_directory);
459 result = call5(handler, Qmswindows_insert_directory, file, switches,
460 wildcard, full_directory_p);
468 CHECK_STRING (switches);
469 switchstr = XSTRING_DATA(switches);
470 for (cptr = switchstr; *cptr; ++cptr)
488 sort_by = MSWINDOWS_SORT_BY_SIZE;
491 sort_by = MSWINDOWS_SORT_BY_MOD_DATE;
501 file = Fdirectory_file_name (file);
502 basename = Ffile_name_nondirectory(file);
503 fns = intern("wildcard-to-regexp");
504 wildpat = call1(fns, basename);
505 newfile = Ffile_name_directory(file);
508 /* Ffile_name_directory() can GC */
509 newfile = Ffile_name_directory(Fexpand_file_name(file, Qnil));
513 if (!NILP(wildcard) || !NILP(full_directory_p))
518 CHECK_STRING(wildpat);
521 files = mswindows_get_files(XSTRING_DATA(file), FALSE, wildpat,
522 hide_dot, hide_system, &nfiles);
523 if (files == NULL || nfiles == 0)
530 files = mswindows_get_files(XSTRING_DATA(file), TRUE, wildpat,
531 hide_dot, hide_system, &nfiles);
533 if ((sorted_files = xmalloc(nfiles * sizeof(WIN32_FIND_DATA *)))
538 for (i = 0; i < nfiles; ++i)
540 sorted_files[i] = &files[i];
544 mswindows_sort_files(sorted_files, nfiles, sort_by, reverse);
546 if (!NILP(wildcard) || !NILP(full_directory_p))
549 * By using doubles, we can handle files up to 2^53 bytes in
550 * size (IEEE doubles have 53 bits of resolution). However,
551 * as we divide by 1024 (or 2^10), the total size is
552 * accurate up to 2^(53+10) --> 2^63 bytes.
554 * Hopefully, we won't have to handle these file sizes anytime
557 double total_size, file_size, block_size;
559 if ((block_size = mswindows_ls_round_file_size) <= 0)
564 for (i = 0; i < nfiles; ++i)
567 sorted_files[i]->nFileSizeHigh * (double)UINT_MAX +
568 sorted_files[i]->nFileSizeLow;
572 * Round file_size up to the next nearest block size.
575 floor((file_size + block_size - 1) / block_size)
578 /* Here, we round to the nearest 1K */
579 total_size += floor((file_size + 512.) / 1024.);
583 /* ANSI C compilers auto-concatenate adjacent strings */
586 "total %.0f\n", total_size);
587 buffer_insert1(current_buffer, build_string(fmtbuf));
589 for (i = 0; i < nfiles; ++i)
591 mswindows_format_file(sorted_files[i], fmtbuf, display_size, TRUE);
592 buffer_insert1(current_buffer, build_string(fmtbuf));
606 /************************************************************************/
608 /************************************************************************/
611 syms_of_dired_mswindows (void)
613 defsymbol (&Qmswindows_insert_directory, "mswindows-insert-directory");
615 DEFSUBR (Fmswindows_insert_directory);
620 vars_of_dired_mswindows (void)
622 DEFVAR_BOOL ("mswindows-ls-sort-case-insensitive", &mswindows_ls_sort_case_insensitive /*
623 *Non-nil means filenames are sorted in a case-insensitive fashion.
624 Nil means filenames are sorted in a case-sensitive fashion, just like Unix.
626 mswindows_ls_sort_case_insensitive = 1;
628 DEFVAR_INT ("mswindows-ls-round-file-size", &mswindows_ls_round_file_size /*
629 *If non-zero, file sizes are rounded in terms of this block size when
630 the file totals are being calculated. This is useful for getting a more
631 accurate estimate of allocated disk space. Note that this only affects
632 the total size calculation; the individual displayed file sizes are not
633 changed. This block size should also be a power of 2 (but this is not
634 enforced), as filesystem block (cluster) sizes are typically powers-of-2.
637 * Here, we choose 4096 because it's the cluster size for both FAT32
638 * and NTFS (?). This is probably much too small for people using
639 * plain FAT, but, hopefully, plain FAT will go away someday.
641 * We should allow something like a alist here, to make the size
642 * dependent on the drive letter, etc..
644 mswindows_ls_round_file_size = 4096;