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