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