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