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