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