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