0
|
1 /* Lisp functions for making directory listings.
|
|
2 Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.30. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include "lisp.h"
|
|
25
|
|
26 #include "buffer.h"
|
|
27 #include "commands.h"
|
|
28 #include "elhash.h"
|
|
29 #include "regex.h"
|
195
|
30 #include "opaque.h"
|
0
|
31
|
|
32 #include "sysfile.h"
|
|
33 #include "sysdir.h"
|
|
34
|
|
35 Lisp_Object Vcompletion_ignored_extensions;
|
|
36
|
|
37 Lisp_Object Qdirectory_files;
|
|
38 Lisp_Object Qfile_name_completion;
|
|
39 Lisp_Object Qfile_name_all_completions;
|
|
40 Lisp_Object Qfile_attributes;
|
|
41
|
195
|
42 static Lisp_Object
|
209
|
43 close_directory_unwind (Lisp_Object unwind_obj)
|
195
|
44 {
|
|
45 DIR *d = (DIR *)get_opaque_ptr (unwind_obj);
|
|
46 closedir (d);
|
|
47 free_opaque_ptr (unwind_obj);
|
|
48 return Qnil;
|
|
49 }
|
|
50
|
20
|
51 DEFUN ("directory-files", Fdirectory_files, 1, 5, 0, /*
|
0
|
52 Return a list of names of files in DIRECTORY.
|
|
53 There are four optional arguments:
|
|
54 If FULL is non-nil, absolute pathnames of the files are returned.
|
|
55 If MATCH is non-nil, only pathnames containing that regexp are returned.
|
|
56 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
|
|
57 NOSORT is useful if you plan to sort the result yourself.
|
185
|
58 If FILES-ONLY is the symbol t, then only the "files" in the directory
|
0
|
59 will be returned; subdirectories will be excluded. If FILES-ONLY is not
|
|
60 nil and not t, then only the subdirectories will be returned. Otherwise,
|
|
61 if FILES-ONLY is nil (the default) then both files and subdirectories will
|
|
62 be returned.
|
20
|
63 */
|
|
64 (dirname, full, match, nosort, files_only))
|
0
|
65 {
|
120
|
66 /* This function can GC. GC checked 1997.04.06. */
|
0
|
67 DIR *d;
|
195
|
68 Bytecount name_as_dir_length;
|
263
|
69 Lisp_Object list = Qnil, name, dirfilename = Qnil;
|
0
|
70 Lisp_Object handler;
|
173
|
71 struct re_pattern_buffer *bufp = NULL;
|
195
|
72 Lisp_Object name_as_dir = Qnil;
|
|
73 int speccount = specpdl_depth ();
|
|
74 char *statbuf, *statbuf_tail;
|
0
|
75
|
195
|
76 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
77 GCPRO4 (dirname, name_as_dir, dirfilename, list);
|
0
|
78
|
|
79 /* If the file name has special constructs in it,
|
|
80 call the corresponding file handler. */
|
|
81 handler = Ffind_file_name_handler (dirname, Qdirectory_files);
|
|
82 if (!NILP (handler))
|
|
83 {
|
|
84 UNGCPRO;
|
|
85 if (!NILP (files_only))
|
|
86 return call6 (handler, Qdirectory_files, dirname, full, match, nosort,
|
|
87 files_only);
|
|
88 else
|
|
89 return call5 (handler, Qdirectory_files, dirname, full, match,
|
|
90 nosort);
|
|
91 }
|
|
92
|
120
|
93 /* #### why do we do Fexpand_file_name after file handlers here,
|
|
94 but earlier everywhere else? */
|
0
|
95 dirname = Fexpand_file_name (dirname, Qnil);
|
|
96 dirfilename = Fdirectory_file_name (dirname);
|
195
|
97 name_as_dir = Ffile_name_as_directory (dirname);
|
0
|
98
|
195
|
99 name_as_dir_length = XSTRING_LENGTH (name_as_dir);
|
243
|
100 statbuf = (char *) alloca (name_as_dir_length + MAXNAMLEN + 1);
|
195
|
101 memcpy (statbuf, XSTRING_DATA (name_as_dir), name_as_dir_length);
|
|
102 statbuf_tail = statbuf + name_as_dir_length;
|
0
|
103
|
|
104 /* XEmacs: this should come after Ffile_name_as_directory() to avoid
|
195
|
105 potential regexp cache smashage. It comes before the opendir()
|
|
106 because it might signal an error. */
|
0
|
107 if (!NILP (match))
|
|
108 {
|
|
109 CHECK_STRING (match);
|
|
110
|
|
111 /* MATCH might be a flawed regular expression. Rather than
|
|
112 catching and signalling our own errors, we just call
|
|
113 compile_pattern to do the work for us. */
|
|
114 bufp = compile_pattern (match, 0, 0, 0, ERROR_ME);
|
|
115 }
|
|
116
|
|
117 /* Now *bufp is the compiled form of MATCH; don't call anything
|
|
118 which might compile a new regexp until we're done with the loop! */
|
|
119
|
195
|
120 /* Do this opendir after anything which might signal an error;
|
|
121 previosly, there was no unwind-protection in case of error, but
|
|
122 now there is. */
|
14
|
123 d = opendir ((char *) XSTRING_DATA (dirfilename));
|
0
|
124 if (! d)
|
|
125 report_file_error ("Opening directory", list1 (dirname));
|
|
126
|
209
|
127 record_unwind_protect (close_directory_unwind, make_opaque_ptr ((void *)d));
|
195
|
128
|
0
|
129 list = Qnil;
|
|
130
|
|
131 /* Loop reading blocks */
|
|
132 while (1)
|
|
133 {
|
|
134 DIRENTRY *dp = readdir (d);
|
|
135 int len;
|
|
136
|
|
137 if (!dp) break;
|
|
138 len = NAMLEN (dp);
|
|
139 if (DIRENTRY_NONEMPTY (dp))
|
|
140 {
|
|
141 int result;
|
|
142 result = (NILP (match)
|
195
|
143 || (0 <= re_search (bufp, dp->d_name, len, 0, len, 0)));
|
0
|
144 if (result)
|
|
145 {
|
|
146 if (!NILP (files_only))
|
|
147 {
|
|
148 int dir_p;
|
|
149 struct stat st;
|
195
|
150 char *cur_statbuf = statbuf;
|
|
151 char *cur_statbuf_tail = statbuf_tail;
|
0
|
152
|
195
|
153 /* A trick: we normally use the buffer created by
|
209
|
154 alloca. However, if the filename is too big
|
|
155 (meaning MAXNAMLEN is wrong or useless on the
|
|
156 system), we'll use a malloced buffer, and free
|
|
157 it. */
|
195
|
158 if (len > MAXNAMLEN)
|
|
159 {
|
|
160 cur_statbuf = (char *) xmalloc (name_as_dir_length
|
|
161 + len + 1);
|
|
162 memcpy (cur_statbuf, statbuf, name_as_dir_length);
|
|
163 cur_statbuf_tail = cur_statbuf + name_as_dir_length;
|
|
164 }
|
|
165 memcpy (cur_statbuf_tail, dp->d_name, len);
|
|
166 cur_statbuf_tail [len] = 0;
|
0
|
167
|
195
|
168 if (stat (cur_statbuf, &st) < 0)
|
0
|
169 dir_p = 0;
|
|
170 else
|
|
171 dir_p = ((st.st_mode & S_IFMT) == S_IFDIR);
|
|
172
|
195
|
173 if (cur_statbuf != statbuf)
|
|
174 xfree (cur_statbuf);
|
|
175
|
0
|
176 if (EQ (files_only, Qt) && dir_p)
|
|
177 continue;
|
|
178 else if (!EQ (files_only, Qt) && !dir_p)
|
|
179 continue;
|
|
180 }
|
|
181
|
|
182 if (!NILP (full))
|
195
|
183 name = concat2 (name_as_dir,
|
209
|
184 make_ext_string ((Bufbyte *)dp->d_name,
|
219
|
185 len, FORMAT_FILENAME));
|
0
|
186 else
|
209
|
187 name = make_ext_string ((Bufbyte *)dp->d_name,
|
219
|
188 len, FORMAT_FILENAME);
|
0
|
189
|
195
|
190 list = Fcons (name, list);
|
0
|
191 }
|
|
192 }
|
|
193 }
|
195
|
194 unbind_to (speccount, Qnil); /* This will close the dir */
|
0
|
195 if (!NILP (nosort))
|
195
|
196 RETURN_UNGCPRO (list);
|
|
197 else
|
|
198 RETURN_UNGCPRO (Fsort (Fnreverse (list), Qstring_lessp));
|
0
|
199 }
|
|
200
|
185
|
201 static Lisp_Object file_name_completion (Lisp_Object file,
|
|
202 Lisp_Object dirname,
|
0
|
203 int all_flag, int ver_flag);
|
|
204
|
20
|
205 DEFUN ("file-name-completion", Ffile_name_completion, 2, 2, 0, /*
|
0
|
206 Complete file name FILE in directory DIR.
|
|
207 Returns the longest string common to all filenames in DIR
|
|
208 that start with FILE.
|
|
209 If there is only one and FILE matches it exactly, returns t.
|
|
210 Returns nil if DIR contains no name starting with FILE.
|
|
211
|
|
212 Filenames which end with any member of `completion-ignored-extensions'
|
|
213 are not considered as possible completions for FILE unless there is no
|
|
214 other possible completion. `completion-ignored-extensions' is not applied
|
|
215 to the names of directories.
|
20
|
216 */
|
|
217 (file, dirname))
|
0
|
218 {
|
120
|
219 /* This function can GC. GC checked 1996.04.06. */
|
0
|
220 Lisp_Object handler;
|
|
221
|
|
222 /* If the directory name has special constructs in it,
|
|
223 call the corresponding file handler. */
|
|
224 handler = Ffind_file_name_handler (dirname, Qfile_name_completion);
|
|
225 if (!NILP (handler))
|
|
226 return call3 (handler, Qfile_name_completion, file, dirname);
|
|
227
|
|
228 /* If the file name has special constructs in it,
|
|
229 call the corresponding file handler. */
|
|
230 handler = Ffind_file_name_handler (file, Qfile_name_completion);
|
|
231 if (!NILP (handler))
|
|
232 return call3 (handler, Qfile_name_completion, file, dirname);
|
|
233
|
|
234 return file_name_completion (file, dirname, 0, 0);
|
|
235 }
|
|
236
|
20
|
237 DEFUN ("file-name-all-completions", Ffile_name_all_completions, 2, 2, 0, /*
|
0
|
238 Return a list of all completions of file name FILE in directory DIR.
|
|
239 These are all file names in directory DIR which begin with FILE.
|
|
240
|
|
241 Filenames which end with any member of `completion-ignored-extensions'
|
|
242 are not considered as possible completions for FILE unless there is no
|
|
243 other possible completion. `completion-ignored-extensions' is not applied
|
|
244 to the names of directories.
|
20
|
245 */
|
|
246 (file, dirname))
|
0
|
247 {
|
120
|
248 /* This function can GC. GC checked 1997.06.04. */
|
0
|
249 Lisp_Object handler;
|
118
|
250 struct gcpro gcpro1;
|
0
|
251
|
118
|
252 GCPRO1 (dirname);
|
|
253 dirname = Fexpand_file_name (dirname, Qnil);
|
0
|
254 /* If the file name has special constructs in it,
|
|
255 call the corresponding file handler. */
|
|
256 handler = Ffind_file_name_handler (dirname, Qfile_name_all_completions);
|
118
|
257 UNGCPRO;
|
0
|
258 if (!NILP (handler))
|
|
259 return call3 (handler, Qfile_name_all_completions, file,
|
|
260 dirname);
|
|
261
|
|
262 return file_name_completion (file, dirname, 1, 0);
|
|
263 }
|
|
264
|
|
265 static int
|
|
266 file_name_completion_stat (Lisp_Object dirname, DIRENTRY *dp,
|
|
267 struct stat *st_addr)
|
|
268 {
|
|
269 Bytecount len = NAMLEN (dp);
|
14
|
270 Bytecount pos = XSTRING_LENGTH (dirname);
|
0
|
271 int value;
|
|
272 char *fullname = (char *) alloca (len + pos + 2);
|
|
273
|
14
|
274 memcpy (fullname, XSTRING_DATA (dirname), pos);
|
0
|
275 if (!IS_DIRECTORY_SEP (fullname[pos - 1]))
|
|
276 fullname[pos++] = DIRECTORY_SEP;
|
|
277
|
|
278 memcpy (fullname + pos, dp->d_name, len);
|
|
279 fullname[pos + len] = 0;
|
|
280
|
|
281 #ifdef S_IFLNK
|
|
282 /* We want to return success if a link points to a nonexistent file,
|
|
283 but we want to return the status for what the link points to,
|
|
284 in case it is a directory. */
|
|
285 value = lstat (fullname, st_addr);
|
|
286 if (S_ISLNK (st_addr->st_mode))
|
|
287 stat (fullname, st_addr);
|
|
288 #else
|
|
289 value = stat (fullname, st_addr);
|
|
290 #endif
|
173
|
291 return value;
|
0
|
292 }
|
|
293
|
|
294 static Lisp_Object
|
219
|
295 file_name_completion_unwind (Lisp_Object unwind_obj)
|
|
296 {
|
|
297 DIR *d;
|
|
298 Lisp_Object obj = XCAR (unwind_obj);
|
|
299
|
|
300 if (NILP (obj))
|
|
301 return Qnil;
|
|
302 d = (DIR *)get_opaque_ptr (obj);
|
|
303 closedir (d);
|
|
304 free_opaque_ptr (obj);
|
227
|
305 free_cons (XCONS (unwind_obj));
|
219
|
306 return Qnil;
|
|
307 }
|
|
308
|
|
309 static Lisp_Object
|
0
|
310 file_name_completion (Lisp_Object file, Lisp_Object dirname, int all_flag,
|
|
311 int ver_flag)
|
|
312 {
|
|
313 /* This function can GC */
|
|
314 DIR *d = 0;
|
|
315 int matchcount = 0;
|
|
316 Lisp_Object bestmatch = Qnil;
|
|
317 Charcount bestmatchsize = 0;
|
|
318 struct stat st;
|
|
319 int passcount;
|
|
320 int speccount = specpdl_depth ();
|
|
321 Charcount file_name_length;
|
|
322 DIRENTRY *((*readfunc) (DIR *)) = readdir;
|
219
|
323 Lisp_Object unwind_closure;
|
0
|
324 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
325
|
|
326 GCPRO3 (file, dirname, bestmatch);
|
|
327
|
|
328 CHECK_STRING (file);
|
|
329
|
219
|
330 #ifdef WINDOWSNT
|
|
331 /* Filename completion on Windows ignores case, since Windows
|
|
332 filesystems do. */
|
0
|
333 specbind (Qcompletion_ignore_case, Qt);
|
219
|
334 #endif /* HAVE_WINDOWS */
|
0
|
335
|
|
336 #ifdef FILE_SYSTEM_CASE
|
|
337 file = FILE_SYSTEM_CASE (file);
|
|
338 #endif
|
|
339 dirname = Fexpand_file_name (dirname, Qnil);
|
|
340 file_name_length = string_char_length (XSTRING (file));
|
|
341
|
|
342 /* With passcount = 0, ignore files that end in an ignored extension.
|
|
343 If nothing found then try again with passcount = 1, don't ignore them.
|
|
344 If looking for all completions, start with passcount = 1,
|
|
345 so always take even the ignored ones.
|
|
346
|
|
347 ** It would not actually be helpful to the user to ignore any possible
|
|
348 completions when making a list of them.** */
|
|
349
|
219
|
350 /* We cannot use close_directory_unwind() because we change the
|
|
351 directory. The old code used to just avoid signaling errors, and
|
|
352 call closedir, but it was wrong, because it made sane handling of
|
|
353 QUIT impossible and, besides, various utility functions like
|
|
354 regexp_ignore_completion_p can signal errors. */
|
227
|
355 unwind_closure = noseeum_cons (Qnil, Qnil);
|
219
|
356 record_unwind_protect (file_name_completion_unwind, unwind_closure);
|
|
357
|
0
|
358 for (passcount = !!all_flag; NILP (bestmatch) && passcount < 2; passcount++)
|
|
359 {
|
16
|
360 d = opendir ((char *) XSTRING_DATA (Fdirectory_file_name (dirname)));
|
0
|
361 if (!d)
|
|
362 report_file_error ("Opening directory", list1 (dirname));
|
219
|
363 XCAR (unwind_closure) = make_opaque_ptr ((void *)d);
|
0
|
364
|
|
365 /* Loop reading blocks */
|
|
366 while (1)
|
|
367 {
|
|
368 DIRENTRY *dp;
|
|
369 Bytecount len;
|
|
370 /* scmp() works in characters, not bytes, so we have to compute
|
|
371 this value: */
|
|
372 Charcount cclen;
|
|
373 int directoryp;
|
|
374 int ignored_extension_p = 0;
|
|
375 Bufbyte *d_name;
|
|
376
|
|
377 dp = (*readfunc) (d);
|
|
378 if (!dp) break;
|
|
379
|
219
|
380 /* #### This is a bad idea, because d_name can contain
|
|
381 control characters, which can make XEmacs crash. This
|
|
382 should be handled properly with FORMAT_FILENAME. */
|
0
|
383 d_name = (Bufbyte *) dp->d_name;
|
|
384 len = NAMLEN (dp);
|
2
|
385 cclen = bytecount_to_charcount (d_name, len);
|
0
|
386
|
219
|
387 QUIT;
|
0
|
388
|
|
389 if (! DIRENTRY_NONEMPTY (dp)
|
|
390 || cclen < file_name_length
|
14
|
391 || 0 <= scmp (d_name, XSTRING_DATA (file), file_name_length))
|
0
|
392 continue;
|
|
393
|
|
394 if (file_name_completion_stat (dirname, dp, &st) < 0)
|
|
395 continue;
|
|
396
|
|
397 directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
|
|
398 if (directoryp)
|
|
399 {
|
|
400 #ifndef TRIVIAL_DIRECTORY_ENTRY
|
|
401 #define TRIVIAL_DIRECTORY_ENTRY(n) (!strcmp (n, ".") || !strcmp (n, ".."))
|
|
402 #endif
|
|
403 /* "." and ".." are never interesting as completions, but are
|
2
|
404 actually in the way in a directory containing only one file. */
|
0
|
405 if (!passcount && TRIVIAL_DIRECTORY_ENTRY (dp->d_name))
|
|
406 continue;
|
|
407 }
|
|
408 else
|
|
409 {
|
|
410 /* Compare extensions-to-be-ignored against end of this file name */
|
2
|
411 /* if name is not an exact match against specified string. */
|
0
|
412 if (!passcount && cclen > file_name_length)
|
|
413 {
|
|
414 Lisp_Object tem;
|
|
415 /* and exit this for loop if a match is found */
|
219
|
416 EXTERNAL_LIST_LOOP (tem, Vcompletion_ignored_extensions)
|
0
|
417 {
|
|
418 Lisp_Object elt = XCAR (tem);
|
|
419 Charcount skip;
|
|
420
|
219
|
421 CHECK_STRING (elt);
|
|
422
|
0
|
423 skip = cclen - string_char_length (XSTRING (elt));
|
|
424 if (skip < 0) continue;
|
|
425
|
|
426 if (0 > scmp (charptr_n_addr (d_name, skip),
|
14
|
427 XSTRING_DATA (elt),
|
0
|
428 string_char_length (XSTRING (elt))))
|
|
429 {
|
|
430 ignored_extension_p = 1;
|
|
431 break;
|
|
432 }
|
|
433 }
|
|
434 }
|
|
435 }
|
|
436
|
|
437 /* If an ignored-extensions match was found,
|
|
438 don't process this name as a completion. */
|
|
439 if (!passcount && ignored_extension_p)
|
|
440 continue;
|
|
441
|
2
|
442 if (!passcount && regexp_ignore_completion_p (d_name, Qnil, 0, cclen))
|
0
|
443 continue;
|
|
444
|
|
445 /* Update computation of how much all possible completions match */
|
|
446 matchcount++;
|
|
447
|
|
448 if (all_flag || NILP (bestmatch))
|
|
449 {
|
|
450 Lisp_Object name = Qnil;
|
|
451 struct gcpro ngcpro1;
|
|
452 NGCPRO1 (name);
|
|
453 /* This is a possible completion */
|
2
|
454 name = make_string (d_name, len);
|
|
455 if (directoryp) /* Completion is a directory; end it with '/' */
|
|
456 name = Ffile_name_as_directory (name);
|
0
|
457 if (all_flag)
|
|
458 {
|
|
459 bestmatch = Fcons (name, bestmatch);
|
|
460 }
|
|
461 else
|
|
462 {
|
|
463 bestmatch = name;
|
|
464 bestmatchsize = string_char_length (XSTRING (name));
|
|
465 }
|
|
466 NUNGCPRO;
|
|
467 }
|
|
468 else
|
|
469 {
|
|
470 Charcount compare = min (bestmatchsize, cclen);
|
14
|
471 Bufbyte *p1 = XSTRING_DATA (bestmatch);
|
0
|
472 Bufbyte *p2 = d_name;
|
|
473 Charcount matchsize = scmp (p1, p2, compare);
|
|
474
|
|
475 if (matchsize < 0)
|
|
476 matchsize = compare;
|
|
477 if (completion_ignore_case)
|
|
478 {
|
|
479 /* If this is an exact match except for case,
|
|
480 use it as the best match rather than one that is not
|
|
481 an exact match. This way, we get the case pattern
|
|
482 of the actual match. */
|
2
|
483 if ((matchsize == cclen
|
185
|
484 && matchsize + !!directoryp
|
0
|
485 < string_char_length (XSTRING (bestmatch)))
|
|
486 ||
|
|
487 /* If there is no exact match ignoring case,
|
|
488 prefer a match that does not change the case
|
|
489 of the input. */
|
2
|
490 (((matchsize == cclen)
|
0
|
491 ==
|
185
|
492 (matchsize + !!directoryp
|
0
|
493 == string_char_length (XSTRING (bestmatch))))
|
|
494 /* If there is more than one exact match aside from
|
|
495 case, and one of them is exact including case,
|
|
496 prefer that one. */
|
14
|
497 && 0 > scmp_1 (p2, XSTRING_DATA (file),
|
0
|
498 file_name_length, 0)
|
14
|
499 && 0 <= scmp_1 (p1, XSTRING_DATA (file),
|
0
|
500 file_name_length, 0)))
|
|
501 {
|
2
|
502 bestmatch = make_string (d_name, len);
|
0
|
503 if (directoryp)
|
2
|
504 bestmatch = Ffile_name_as_directory (bestmatch);
|
0
|
505 }
|
|
506 }
|
|
507
|
|
508 /* If this dirname all matches,
|
|
509 see if implicit following slash does too. */
|
|
510 if (directoryp
|
|
511 && compare == matchsize
|
|
512 && bestmatchsize > matchsize
|
|
513 && IS_ANY_SEP (charptr_emchar_n (p1, matchsize)))
|
|
514 matchsize++;
|
|
515 bestmatchsize = matchsize;
|
|
516 }
|
|
517 }
|
|
518 closedir (d);
|
219
|
519 free_opaque_ptr (XCAR (unwind_closure));
|
|
520 XCAR (unwind_closure) = Qnil;
|
0
|
521 }
|
|
522
|
|
523 unbind_to (speccount, Qnil);
|
|
524
|
|
525 UNGCPRO;
|
|
526
|
|
527 if (all_flag || NILP (bestmatch))
|
|
528 return bestmatch;
|
|
529 if (matchcount == 1 && bestmatchsize == file_name_length)
|
|
530 return Qt;
|
227
|
531 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize));
|
0
|
532 }
|
|
533
|
|
534
|
|
535 Lisp_Object
|
|
536 make_directory_hash_table (char *path)
|
|
537 {
|
|
538 DIR *d;
|
|
539 DIRENTRY *dp;
|
|
540 Bytecount len;
|
|
541 Lisp_Object hash = make_lisp_hashtable (100, HASHTABLE_NONWEAK,
|
|
542 HASHTABLE_EQUAL);
|
|
543 if ((d = opendir (path)))
|
|
544 {
|
|
545 while ((dp = readdir (d)))
|
|
546 {
|
|
547 len = NAMLEN (dp);
|
|
548 if (DIRENTRY_NONEMPTY (dp))
|
209
|
549 Fputhash (make_ext_string ((Bufbyte *) dp->d_name, len,
|
219
|
550 FORMAT_FILENAME), Qt, hash);
|
0
|
551 }
|
|
552 closedir (d);
|
|
553 }
|
|
554 return hash;
|
|
555 }
|
|
556
|
|
557 Lisp_Object
|
|
558 wasteful_word_to_lisp (unsigned int item)
|
|
559 {
|
|
560 /* Compatibility: in other versions, file-attributes returns a LIST
|
|
561 of two 16 bit integers... */
|
|
562 Lisp_Object cons = word_to_lisp (item);
|
|
563 XCDR (cons) = Fcons (XCDR (cons), Qnil);
|
|
564 return cons;
|
|
565 }
|
|
566
|
20
|
567 DEFUN ("file-attributes", Ffile_attributes, 1, 1, 0, /*
|
0
|
568 Return a list of attributes of file FILENAME.
|
|
569 Value is nil if specified file cannot be opened.
|
|
570 Otherwise, list elements are:
|
|
571 0. t for directory, string (name linked to) for symbolic link, or nil.
|
|
572 1. Number of links to file.
|
|
573 2. File uid.
|
|
574 3. File gid.
|
|
575 4. Last access time, as a list of two integers.
|
|
576 First integer has high-order 16 bits of time, second has low 16 bits.
|
|
577 5. Last modification time, likewise.
|
|
578 6. Last status change time, likewise.
|
|
579 7. Size in bytes. (-1, if number is out of range).
|
|
580 8. File modes, as a string of ten letters or dashes as in ls -l.
|
|
581 9. t iff file's gid would change if file were deleted and recreated.
|
|
582 10. inode number.
|
|
583 11. Device number.
|
|
584
|
|
585 If file does not exist, returns nil.
|
20
|
586 */
|
|
587 (filename))
|
0
|
588 {
|
120
|
589 /* This function can GC. GC checked 1997.06.04. */
|
0
|
590 Lisp_Object values[12];
|
|
591 Lisp_Object dirname = Qnil;
|
|
592 struct stat s;
|
|
593 char modes[10];
|
|
594 Lisp_Object handler;
|
|
595 struct gcpro gcpro1, gcpro2;
|
|
596
|
120
|
597 GCPRO2 (filename, dirname);
|
0
|
598 filename = Fexpand_file_name (filename, Qnil);
|
|
599
|
|
600 /* If the file name has special constructs in it,
|
|
601 call the corresponding file handler. */
|
|
602 handler = Ffind_file_name_handler (filename, Qfile_attributes);
|
|
603 if (!NILP (handler))
|
120
|
604 {
|
|
605 UNGCPRO;
|
|
606 return call2 (handler, Qfile_attributes, filename);
|
|
607 }
|
0
|
608
|
14
|
609 if (lstat ((char *) XSTRING_DATA (filename), &s) < 0)
|
120
|
610 {
|
|
611 UNGCPRO;
|
|
612 return Qnil;
|
|
613 }
|
0
|
614
|
|
615 #ifdef BSD4_2
|
|
616 dirname = Ffile_name_directory (filename);
|
|
617 #endif
|
|
618
|
|
619 #ifdef MSDOS
|
|
620 {
|
14
|
621 char *tmpnam = (char *) XSTRING_DATA (Ffile_name_nondirectory (filename));
|
0
|
622 int l = strlen (tmpnam);
|
|
623
|
185
|
624 if (l >= 5
|
0
|
625 && S_ISREG (s.st_mode)
|
14
|
626 && (stricmp (&tmpnam[l - 4], ".com") == 0 ||
|
|
627 stricmp (&tmpnam[l - 4], ".exe") == 0 ||
|
|
628 stricmp (&tmpnam[l - 4], ".bat") == 0))
|
0
|
629 {
|
|
630 s.st_mode |= S_IEXEC;
|
|
631 }
|
|
632 }
|
|
633 #endif /* MSDOS */
|
|
634
|
|
635 switch (s.st_mode & S_IFMT)
|
|
636 {
|
|
637 default:
|
|
638 values[0] = Qnil;
|
|
639 break;
|
|
640 case S_IFDIR:
|
|
641 values[0] = Qt;
|
|
642 break;
|
|
643 #ifdef S_IFLNK
|
|
644 case S_IFLNK:
|
|
645 values[0] = Ffile_symlink_p (filename);
|
|
646 break;
|
|
647 #endif
|
|
648 }
|
|
649 values[1] = make_int (s.st_nlink);
|
|
650 values[2] = make_int (s.st_uid);
|
|
651 values[3] = make_int (s.st_gid);
|
|
652 values[4] = wasteful_word_to_lisp (s.st_atime);
|
|
653 values[5] = wasteful_word_to_lisp (s.st_mtime);
|
|
654 values[6] = wasteful_word_to_lisp (s.st_ctime);
|
|
655 values[7] = make_int ((EMACS_INT) s.st_size);
|
|
656 /* If the size is out of range, give back -1. */
|
|
657 /* #### Fix when Emacs gets bignums! */
|
|
658 if (XINT (values[7]) != s.st_size)
|
|
659 XSETINT (values[7], -1);
|
|
660 filemodestring (&s, modes);
|
|
661 values[8] = make_string ((Bufbyte *) modes, 10);
|
|
662 #if defined (BSD4_2) || defined (BSD4_3) /* file gid will be dir gid */
|
|
663 {
|
|
664 struct stat sdir;
|
|
665
|
14
|
666 if (!NILP (dirname) && stat ((char *) XSTRING_DATA (dirname), &sdir) == 0)
|
0
|
667 values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
|
|
668 else /* if we can't tell, assume worst */
|
|
669 values[9] = Qt;
|
|
670 }
|
|
671 #else /* file gid will be egid */
|
|
672 values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
|
|
673 #endif /* BSD4_2 or BSD4_3 */
|
|
674 values[10] = make_int (s.st_ino);
|
|
675 values[11] = make_int (s.st_dev);
|
|
676 UNGCPRO;
|
|
677 return Flist (countof (values), values);
|
|
678 }
|
|
679
|
|
680
|
|
681 /************************************************************************/
|
|
682 /* initialization */
|
|
683 /************************************************************************/
|
|
684
|
|
685 void
|
|
686 syms_of_dired (void)
|
|
687 {
|
|
688 defsymbol (&Qdirectory_files, "directory-files");
|
|
689 defsymbol (&Qfile_name_completion, "file-name-completion");
|
|
690 defsymbol (&Qfile_name_all_completions, "file-name-all-completions");
|
|
691 defsymbol (&Qfile_attributes, "file-attributes");
|
|
692
|
20
|
693 DEFSUBR (Fdirectory_files);
|
|
694 DEFSUBR (Ffile_name_completion);
|
|
695 DEFSUBR (Ffile_name_all_completions);
|
|
696 DEFSUBR (Ffile_attributes);
|
0
|
697 }
|
|
698
|
|
699 void
|
|
700 vars_of_dired (void)
|
|
701 {
|
|
702 DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions /*
|
|
703 *Completion ignores filenames ending in any string in this list.
|
|
704 This variable does not affect lists of possible completions,
|
|
705 but does affect the commands that actually do completions.
|
|
706 It is used by the functions `file-name-completion' and
|
|
707 `file-name-all-completions'.
|
|
708 */ );
|
|
709 Vcompletion_ignored_extensions = Qnil;
|
|
710 }
|