]> code.delx.au - gnu-emacs/blob - src/dired.c
(stat): Fix last change.
[gnu-emacs] / src / dired.c
1 /* Lisp functions for making directory listings.
2 Copyright (C) 1985, 1986, 1993, 1994, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #ifdef HAVE_PWD_H
28 #include <pwd.h>
29 #endif
30 #include <grp.h>
31
32 #include <errno.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 /* The d_nameln member of a struct dirent includes the '\0' character
39 on some systems, but not on others. What's worse, you can't tell
40 at compile-time which one it will be, since it really depends on
41 the sort of system providing the filesystem you're reading from,
42 not the system you are running on. Paul Eggert
43 <eggert@bi.twinsun.com> says this occurs when Emacs is running on a
44 SunOS 4.1.2 host, reading a directory that is remote-mounted from a
45 Solaris 2.1 host and is in a native Solaris 2.1 filesystem.
46
47 Since applying strlen to the name always works, we'll just do that. */
48 #define NAMLEN(p) strlen (p->d_name)
49
50 #ifdef SYSV_SYSTEM_DIR
51
52 #include <dirent.h>
53 #define DIRENTRY struct dirent
54
55 #else /* not SYSV_SYSTEM_DIR */
56
57 #ifdef MSDOS
58 #include <dirent.h>
59 #else
60 #include <sys/dir.h>
61 #endif
62
63 #include <sys/stat.h>
64
65 #ifndef MSDOS
66 #define DIRENTRY struct direct
67
68 extern DIR *opendir ();
69 extern struct direct *readdir ();
70
71 #endif /* not MSDOS */
72 #endif /* not SYSV_SYSTEM_DIR */
73
74 /* Some versions of Cygwin don't have d_ino in `struct dirent'. */
75 #if defined(MSDOS) || defined(__CYGWIN__)
76 #define DIRENTRY_NONEMPTY(p) ((p)->d_name[0] != 0)
77 #else
78 #define DIRENTRY_NONEMPTY(p) ((p)->d_ino)
79 #endif
80
81 #include "lisp.h"
82 #include "systime.h"
83 #include "buffer.h"
84 #include "commands.h"
85 #include "character.h"
86 #include "charset.h"
87 #include "coding.h"
88 #include "regex.h"
89 #include "blockinput.h"
90
91 /* Returns a search buffer, with a fastmap allocated and ready to go. */
92 extern struct re_pattern_buffer *compile_pattern ();
93
94 /* From filemode.c. Can't go in Lisp.h because of `stat'. */
95 extern void filemodestring P_ ((struct stat *, char *));
96
97 /* if system does not have symbolic links, it does not have lstat.
98 In that case, use ordinary stat instead. */
99
100 #ifndef S_IFLNK
101 #define lstat stat
102 #endif
103
104 extern int completion_ignore_case;
105 extern Lisp_Object Qcompletion_ignore_case;
106 extern Lisp_Object Vcompletion_regexp_list;
107 extern Lisp_Object Vw32_get_true_file_attributes;
108
109 Lisp_Object Vcompletion_ignored_extensions;
110 Lisp_Object Qdirectory_files;
111 Lisp_Object Qdirectory_files_and_attributes;
112 Lisp_Object Qfile_name_completion;
113 Lisp_Object Qfile_name_all_completions;
114 Lisp_Object Qfile_attributes;
115 Lisp_Object Qfile_attributes_lessp;
116
117 static int scmp P_ ((unsigned char *, unsigned char *, int));
118 \f
119 #ifdef WINDOWSNT
120 Lisp_Object
121 directory_files_internal_w32_unwind (Lisp_Object arg)
122 {
123 Vw32_get_true_file_attributes = arg;
124 return Qnil;
125 }
126 #endif
127
128 Lisp_Object
129 directory_files_internal_unwind (dh)
130 Lisp_Object dh;
131 {
132 DIR *d = (DIR *) XSAVE_VALUE (dh)->pointer;
133 BLOCK_INPUT;
134 closedir (d);
135 UNBLOCK_INPUT;
136 return Qnil;
137 }
138
139 /* Function shared by Fdirectory_files and Fdirectory_files_and_attributes.
140 When ATTRS is zero, return a list of directory filenames; when
141 non-zero, return a list of directory filenames and their attributes.
142 In the latter case, ID_FORMAT is passed to Ffile_attributes. */
143
144 Lisp_Object
145 directory_files_internal (directory, full, match, nosort, attrs, id_format)
146 Lisp_Object directory, full, match, nosort;
147 int attrs;
148 Lisp_Object id_format;
149 {
150 DIR *d;
151 int directory_nbytes;
152 Lisp_Object list, dirfilename, encoded_directory;
153 struct re_pattern_buffer *bufp = NULL;
154 int needsep = 0;
155 int count = SPECPDL_INDEX ();
156 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
157 DIRENTRY *dp;
158 #ifdef WINDOWSNT
159 Lisp_Object w32_save = Qnil;
160 #endif
161
162 /* Because of file name handlers, these functions might call
163 Ffuncall, and cause a GC. */
164 list = encoded_directory = dirfilename = Qnil;
165 GCPRO5 (match, directory, list, dirfilename, encoded_directory);
166 dirfilename = Fdirectory_file_name (directory);
167
168 if (!NILP (match))
169 {
170 CHECK_STRING (match);
171
172 /* MATCH might be a flawed regular expression. Rather than
173 catching and signaling our own errors, we just call
174 compile_pattern to do the work for us. */
175 /* Pass 1 for the MULTIBYTE arg
176 because we do make multibyte strings if the contents warrant. */
177 # ifdef WINDOWSNT
178 /* Windows users want case-insensitive wildcards. */
179 bufp = compile_pattern (match, 0,
180 buffer_defaults.case_canon_table, 0, 1);
181 # else /* !WINDOWSNT */
182 bufp = compile_pattern (match, 0, Qnil, 0, 1);
183 # endif /* !WINDOWSNT */
184 }
185
186 /* Note: ENCODE_FILE and DECODE_FILE can GC because they can run
187 run_pre_post_conversion_on_str which calls Lisp directly and
188 indirectly. */
189 if (STRING_MULTIBYTE (dirfilename))
190 dirfilename = ENCODE_FILE (dirfilename);
191 encoded_directory = (STRING_MULTIBYTE (directory)
192 ? ENCODE_FILE (directory) : directory);
193
194 /* Now *bufp is the compiled form of MATCH; don't call anything
195 which might compile a new regexp until we're done with the loop! */
196
197 BLOCK_INPUT;
198 d = opendir (SDATA (dirfilename));
199 UNBLOCK_INPUT;
200 if (d == NULL)
201 report_file_error ("Opening directory", Fcons (directory, Qnil));
202
203 /* Unfortunately, we can now invoke expand-file-name and
204 file-attributes on filenames, both of which can throw, so we must
205 do a proper unwind-protect. */
206 record_unwind_protect (directory_files_internal_unwind,
207 make_save_value (d, 0));
208
209 #ifdef WINDOWSNT
210 if (attrs)
211 {
212 extern Lisp_Object Qlocal;
213 extern int is_slow_fs (const char *);
214
215 /* Do this only once to avoid doing it (in w32.c:stat) for each
216 file in the directory, when we call Ffile_attributes below. */
217 record_unwind_protect (directory_files_internal_w32_unwind,
218 Vw32_get_true_file_attributes);
219 w32_save = Vw32_get_true_file_attributes;
220 if (EQ (Vw32_get_true_file_attributes, Qlocal))
221 {
222 char *dirnm = SDATA (dirfilename);
223 char *fn = alloca (SBYTES (dirfilename) + 1);
224
225 strncpy (fn, SDATA (dirfilename), SBYTES (dirfilename));
226 fn[SBYTES (dirfilename)] = '\0';
227 /* w32.c:stat will notice these bindings and avoid calling
228 GetDriveType for each file. */
229 if (is_slow_fs (fn))
230 Vw32_get_true_file_attributes = Qnil;
231 else
232 Vw32_get_true_file_attributes = Qt;
233 }
234 }
235 #endif
236
237 directory_nbytes = SBYTES (directory);
238 re_match_object = Qt;
239
240 /* Decide whether we need to add a directory separator. */
241 if (directory_nbytes == 0
242 || !IS_ANY_SEP (SREF (directory, directory_nbytes - 1)))
243 needsep = 1;
244
245 /* Loop reading blocks until EOF or error. */
246 for (;;)
247 {
248 errno = 0;
249 dp = readdir (d);
250
251 if (dp == NULL && (0
252 #ifdef EAGAIN
253 || errno == EAGAIN
254 #endif
255 #ifdef EINTR
256 || errno == EINTR
257 #endif
258 ))
259 { QUIT; continue; }
260
261 if (dp == NULL)
262 break;
263
264 if (DIRENTRY_NONEMPTY (dp))
265 {
266 int len;
267 int wanted = 0;
268 Lisp_Object name, finalname;
269 struct gcpro gcpro1, gcpro2;
270
271 len = NAMLEN (dp);
272 name = finalname = make_unibyte_string (dp->d_name, len);
273 GCPRO2 (finalname, name);
274
275 /* Note: DECODE_FILE can GC; it should protect its argument,
276 though. */
277 name = DECODE_FILE (name);
278 len = SBYTES (name);
279
280 /* Now that we have unwind_protect in place, we might as well
281 allow matching to be interrupted. */
282 immediate_quit = 1;
283 QUIT;
284
285 if (NILP (match)
286 || (0 <= re_search (bufp, SDATA (name), len, 0, len, 0)))
287 wanted = 1;
288
289 immediate_quit = 0;
290
291 if (wanted)
292 {
293 if (!NILP (full))
294 {
295 Lisp_Object fullname;
296 int nbytes = len + directory_nbytes + needsep;
297 int nchars;
298
299 fullname = make_uninit_multibyte_string (nbytes, nbytes);
300 bcopy (SDATA (directory), SDATA (fullname),
301 directory_nbytes);
302
303 if (needsep)
304 SSET (fullname, directory_nbytes, DIRECTORY_SEP);
305
306 bcopy (SDATA (name),
307 SDATA (fullname) + directory_nbytes + needsep,
308 len);
309
310 nchars = chars_in_text (SDATA (fullname), nbytes);
311
312 /* Some bug somewhere. */
313 if (nchars > nbytes)
314 abort ();
315
316 STRING_SET_CHARS (fullname, nchars);
317 if (nchars == nbytes)
318 STRING_SET_UNIBYTE (fullname);
319
320 finalname = fullname;
321 }
322 else
323 finalname = name;
324
325 if (attrs)
326 {
327 /* Construct an expanded filename for the directory entry.
328 Use the decoded names for input to Ffile_attributes. */
329 Lisp_Object decoded_fullname, fileattrs;
330 struct gcpro gcpro1, gcpro2;
331
332 decoded_fullname = fileattrs = Qnil;
333 GCPRO2 (decoded_fullname, fileattrs);
334
335 /* Both Fexpand_file_name and Ffile_attributes can GC. */
336 decoded_fullname = Fexpand_file_name (name, directory);
337 fileattrs = Ffile_attributes (decoded_fullname, id_format);
338
339 list = Fcons (Fcons (finalname, fileattrs), list);
340 UNGCPRO;
341 }
342 else
343 list = Fcons (finalname, list);
344 }
345
346 UNGCPRO;
347 }
348 }
349
350 BLOCK_INPUT;
351 closedir (d);
352 UNBLOCK_INPUT;
353 #ifdef WINDOWSNT
354 if (attrs)
355 Vw32_get_true_file_attributes = w32_save;
356 #endif
357
358 /* Discard the unwind protect. */
359 specpdl_ptr = specpdl + count;
360
361 if (NILP (nosort))
362 list = Fsort (Fnreverse (list),
363 attrs ? Qfile_attributes_lessp : Qstring_lessp);
364
365 RETURN_UNGCPRO (list);
366 }
367
368
369 DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 4, 0,
370 doc: /* Return a list of names of files in DIRECTORY.
371 There are three optional arguments:
372 If FULL is non-nil, return absolute file names. Otherwise return names
373 that are relative to the specified directory.
374 If MATCH is non-nil, mention only file names that match the regexp MATCH.
375 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
376 Otherwise, the list returned is sorted with `stringp-lessp'.
377 NOSORT is useful if you plan to sort the result yourself. */)
378 (directory, full, match, nosort)
379 Lisp_Object directory, full, match, nosort;
380 {
381 Lisp_Object handler;
382 directory = Fexpand_file_name (directory, Qnil);
383
384 /* If the file name has special constructs in it,
385 call the corresponding file handler. */
386 handler = Ffind_file_name_handler (directory, Qdirectory_files);
387 if (!NILP (handler))
388 return call5 (handler, Qdirectory_files, directory,
389 full, match, nosort);
390
391 return directory_files_internal (directory, full, match, nosort, 0, Qnil);
392 }
393
394 DEFUN ("directory-files-and-attributes", Fdirectory_files_and_attributes,
395 Sdirectory_files_and_attributes, 1, 5, 0,
396 doc: /* Return a list of names of files and their attributes in DIRECTORY.
397 There are four optional arguments:
398 If FULL is non-nil, return absolute file names. Otherwise return names
399 that are relative to the specified directory.
400 If MATCH is non-nil, mention only file names that match the regexp MATCH.
401 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
402 NOSORT is useful if you plan to sort the result yourself.
403 ID-FORMAT specifies the preferred format of attributes uid and gid, see
404 `file-attributes' for further documentation.
405 On MS-Windows, performance depends on `w32-get-true-file-attributes',
406 which see. */)
407 (directory, full, match, nosort, id_format)
408 Lisp_Object directory, full, match, nosort, id_format;
409 {
410 Lisp_Object handler;
411 directory = Fexpand_file_name (directory, Qnil);
412
413 /* If the file name has special constructs in it,
414 call the corresponding file handler. */
415 handler = Ffind_file_name_handler (directory, Qdirectory_files_and_attributes);
416 if (!NILP (handler))
417 return call6 (handler, Qdirectory_files_and_attributes,
418 directory, full, match, nosort, id_format);
419
420 return directory_files_internal (directory, full, match, nosort, 1, id_format);
421 }
422
423 \f
424 Lisp_Object file_name_completion ();
425
426 DEFUN ("file-name-completion", Ffile_name_completion, Sfile_name_completion,
427 2, 3, 0,
428 doc: /* Complete file name FILE in directory DIRECTORY.
429 Returns the longest string
430 common to all file names in DIRECTORY that start with FILE.
431 If there is only one and FILE matches it exactly, returns t.
432 Returns nil if DIRECTORY contains no name starting with FILE.
433
434 If PREDICATE is non-nil, call PREDICATE with each possible
435 completion (in absolute form) and ignore it if PREDICATE returns nil.
436
437 This function ignores some of the possible completions as
438 determined by the variable `completion-ignored-extensions', which see. */)
439 (file, directory, predicate)
440 Lisp_Object file, directory, predicate;
441 {
442 Lisp_Object handler;
443
444 /* If the directory name has special constructs in it,
445 call the corresponding file handler. */
446 handler = Ffind_file_name_handler (directory, Qfile_name_completion);
447 if (!NILP (handler))
448 return call4 (handler, Qfile_name_completion, file, directory, predicate);
449
450 /* If the file name has special constructs in it,
451 call the corresponding file handler. */
452 handler = Ffind_file_name_handler (file, Qfile_name_completion);
453 if (!NILP (handler))
454 return call4 (handler, Qfile_name_completion, file, directory, predicate);
455
456 return file_name_completion (file, directory, 0, 0, predicate);
457 }
458
459 DEFUN ("file-name-all-completions", Ffile_name_all_completions,
460 Sfile_name_all_completions, 2, 2, 0,
461 doc: /* Return a list of all completions of file name FILE in directory DIRECTORY.
462 These are all file names in directory DIRECTORY which begin with FILE. */)
463 (file, directory)
464 Lisp_Object file, directory;
465 {
466 Lisp_Object handler;
467
468 /* If the directory name has special constructs in it,
469 call the corresponding file handler. */
470 handler = Ffind_file_name_handler (directory, Qfile_name_all_completions);
471 if (!NILP (handler))
472 return call3 (handler, Qfile_name_all_completions, file, directory);
473
474 /* If the file name has special constructs in it,
475 call the corresponding file handler. */
476 handler = Ffind_file_name_handler (file, Qfile_name_all_completions);
477 if (!NILP (handler))
478 return call3 (handler, Qfile_name_all_completions, file, directory);
479
480 return file_name_completion (file, directory, 1, 0, Qnil);
481 }
482
483 static int file_name_completion_stat ();
484 Lisp_Object Qdefault_directory;
485
486 Lisp_Object
487 file_name_completion (file, dirname, all_flag, ver_flag, predicate)
488 Lisp_Object file, dirname;
489 int all_flag, ver_flag;
490 Lisp_Object predicate;
491 {
492 DIR *d;
493 int bestmatchsize = 0;
494 int matchcount = 0;
495 /* If ALL_FLAG is 1, BESTMATCH is the list of all matches, decoded.
496 If ALL_FLAG is 0, BESTMATCH is either nil
497 or the best match so far, not decoded. */
498 Lisp_Object bestmatch, tem, elt, name;
499 Lisp_Object encoded_file;
500 Lisp_Object encoded_dir;
501 struct stat st;
502 int directoryp;
503 /* If includeall is zero, exclude files in completion-ignored-extensions as
504 well as "." and "..". Until shown otherwise, assume we can't exclude
505 anything. */
506 int includeall = 1;
507 int count = SPECPDL_INDEX ();
508 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
509
510 elt = Qnil;
511
512 CHECK_STRING (file);
513
514 #ifdef FILE_SYSTEM_CASE
515 file = FILE_SYSTEM_CASE (file);
516 #endif
517 bestmatch = Qnil;
518 encoded_file = encoded_dir = Qnil;
519 GCPRO5 (file, dirname, bestmatch, encoded_file, encoded_dir);
520 dirname = Fexpand_file_name (dirname, Qnil);
521 specbind (Qdefault_directory, dirname);
522
523 /* Do completion on the encoded file name
524 because the other names in the directory are (we presume)
525 encoded likewise. We decode the completed string at the end. */
526 /* Actually, this is not quite true any more: we do most of the completion
527 work with decoded file names, but we still do some filtering based
528 on the encoded file name. */
529 encoded_file = STRING_MULTIBYTE (file) ? ENCODE_FILE (file) : file;
530
531 encoded_dir = ENCODE_FILE (dirname);
532
533 BLOCK_INPUT;
534 d = opendir (SDATA (Fdirectory_file_name (encoded_dir)));
535 UNBLOCK_INPUT;
536 if (!d)
537 report_file_error ("Opening directory", Fcons (dirname, Qnil));
538
539 record_unwind_protect (directory_files_internal_unwind,
540 make_save_value (d, 0));
541
542 /* Loop reading blocks */
543 /* (att3b compiler bug requires do a null comparison this way) */
544 while (1)
545 {
546 DIRENTRY *dp;
547 int len;
548 int canexclude = 0;
549
550 errno = 0;
551 dp = readdir (d);
552 if (dp == NULL && (0
553 # ifdef EAGAIN
554 || errno == EAGAIN
555 # endif
556 # ifdef EINTR
557 || errno == EINTR
558 # endif
559 ))
560 { QUIT; continue; }
561
562 if (!dp) break;
563
564 len = NAMLEN (dp);
565
566 QUIT;
567 if (! DIRENTRY_NONEMPTY (dp)
568 || len < SCHARS (encoded_file)
569 || 0 <= scmp (dp->d_name, SDATA (encoded_file),
570 SCHARS (encoded_file)))
571 continue;
572
573 if (file_name_completion_stat (encoded_dir, dp, &st) < 0)
574 continue;
575
576 directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
577 tem = Qnil;
578 /* If all_flag is set, always include all.
579 It would not actually be helpful to the user to ignore any possible
580 completions when making a list of them. */
581 if (!all_flag)
582 {
583 int skip;
584
585 #if 0 /* FIXME: The `scmp' call compares an encoded and a decoded string. */
586 /* If this entry matches the current bestmatch, the only
587 thing it can do is increase matchcount, so don't bother
588 investigating it any further. */
589 if (!completion_ignore_case
590 /* The return result depends on whether it's the sole match. */
591 && matchcount > 1
592 && !includeall /* This match may allow includeall to 0. */
593 && len >= bestmatchsize
594 && 0 > scmp (dp->d_name, SDATA (bestmatch), bestmatchsize))
595 continue;
596 #endif
597
598 if (directoryp)
599 {
600 #ifndef TRIVIAL_DIRECTORY_ENTRY
601 #define TRIVIAL_DIRECTORY_ENTRY(n) (!strcmp (n, ".") || !strcmp (n, ".."))
602 #endif
603 /* "." and ".." are never interesting as completions, and are
604 actually in the way in a directory with only one file. */
605 if (TRIVIAL_DIRECTORY_ENTRY (dp->d_name))
606 canexclude = 1;
607 else if (len > SCHARS (encoded_file))
608 /* Ignore directories if they match an element of
609 completion-ignored-extensions which ends in a slash. */
610 for (tem = Vcompletion_ignored_extensions;
611 CONSP (tem); tem = XCDR (tem))
612 {
613 int elt_len;
614 unsigned char *p1;
615
616 elt = XCAR (tem);
617 if (!STRINGP (elt))
618 continue;
619 /* Need to encode ELT, since scmp compares unibyte
620 strings only. */
621 elt = ENCODE_FILE (elt);
622 elt_len = SCHARS (elt) - 1; /* -1 for trailing / */
623 if (elt_len <= 0)
624 continue;
625 p1 = SDATA (elt);
626 if (p1[elt_len] != '/')
627 continue;
628 skip = len - elt_len;
629 if (skip < 0)
630 continue;
631
632 if (0 <= scmp (dp->d_name + skip, p1, elt_len))
633 continue;
634 break;
635 }
636 }
637 else
638 {
639 /* Compare extensions-to-be-ignored against end of this file name */
640 /* if name is not an exact match against specified string */
641 if (len > SCHARS (encoded_file))
642 /* and exit this for loop if a match is found */
643 for (tem = Vcompletion_ignored_extensions;
644 CONSP (tem); tem = XCDR (tem))
645 {
646 elt = XCAR (tem);
647 if (!STRINGP (elt)) continue;
648 /* Need to encode ELT, since scmp compares unibyte
649 strings only. */
650 elt = ENCODE_FILE (elt);
651 skip = len - SCHARS (elt);
652 if (skip < 0) continue;
653
654 if (0 <= scmp (dp->d_name + skip,
655 SDATA (elt),
656 SCHARS (elt)))
657 continue;
658 break;
659 }
660 }
661
662 /* If an ignored-extensions match was found,
663 don't process this name as a completion. */
664 if (CONSP (tem))
665 canexclude = 1;
666
667 if (!includeall && canexclude)
668 /* We're not including all files and this file can be excluded. */
669 continue;
670
671 if (includeall && !canexclude)
672 { /* If we have one non-excludable file, we want to exclude the
673 excudable files. */
674 includeall = 0;
675 /* Throw away any previous excludable match found. */
676 bestmatch = Qnil;
677 bestmatchsize = 0;
678 matchcount = 0;
679 }
680 }
681 /* FIXME: If we move this `decode' earlier we can eliminate
682 the repeated ENCODE_FILE on Vcompletion_ignored_extensions. */
683 name = make_unibyte_string (dp->d_name, len);
684 name = DECODE_FILE (name);
685
686 {
687 Lisp_Object regexps;
688 Lisp_Object zero;
689 XSETFASTINT (zero, 0);
690
691 /* Ignore this element if it fails to match all the regexps. */
692 if (completion_ignore_case)
693 {
694 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
695 regexps = XCDR (regexps))
696 if (fast_string_match_ignore_case (XCAR (regexps), name) < 0)
697 break;
698 }
699 else
700 {
701 for (regexps = Vcompletion_regexp_list; CONSP (regexps);
702 regexps = XCDR (regexps))
703 if (fast_string_match (XCAR (regexps), name) < 0)
704 break;
705 }
706
707 if (CONSP (regexps))
708 continue;
709 }
710
711 /* This is a possible completion */
712 if (directoryp)
713 /* This completion is a directory; make it end with '/'. */
714 name = Ffile_name_as_directory (name);
715
716 /* Test the predicate, if any. */
717 if (!NILP (predicate))
718 {
719 Lisp_Object val;
720 struct gcpro gcpro1;
721
722 GCPRO1 (name);
723 val = call1 (predicate, name);
724 UNGCPRO;
725
726 if (NILP (val))
727 continue;
728 }
729
730 /* Suitably record this match. */
731
732 matchcount++;
733
734 if (all_flag)
735 bestmatch = Fcons (name, bestmatch);
736 else if (NILP (bestmatch))
737 {
738 bestmatch = name;
739 bestmatchsize = SCHARS (name);
740 }
741 else
742 {
743 Lisp_Object zero = make_number (0);
744 /* FIXME: This is a copy of the code in Ftry_completion. */
745 int compare = min (bestmatchsize, SCHARS (name));
746 Lisp_Object tem
747 = Fcompare_strings (bestmatch, zero,
748 make_number (compare),
749 name, zero,
750 make_number (compare),
751 completion_ignore_case ? Qt : Qnil);
752 int matchsize
753 = (EQ (tem, Qt) ? compare
754 : XINT (tem) < 0 ? - XINT (tem) - 1
755 : XINT (tem) - 1);
756
757 if (completion_ignore_case)
758 {
759 /* If this is an exact match except for case,
760 use it as the best match rather than one that is not
761 an exact match. This way, we get the case pattern
762 of the actual match. */
763 /* This tests that the current file is an exact match
764 but BESTMATCH is not (it is too long). */
765 if ((matchsize == SCHARS (name)
766 && matchsize + !!directoryp < SCHARS (bestmatch))
767 ||
768 /* If there is no exact match ignoring case,
769 prefer a match that does not change the case
770 of the input. */
771 /* If there is more than one exact match aside from
772 case, and one of them is exact including case,
773 prefer that one. */
774 /* This == checks that, of current file and BESTMATCH,
775 either both or neither are exact. */
776 (((matchsize == SCHARS (name))
777 ==
778 (matchsize + !!directoryp == SCHARS (bestmatch)))
779 && (tem = Fcompare_strings (name, zero,
780 make_number (SCHARS (file)),
781 file, zero,
782 Qnil,
783 Qnil),
784 EQ (Qt, tem))
785 && (tem = Fcompare_strings (bestmatch, zero,
786 make_number (SCHARS (file)),
787 file, zero,
788 Qnil,
789 Qnil),
790 ! EQ (Qt, tem))))
791 bestmatch = name;
792 }
793 bestmatchsize = matchsize;
794
795 /* If the best completion so far is reduced to the string
796 we're trying to complete, then we already know there's no
797 other completion, so there's no point looking any further. */
798 if (matchsize <= SCHARS (file)
799 && !includeall /* A future match may allow includeall to 0. */
800 /* If completion-ignore-case is non-nil, don't
801 short-circuit because we want to find the best
802 possible match *including* case differences. */
803 && (!completion_ignore_case || matchsize == 0)
804 /* The return value depends on whether it's the sole match. */
805 && matchcount > 1)
806 break;
807
808 }
809 }
810
811 UNGCPRO;
812 /* This closes the directory. */
813 bestmatch = unbind_to (count, bestmatch);
814
815 if (all_flag || NILP (bestmatch))
816 return bestmatch;
817 /* Return t if the supplied string is an exact match (counting case);
818 it does not require any change to be made. */
819 if (matchcount == 1 && !NILP (Fequal (bestmatch, file)))
820 return Qt;
821 bestmatch = Fsubstring (bestmatch, make_number (0),
822 make_number (bestmatchsize));
823 return bestmatch;
824 }
825
826 /* Compare exactly LEN chars of strings at S1 and S2,
827 ignoring case if appropriate.
828 Return -1 if strings match,
829 else number of chars that match at the beginning. */
830
831 static int
832 scmp (s1, s2, len)
833 register unsigned char *s1, *s2;
834 int len;
835 {
836 register int l = len;
837
838 if (completion_ignore_case)
839 {
840 while (l && DOWNCASE (*s1++) == DOWNCASE (*s2++))
841 l--;
842 }
843 else
844 {
845 while (l && *s1++ == *s2++)
846 l--;
847 }
848 if (l == 0)
849 return -1;
850 else
851 return len - l;
852 }
853
854 static int
855 file_name_completion_stat (dirname, dp, st_addr)
856 Lisp_Object dirname;
857 DIRENTRY *dp;
858 struct stat *st_addr;
859 {
860 int len = NAMLEN (dp);
861 int pos = SCHARS (dirname);
862 int value;
863 char *fullname = (char *) alloca (len + pos + 2);
864
865 #ifdef MSDOS
866 #if __DJGPP__ > 1
867 /* Some fields of struct stat are *very* expensive to compute on MS-DOS,
868 but aren't required here. Avoid computing the following fields:
869 st_inode, st_size and st_nlink for directories, and the execute bits
870 in st_mode for non-directory files with non-standard extensions. */
871
872 unsigned short save_djstat_flags = _djstat_flags;
873
874 _djstat_flags = _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
875 #endif /* __DJGPP__ > 1 */
876 #endif /* MSDOS */
877
878 bcopy (SDATA (dirname), fullname, pos);
879 if (!IS_DIRECTORY_SEP (fullname[pos - 1]))
880 fullname[pos++] = DIRECTORY_SEP;
881
882 bcopy (dp->d_name, fullname + pos, len);
883 fullname[pos + len] = 0;
884
885 #ifdef S_IFLNK
886 /* We want to return success if a link points to a nonexistent file,
887 but we want to return the status for what the link points to,
888 in case it is a directory. */
889 value = lstat (fullname, st_addr);
890 stat (fullname, st_addr);
891 return value;
892 #else
893 value = stat (fullname, st_addr);
894 #ifdef MSDOS
895 #if __DJGPP__ > 1
896 _djstat_flags = save_djstat_flags;
897 #endif /* __DJGPP__ > 1 */
898 #endif /* MSDOS */
899 return value;
900 #endif /* S_IFLNK */
901 }
902 \f
903 Lisp_Object
904 make_time (time)
905 time_t time;
906 {
907 return Fcons (make_number (time >> 16),
908 Fcons (make_number (time & 0177777), Qnil));
909 }
910
911 static char *
912 stat_uname (struct stat *st)
913 {
914 #ifdef WINDOWSNT
915 return st->st_uname;
916 #else
917 struct passwd *pw = (struct passwd *) getpwuid (st->st_uid);
918
919 if (pw)
920 return pw->pw_name;
921 else
922 return NULL;
923 #endif
924 }
925
926 static char *
927 stat_gname (struct stat *st)
928 {
929 #ifdef WINDOWSNT
930 return st->st_gname;
931 #else
932 struct group *gr = (struct group *) getgrgid (st->st_gid);
933
934 if (gr)
935 return gr->gr_name;
936 else
937 return NULL;
938 #endif
939 }
940
941 /* Make an integer or float number for UID and GID, while being
942 careful not to produce negative numbers due to signed integer
943 overflow. */
944 static Lisp_Object
945 make_uid (struct stat *st)
946 {
947 EMACS_INT uid = st->st_uid;
948
949 if (sizeof (st->st_uid) > sizeof (uid) || uid < 0 || FIXNUM_OVERFLOW_P (uid))
950 return make_float ((double)st->st_uid);
951 return make_number (uid);
952 }
953
954 static Lisp_Object
955 make_gid (struct stat *st)
956 {
957 EMACS_INT gid = st->st_gid;
958
959 if (sizeof (st->st_gid) > sizeof (gid) || gid < 0 || FIXNUM_OVERFLOW_P (gid))
960 return make_float ((double)st->st_gid);
961 return make_number (gid);
962 }
963
964 DEFUN ("file-attributes", Ffile_attributes, Sfile_attributes, 1, 2, 0,
965 doc: /* Return a list of attributes of file FILENAME.
966 Value is nil if specified file cannot be opened.
967
968 ID-FORMAT specifies the preferred format of attributes uid and gid (see
969 below) - valid values are 'string and 'integer. The latter is the default,
970 but we plan to change that, so you should specify a non-nil value for
971 ID-FORMAT if you use the returned uid or gid.
972
973 Elements of the attribute list are:
974 0. t for directory, string (name linked to) for symbolic link, or nil.
975 1. Number of links to file.
976 2. File uid as a string or a number. If a string value cannot be
977 looked up, a numeric value, either an integer or a float, is returned.
978 3. File gid, likewise.
979 4. Last access time, as a list of two integers.
980 First integer has high-order 16 bits of time, second has low 16 bits.
981 (See a note below about FAT-based filesystems.)
982 5. Last modification time, likewise.
983 6. Last status change time, likewise.
984 7. Size in bytes.
985 This is a floating point number if the size is too large for an integer.
986 8. File modes, as a string of ten letters or dashes as in ls -l.
987 9. t if file's gid would change if file were deleted and recreated.
988 10. inode number. If inode number is larger than the Emacs integer,
989 but still fits into a 32-bit number, this is a cons cell containing two
990 integers: first the high part, then the low 16 bits. If the inode number
991 is wider than 32 bits, this is a cons cell containing three integers:
992 first the high 24 bits, then middle 24 bits, and finally the low 16 bits.
993 11. Device number. If it is larger than the Emacs integer, this is
994 a cons cell, similar to the inode number.
995
996 On MS-Windows, performance depends on `w32-get-true-file-attributes',
997 which see.
998
999 On some FAT-based filesystems, only the date of last access is recorded,
1000 so last access time will always be midnight of that day. */)
1001 (filename, id_format)
1002 Lisp_Object filename, id_format;
1003 {
1004 Lisp_Object values[12];
1005 Lisp_Object encoded;
1006 struct stat s;
1007 #if defined (BSD4_2) || defined (BSD4_3)
1008 Lisp_Object dirname;
1009 struct stat sdir;
1010 #endif
1011 char modes[10];
1012 Lisp_Object handler;
1013 struct gcpro gcpro1;
1014 EMACS_INT ino, uid, gid;
1015 char *uname = NULL, *gname = NULL;
1016
1017 filename = Fexpand_file_name (filename, Qnil);
1018
1019 /* If the file name has special constructs in it,
1020 call the corresponding file handler. */
1021 handler = Ffind_file_name_handler (filename, Qfile_attributes);
1022 if (!NILP (handler))
1023 { /* Only pass the extra arg if it is used to help backward compatibility
1024 with old file handlers which do not implement the new arg. --Stef */
1025 if (NILP (id_format))
1026 return call2 (handler, Qfile_attributes, filename);
1027 else
1028 return call3 (handler, Qfile_attributes, filename, id_format);
1029 }
1030
1031 GCPRO1 (filename);
1032 encoded = ENCODE_FILE (filename);
1033 UNGCPRO;
1034
1035 if (lstat (SDATA (encoded), &s) < 0)
1036 return Qnil;
1037
1038 switch (s.st_mode & S_IFMT)
1039 {
1040 default:
1041 values[0] = Qnil; break;
1042 case S_IFDIR:
1043 values[0] = Qt; break;
1044 #ifdef S_IFLNK
1045 case S_IFLNK:
1046 values[0] = Ffile_symlink_p (filename); break;
1047 #endif
1048 }
1049 values[1] = make_number (s.st_nlink);
1050
1051 if (!(NILP (id_format) || EQ (id_format, Qinteger)))
1052 {
1053 BLOCK_INPUT;
1054 uname = stat_uname (&s);
1055 gname = stat_gname (&s);
1056 UNBLOCK_INPUT;
1057 }
1058 if (uname)
1059 values[2] = DECODE_SYSTEM (build_string (uname));
1060 else
1061 values[2] = make_uid (&s);
1062 if (gname)
1063 values[3] = DECODE_SYSTEM (build_string (gname));
1064 else
1065 values[3] = make_gid (&s);
1066
1067 values[4] = make_time (s.st_atime);
1068 values[5] = make_time (s.st_mtime);
1069 values[6] = make_time (s.st_ctime);
1070 values[7] = make_number (s.st_size);
1071 /* If the size is out of range for an integer, return a float. */
1072 if (XINT (values[7]) != s.st_size)
1073 values[7] = make_float ((double)s.st_size);
1074 /* If the size is negative, and its type is long, convert it back to
1075 positive. */
1076 if (s.st_size < 0 && sizeof (s.st_size) == sizeof (long))
1077 values[7] = make_float ((double) ((unsigned long) s.st_size));
1078
1079 filemodestring (&s, modes);
1080 values[8] = make_string (modes, 10);
1081 #if defined (BSD4_2) || defined (BSD4_3) /* file gid will be dir gid */
1082 dirname = Ffile_name_directory (filename);
1083 if (! NILP (dirname))
1084 encoded = ENCODE_FILE (dirname);
1085 if (! NILP (dirname) && stat (SDATA (encoded), &sdir) == 0)
1086 values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
1087 else /* if we can't tell, assume worst */
1088 values[9] = Qt;
1089 #else /* file gid will be egid */
1090 values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
1091 #endif /* BSD4_2 (or BSD4_3) */
1092 /* Shut up GCC warnings in FIXNUM_OVERFLOW_P below. */
1093 if (sizeof (s.st_ino) > sizeof (ino))
1094 ino = (EMACS_INT)(s.st_ino & 0xffffffff);
1095 else
1096 ino = s.st_ino;
1097 if (!FIXNUM_OVERFLOW_P (ino)
1098 && (sizeof (s.st_ino) <= sizeof (ino) || (s.st_ino & ~INTMASK) == 0))
1099 /* Keep the most common cases as integers. */
1100 values[10] = make_number (ino);
1101 else if (sizeof (s.st_ino) <= sizeof (ino)
1102 || ((s.st_ino >> 16) & ~INTMASK) == 0)
1103 /* To allow inode numbers larger than VALBITS, separate the bottom
1104 16 bits. */
1105 values[10] = Fcons (make_number ((EMACS_INT)(s.st_ino >> 16)),
1106 make_number ((EMACS_INT)(s.st_ino & 0xffff)));
1107 else
1108 {
1109 /* To allow inode numbers beyond 32 bits, separate into 2 24-bit
1110 high parts and a 16-bit bottom part.
1111 The code on the next line avoids a compiler warning on
1112 systems where st_ino is 32 bit wide. (bug#766). */
1113 EMACS_INT high_ino = s.st_ino >> 31 >> 1;
1114 EMACS_INT low_ino = s.st_ino & 0xffffffff;
1115
1116 values[10] = Fcons (make_number (high_ino >> 8),
1117 Fcons (make_number (((high_ino & 0xff) << 16)
1118 + (low_ino >> 16)),
1119 make_number (low_ino & 0xffff)));
1120 }
1121
1122 /* Likewise for device, but don't let it become negative. We used
1123 to use FIXNUM_OVERFLOW_P here, but that won't catch large
1124 positive numbers such as 0xFFEEDDCC. */
1125 if ((EMACS_INT)s.st_dev < 0
1126 || (EMACS_INT)s.st_dev > MOST_POSITIVE_FIXNUM)
1127 values[11] = Fcons (make_number (s.st_dev >> 16),
1128 make_number (s.st_dev & 0xffff));
1129 else
1130 values[11] = make_number (s.st_dev);
1131
1132 return Flist (sizeof(values) / sizeof(values[0]), values);
1133 }
1134
1135 DEFUN ("file-attributes-lessp", Ffile_attributes_lessp, Sfile_attributes_lessp, 2, 2, 0,
1136 doc: /* Return t if first arg file attributes list is less than second.
1137 Comparison is in lexicographic order and case is significant. */)
1138 (f1, f2)
1139 Lisp_Object f1, f2;
1140 {
1141 return Fstring_lessp (Fcar (f1), Fcar (f2));
1142 }
1143 \f
1144 void
1145 syms_of_dired ()
1146 {
1147 Qdirectory_files = intern ("directory-files");
1148 Qdirectory_files_and_attributes = intern ("directory-files-and-attributes");
1149 Qfile_name_completion = intern ("file-name-completion");
1150 Qfile_name_all_completions = intern ("file-name-all-completions");
1151 Qfile_attributes = intern ("file-attributes");
1152 Qfile_attributes_lessp = intern ("file-attributes-lessp");
1153 Qdefault_directory = intern ("default-directory");
1154
1155 staticpro (&Qdirectory_files);
1156 staticpro (&Qdirectory_files_and_attributes);
1157 staticpro (&Qfile_name_completion);
1158 staticpro (&Qfile_name_all_completions);
1159 staticpro (&Qfile_attributes);
1160 staticpro (&Qfile_attributes_lessp);
1161 staticpro (&Qdefault_directory);
1162
1163 defsubr (&Sdirectory_files);
1164 defsubr (&Sdirectory_files_and_attributes);
1165 defsubr (&Sfile_name_completion);
1166 defsubr (&Sfile_name_all_completions);
1167 defsubr (&Sfile_attributes);
1168 defsubr (&Sfile_attributes_lessp);
1169
1170 DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions,
1171 doc: /* Completion ignores file names ending in any string in this list.
1172 It does not ignore them if all possible completions end in one of
1173 these strings or when displaying a list of completions.
1174 It ignores directory names if they match any string in this list which
1175 ends in a slash. */);
1176 Vcompletion_ignored_extensions = Qnil;
1177 }
1178
1179 /* arch-tag: 1ac8deca-4d8f-4d41-ade9-089154d98c03
1180 (do not change this comment) */