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