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 {
|
|
56 /* This function can GC */
|
|
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
|
|
63 char statbuf [MAXNAMLEN+2];
|
|
64 char *statbuf_tail;
|
2
|
65 Lisp_Object tail_cons = Qnil;
|
0
|
66 char slashfilename[MAXNAMLEN+2];
|
|
67 char *filename = slashfilename;
|
|
68
|
|
69 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
70
|
|
71 /* #### Needs more gcpro's */
|
|
72 GCPRO5 (dirname, match, files_only, tail_cons, dirfilename);
|
|
73
|
|
74 /* If the file name has special constructs in it,
|
|
75 call the corresponding file handler. */
|
|
76 handler = Ffind_file_name_handler (dirname, Qdirectory_files);
|
|
77 if (!NILP (handler))
|
|
78 {
|
|
79 UNGCPRO;
|
|
80 if (!NILP (files_only))
|
|
81 return call6 (handler, Qdirectory_files, dirname, full, match, nosort,
|
|
82 files_only);
|
|
83 else
|
|
84 return call5 (handler, Qdirectory_files, dirname, full, match,
|
|
85 nosort);
|
|
86 }
|
|
87
|
|
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
|
14
|
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 {
|
|
228 /* This function can GC */
|
|
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 {
|
|
257 /* This function can GC */
|
|
258 Lisp_Object handler;
|
|
259
|
|
260 /* If the file name has special constructs in it,
|
|
261 call the corresponding file handler. */
|
|
262 handler = Ffind_file_name_handler (dirname, Qfile_name_all_completions);
|
|
263 if (!NILP (handler))
|
|
264 return call3 (handler, Qfile_name_all_completions, file,
|
|
265 dirname);
|
|
266
|
|
267 return file_name_completion (file, dirname, 1, 0);
|
|
268 }
|
|
269
|
|
270 static int
|
|
271 file_name_completion_stat (Lisp_Object dirname, DIRENTRY *dp,
|
|
272 struct stat *st_addr)
|
|
273 {
|
|
274 Bytecount len = NAMLEN (dp);
|
14
|
275 Bytecount pos = XSTRING_LENGTH (dirname);
|
0
|
276 int value;
|
|
277 char *fullname = (char *) alloca (len + pos + 2);
|
|
278
|
14
|
279 memcpy (fullname, XSTRING_DATA (dirname), pos);
|
0
|
280 #ifndef VMS
|
|
281 if (!IS_DIRECTORY_SEP (fullname[pos - 1]))
|
|
282 fullname[pos++] = DIRECTORY_SEP;
|
|
283 #endif
|
|
284
|
|
285 memcpy (fullname + pos, dp->d_name, len);
|
|
286 fullname[pos + len] = 0;
|
|
287
|
|
288 #ifdef S_IFLNK
|
|
289 /* We want to return success if a link points to a nonexistent file,
|
|
290 but we want to return the status for what the link points to,
|
|
291 in case it is a directory. */
|
|
292 value = lstat (fullname, st_addr);
|
|
293 if (S_ISLNK (st_addr->st_mode))
|
|
294 stat (fullname, st_addr);
|
|
295 #else
|
|
296 value = stat (fullname, st_addr);
|
|
297 #endif
|
|
298 return (value);
|
|
299 }
|
|
300
|
|
301 static Lisp_Object
|
|
302 file_name_completion (Lisp_Object file, Lisp_Object dirname, int all_flag,
|
|
303 int ver_flag)
|
|
304 {
|
|
305 /* This function can GC */
|
|
306 DIR *d = 0;
|
|
307 int matchcount = 0;
|
|
308 Lisp_Object bestmatch = Qnil;
|
|
309 Charcount bestmatchsize = 0;
|
|
310 struct stat st;
|
|
311 int passcount;
|
|
312 int speccount = specpdl_depth ();
|
|
313 Charcount file_name_length;
|
|
314 DIRENTRY *((*readfunc) (DIR *)) = readdir;
|
|
315 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
316
|
|
317 GCPRO3 (file, dirname, bestmatch);
|
|
318
|
|
319 CHECK_STRING (file);
|
|
320
|
|
321 #ifdef VMS
|
|
322 /* Filename completion on VMS ignores case, since VMS filesys does. */
|
|
323 specbind (Qcompletion_ignore_case, Qt);
|
|
324
|
|
325 if (ver_flag)
|
|
326 readfunc = readdirver;
|
|
327 #endif /* VMS */
|
|
328
|
|
329 #ifdef FILE_SYSTEM_CASE
|
|
330 file = FILE_SYSTEM_CASE (file);
|
|
331 #endif
|
|
332 dirname = Fexpand_file_name (dirname, Qnil);
|
|
333 file_name_length = string_char_length (XSTRING (file));
|
|
334
|
|
335 /* With passcount = 0, ignore files that end in an ignored extension.
|
|
336 If nothing found then try again with passcount = 1, don't ignore them.
|
|
337 If looking for all completions, start with passcount = 1,
|
|
338 so always take even the ignored ones.
|
|
339
|
|
340 ** It would not actually be helpful to the user to ignore any possible
|
|
341 completions when making a list of them.** */
|
|
342
|
|
343 for (passcount = !!all_flag; NILP (bestmatch) && passcount < 2; passcount++)
|
|
344 {
|
16
|
345 d = opendir ((char *) XSTRING_DATA (Fdirectory_file_name (dirname)));
|
0
|
346 if (!d)
|
|
347 report_file_error ("Opening directory", list1 (dirname));
|
|
348
|
|
349 /* Loop reading blocks */
|
|
350 while (1)
|
|
351 {
|
|
352 DIRENTRY *dp;
|
|
353 Bytecount len;
|
|
354 /* scmp() works in characters, not bytes, so we have to compute
|
|
355 this value: */
|
|
356 Charcount cclen;
|
|
357 int directoryp;
|
|
358 int ignored_extension_p = 0;
|
|
359 Bufbyte *d_name;
|
|
360
|
|
361 dp = (*readfunc) (d);
|
|
362 if (!dp) break;
|
|
363
|
|
364 d_name = (Bufbyte *) dp->d_name;
|
|
365 len = NAMLEN (dp);
|
2
|
366 cclen = bytecount_to_charcount (d_name, len);
|
0
|
367
|
|
368 /* Can't just use QUIT because we have to make sure the file
|
|
369 descriptor gets closed. */
|
|
370 if (QUITP)
|
|
371 {
|
|
372 closedir (d);
|
|
373 signal_quit ();
|
|
374 }
|
|
375
|
|
376 if (! DIRENTRY_NONEMPTY (dp)
|
|
377 || cclen < file_name_length
|
14
|
378 || 0 <= scmp (d_name, XSTRING_DATA (file), file_name_length))
|
0
|
379 continue;
|
|
380
|
|
381 if (file_name_completion_stat (dirname, dp, &st) < 0)
|
|
382 continue;
|
|
383
|
|
384 directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
|
|
385 if (directoryp)
|
|
386 {
|
|
387 #ifndef TRIVIAL_DIRECTORY_ENTRY
|
|
388 #define TRIVIAL_DIRECTORY_ENTRY(n) (!strcmp (n, ".") || !strcmp (n, ".."))
|
|
389 #endif
|
|
390 /* "." and ".." are never interesting as completions, but are
|
2
|
391 actually in the way in a directory containing only one file. */
|
0
|
392 if (!passcount && TRIVIAL_DIRECTORY_ENTRY (dp->d_name))
|
|
393 continue;
|
|
394 }
|
|
395 else
|
|
396 {
|
|
397 /* Compare extensions-to-be-ignored against end of this file name */
|
2
|
398 /* if name is not an exact match against specified string. */
|
0
|
399 if (!passcount && cclen > file_name_length)
|
|
400 {
|
|
401 Lisp_Object tem;
|
|
402 /* and exit this for loop if a match is found */
|
|
403 for (tem = Vcompletion_ignored_extensions;
|
|
404 CONSP (tem);
|
|
405 tem = XCDR (tem))
|
|
406 {
|
|
407 Lisp_Object elt = XCAR (tem);
|
|
408 Charcount skip;
|
|
409
|
|
410 if (!STRINGP (elt)) continue;
|
|
411 skip = cclen - string_char_length (XSTRING (elt));
|
|
412 if (skip < 0) continue;
|
|
413
|
|
414 if (0 > scmp (charptr_n_addr (d_name, skip),
|
14
|
415 XSTRING_DATA (elt),
|
0
|
416 string_char_length (XSTRING (elt))))
|
|
417 {
|
|
418 ignored_extension_p = 1;
|
|
419 break;
|
|
420 }
|
|
421 }
|
|
422 }
|
|
423 }
|
|
424
|
|
425 /* If an ignored-extensions match was found,
|
|
426 don't process this name as a completion. */
|
|
427 if (!passcount && ignored_extension_p)
|
|
428 continue;
|
|
429
|
2
|
430 if (!passcount && regexp_ignore_completion_p (d_name, Qnil, 0, cclen))
|
0
|
431 continue;
|
|
432
|
|
433 /* Update computation of how much all possible completions match */
|
|
434 matchcount++;
|
|
435
|
|
436 if (all_flag || NILP (bestmatch))
|
|
437 {
|
|
438 Lisp_Object name = Qnil;
|
|
439 struct gcpro ngcpro1;
|
|
440 NGCPRO1 (name);
|
|
441 /* This is a possible completion */
|
2
|
442 name = make_string (d_name, len);
|
|
443 if (directoryp) /* Completion is a directory; end it with '/' */
|
|
444 name = Ffile_name_as_directory (name);
|
0
|
445 if (all_flag)
|
|
446 {
|
|
447 bestmatch = Fcons (name, bestmatch);
|
|
448 }
|
|
449 else
|
|
450 {
|
|
451 bestmatch = name;
|
|
452 bestmatchsize = string_char_length (XSTRING (name));
|
|
453 }
|
|
454 NUNGCPRO;
|
|
455 }
|
|
456 else
|
|
457 {
|
|
458 Charcount compare = min (bestmatchsize, cclen);
|
14
|
459 Bufbyte *p1 = XSTRING_DATA (bestmatch);
|
0
|
460 Bufbyte *p2 = d_name;
|
|
461 Charcount matchsize = scmp (p1, p2, compare);
|
|
462
|
|
463 if (matchsize < 0)
|
|
464 matchsize = compare;
|
|
465 if (completion_ignore_case)
|
|
466 {
|
|
467 /* If this is an exact match except for case,
|
|
468 use it as the best match rather than one that is not
|
|
469 an exact match. This way, we get the case pattern
|
|
470 of the actual match. */
|
2
|
471 if ((matchsize == cclen
|
0
|
472 && matchsize + !!directoryp
|
|
473 < string_char_length (XSTRING (bestmatch)))
|
|
474 ||
|
|
475 /* If there is no exact match ignoring case,
|
|
476 prefer a match that does not change the case
|
|
477 of the input. */
|
2
|
478 (((matchsize == cclen)
|
0
|
479 ==
|
|
480 (matchsize + !!directoryp
|
|
481 == string_char_length (XSTRING (bestmatch))))
|
|
482 /* If there is more than one exact match aside from
|
|
483 case, and one of them is exact including case,
|
|
484 prefer that one. */
|
14
|
485 && 0 > scmp_1 (p2, XSTRING_DATA (file),
|
0
|
486 file_name_length, 0)
|
14
|
487 && 0 <= scmp_1 (p1, XSTRING_DATA (file),
|
0
|
488 file_name_length, 0)))
|
|
489 {
|
2
|
490 bestmatch = make_string (d_name, len);
|
0
|
491 if (directoryp)
|
2
|
492 bestmatch = Ffile_name_as_directory (bestmatch);
|
0
|
493 }
|
|
494 }
|
|
495
|
|
496 /* If this dirname all matches,
|
|
497 see if implicit following slash does too. */
|
|
498 if (directoryp
|
|
499 && compare == matchsize
|
|
500 && bestmatchsize > matchsize
|
|
501 && IS_ANY_SEP (charptr_emchar_n (p1, matchsize)))
|
|
502 matchsize++;
|
|
503 bestmatchsize = matchsize;
|
|
504 }
|
|
505 }
|
|
506 closedir (d);
|
|
507 }
|
|
508
|
|
509 unbind_to (speccount, Qnil);
|
|
510
|
|
511 UNGCPRO;
|
|
512
|
|
513 if (all_flag || NILP (bestmatch))
|
|
514 return bestmatch;
|
|
515 if (matchcount == 1 && bestmatchsize == file_name_length)
|
|
516 return Qt;
|
|
517 return Fsubstring (bestmatch, make_int (0), make_int (bestmatchsize));
|
|
518 }
|
|
519
|
|
520
|
|
521 Lisp_Object
|
|
522 make_directory_hash_table (char *path)
|
|
523 {
|
|
524 DIR *d;
|
|
525 DIRENTRY *dp;
|
|
526 Bytecount len;
|
|
527 Lisp_Object hash = make_lisp_hashtable (100, HASHTABLE_NONWEAK,
|
|
528 HASHTABLE_EQUAL);
|
|
529 if ((d = opendir (path)))
|
|
530 {
|
|
531 while ((dp = readdir (d)))
|
|
532 {
|
|
533 len = NAMLEN (dp);
|
|
534 if (DIRENTRY_NONEMPTY (dp))
|
|
535 Fputhash (make_string ((Bufbyte *) dp->d_name, len), Qt, hash);
|
|
536 }
|
|
537 closedir (d);
|
|
538 }
|
|
539 return hash;
|
|
540 }
|
|
541
|
|
542 #ifdef VMS
|
|
543
|
20
|
544 DEFUN ("file-name-all-versions", Ffile_name_all_versions, 2, 2, 0, /*
|
0
|
545 Return a list of all versions of file name FILE in directory DIR.
|
20
|
546 */
|
|
547 (file, dirname))
|
0
|
548 {
|
|
549 /* This function can GC */
|
|
550 return file_name_completion (file, dirname, 1, 1);
|
|
551 }
|
|
552
|
20
|
553 DEFUN ("file-version-limit", Ffile_version_limit, 1, 1, 0, /*
|
0
|
554 Return the maximum number of versions allowed for FILE.
|
|
555 Returns nil if the file cannot be opened or if there is no version limit.
|
20
|
556 */
|
|
557 (filename))
|
0
|
558 {
|
|
559 /* This function can GC */
|
|
560 Lisp_Object retval;
|
|
561 struct FAB fab;
|
|
562 struct RAB rab;
|
|
563 struct XABFHC xabfhc;
|
|
564 int status;
|
|
565
|
|
566 filename = Fexpand_file_name (filename, Qnil);
|
|
567 CHECK_STRING (filename);
|
|
568 fab = cc$rms_fab;
|
|
569 xabfhc = cc$rms_xabfhc;
|
14
|
570 fab.fab$l_fna = XSTRING_DATA (filename);
|
0
|
571 fab.fab$b_fns = strlen (fab.fab$l_fna);
|
|
572 fab.fab$l_xab = (char *) &xabfhc;
|
|
573 status = sys$open (&fab, 0, 0);
|
|
574 if (status != RMS$_NORMAL) /* Probably non-existent file */
|
|
575 return Qnil;
|
|
576 sys$close (&fab, 0, 0);
|
|
577 if (xabfhc.xab$w_verlimit == 32767)
|
|
578 return Qnil; /* No version limit */
|
|
579 else
|
|
580 return make_int (xabfhc.xab$w_verlimit);
|
|
581 }
|
|
582
|
|
583 #endif /* VMS */
|
|
584
|
|
585
|
|
586 Lisp_Object
|
|
587 wasteful_word_to_lisp (unsigned int item)
|
|
588 {
|
|
589 /* Compatibility: in other versions, file-attributes returns a LIST
|
|
590 of two 16 bit integers... */
|
|
591 Lisp_Object cons = word_to_lisp (item);
|
|
592 XCDR (cons) = Fcons (XCDR (cons), Qnil);
|
|
593 return cons;
|
|
594 }
|
|
595
|
20
|
596 DEFUN ("file-attributes", Ffile_attributes, 1, 1, 0, /*
|
0
|
597 Return a list of attributes of file FILENAME.
|
|
598 Value is nil if specified file cannot be opened.
|
|
599 Otherwise, list elements are:
|
|
600 0. t for directory, string (name linked to) for symbolic link, or nil.
|
|
601 1. Number of links to file.
|
|
602 2. File uid.
|
|
603 3. File gid.
|
|
604 4. Last access time, as a list of two integers.
|
|
605 First integer has high-order 16 bits of time, second has low 16 bits.
|
|
606 5. Last modification time, likewise.
|
|
607 6. Last status change time, likewise.
|
|
608 7. Size in bytes. (-1, if number is out of range).
|
|
609 8. File modes, as a string of ten letters or dashes as in ls -l.
|
|
610 9. t iff file's gid would change if file were deleted and recreated.
|
|
611 10. inode number.
|
|
612 11. Device number.
|
|
613
|
|
614 If file does not exist, returns nil.
|
20
|
615 */
|
|
616 (filename))
|
0
|
617 {
|
|
618 /* This function can GC */
|
|
619 Lisp_Object values[12];
|
|
620 Lisp_Object dirname = Qnil;
|
|
621 struct stat s;
|
|
622 char modes[10];
|
|
623 Lisp_Object handler;
|
|
624 struct gcpro gcpro1, gcpro2;
|
|
625
|
|
626 GCPRO1 (filename);
|
|
627 filename = Fexpand_file_name (filename, Qnil);
|
|
628
|
|
629 /* If the file name has special constructs in it,
|
|
630 call the corresponding file handler. */
|
|
631 handler = Ffind_file_name_handler (filename, Qfile_attributes);
|
|
632 UNGCPRO;
|
|
633 if (!NILP (handler))
|
|
634 return call2 (handler, Qfile_attributes, filename);
|
|
635
|
14
|
636 if (lstat ((char *) XSTRING_DATA (filename), &s) < 0)
|
0
|
637 return Qnil;
|
|
638
|
|
639 GCPRO2 (filename, dirname);
|
|
640
|
|
641 #ifdef BSD4_2
|
|
642 dirname = Ffile_name_directory (filename);
|
|
643 #endif
|
|
644
|
|
645 #ifdef MSDOS
|
|
646 {
|
14
|
647 char *tmpnam = (char *) XSTRING_DATA (Ffile_name_nondirectory (filename));
|
0
|
648 int l = strlen (tmpnam);
|
|
649
|
|
650 if (l >= 5
|
|
651 && S_ISREG (s.st_mode)
|
14
|
652 && (stricmp (&tmpnam[l - 4], ".com") == 0 ||
|
|
653 stricmp (&tmpnam[l - 4], ".exe") == 0 ||
|
|
654 stricmp (&tmpnam[l - 4], ".bat") == 0))
|
0
|
655 {
|
|
656 s.st_mode |= S_IEXEC;
|
|
657 }
|
|
658 }
|
|
659 #endif /* MSDOS */
|
|
660
|
|
661 switch (s.st_mode & S_IFMT)
|
|
662 {
|
|
663 default:
|
|
664 values[0] = Qnil;
|
|
665 break;
|
|
666 case S_IFDIR:
|
|
667 values[0] = Qt;
|
|
668 break;
|
|
669 #ifdef S_IFLNK
|
|
670 case S_IFLNK:
|
|
671 values[0] = Ffile_symlink_p (filename);
|
|
672 break;
|
|
673 #endif
|
|
674 }
|
|
675 values[1] = make_int (s.st_nlink);
|
|
676 values[2] = make_int (s.st_uid);
|
|
677 values[3] = make_int (s.st_gid);
|
|
678 values[4] = wasteful_word_to_lisp (s.st_atime);
|
|
679 values[5] = wasteful_word_to_lisp (s.st_mtime);
|
|
680 values[6] = wasteful_word_to_lisp (s.st_ctime);
|
|
681 values[7] = make_int ((EMACS_INT) s.st_size);
|
|
682 /* If the size is out of range, give back -1. */
|
|
683 /* #### Fix when Emacs gets bignums! */
|
|
684 if (XINT (values[7]) != s.st_size)
|
|
685 XSETINT (values[7], -1);
|
|
686 filemodestring (&s, modes);
|
|
687 values[8] = make_string ((Bufbyte *) modes, 10);
|
|
688 #if defined (BSD4_2) || defined (BSD4_3) /* file gid will be dir gid */
|
|
689 {
|
|
690 struct stat sdir;
|
|
691
|
14
|
692 if (!NILP (dirname) && stat ((char *) XSTRING_DATA (dirname), &sdir) == 0)
|
0
|
693 values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
|
|
694 else /* if we can't tell, assume worst */
|
|
695 values[9] = Qt;
|
|
696 }
|
|
697 #else /* file gid will be egid */
|
|
698 #ifdef WINDOWSNT
|
|
699 values[9] = Qnil; /* sorry, no group IDs on NT */
|
|
700 #else /* not WINDOWSNT */
|
|
701 values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
|
|
702 #endif /* not WINDOWSNT */
|
|
703 #endif /* BSD4_2 or BSD4_3 */
|
|
704 #ifdef WINDOWSNT
|
|
705 /* Fill in the inode and device values specially...see nt.c. */
|
|
706 if (!get_inode_and_device_vals (filename, &values[10], &values[11]))
|
|
707 {
|
|
708 UNGCPRO;
|
|
709 return Qnil;
|
|
710 }
|
|
711 #else /* not WINDOWSNT */
|
|
712 values[10] = make_int (s.st_ino);
|
|
713 values[11] = make_int (s.st_dev);
|
|
714 #endif /* not WINDOWSNT */
|
|
715 UNGCPRO;
|
|
716 return Flist (countof (values), values);
|
|
717 }
|
|
718
|
|
719
|
|
720 /************************************************************************/
|
|
721 /* initialization */
|
|
722 /************************************************************************/
|
|
723
|
|
724 void
|
|
725 syms_of_dired (void)
|
|
726 {
|
|
727 defsymbol (&Qdirectory_files, "directory-files");
|
|
728 defsymbol (&Qfile_name_completion, "file-name-completion");
|
|
729 defsymbol (&Qfile_name_all_completions, "file-name-all-completions");
|
|
730 defsymbol (&Qfile_attributes, "file-attributes");
|
|
731
|
20
|
732 DEFSUBR (Fdirectory_files);
|
|
733 DEFSUBR (Ffile_name_completion);
|
0
|
734 #ifdef VMS
|
20
|
735 DEFSUBR (Ffile_name_all_versions);
|
|
736 DEFSUBR (Ffile_version_limit);
|
0
|
737 #endif /* VMS */
|
20
|
738 DEFSUBR (Ffile_name_all_completions);
|
|
739 DEFSUBR (Ffile_attributes);
|
0
|
740 }
|
|
741
|
|
742 void
|
|
743 vars_of_dired (void)
|
|
744 {
|
|
745 DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions /*
|
|
746 *Completion ignores filenames ending in any string in this list.
|
|
747 This variable does not affect lists of possible completions,
|
|
748 but does affect the commands that actually do completions.
|
|
749 It is used by the functions `file-name-completion' and
|
|
750 `file-name-all-completions'.
|
|
751 */ );
|
|
752 Vcompletion_ignored_extensions = Qnil;
|
|
753 }
|