428
|
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
|
442
|
26 #include "sysfile.h"
|
|
27 #include "sysdir.h"
|
|
28 #include "systime.h"
|
|
29 #include "sysdep.h"
|
|
30 #include "syspwd.h"
|
428
|
31 #include "buffer.h"
|
|
32 #include "commands.h"
|
|
33 #include "elhash.h"
|
|
34 #include "regex.h"
|
|
35 #include "opaque.h"
|
460
|
36 #include "syntax.h"
|
428
|
37
|
528
|
38 #ifdef WIN32_NATIVE
|
|
39 #include "syswindows.h"
|
|
40 #include <lmaccess.h>
|
|
41 #include <lmapibuf.h>
|
|
42 #include <lmerr.h>
|
|
43 #endif
|
|
44
|
428
|
45 Lisp_Object Vcompletion_ignored_extensions;
|
|
46 Lisp_Object Qdirectory_files;
|
|
47 Lisp_Object Qfile_name_completion;
|
|
48 Lisp_Object Qfile_name_all_completions;
|
|
49 Lisp_Object Qfile_attributes;
|
|
50
|
|
51 static Lisp_Object
|
|
52 close_directory_unwind (Lisp_Object unwind_obj)
|
|
53 {
|
|
54 DIR *d = (DIR *)get_opaque_ptr (unwind_obj);
|
|
55 closedir (d);
|
|
56 free_opaque_ptr (unwind_obj);
|
|
57 return Qnil;
|
|
58 }
|
|
59
|
|
60 DEFUN ("directory-files", Fdirectory_files, 1, 5, 0, /*
|
|
61 Return a list of names of files in DIRECTORY.
|
|
62 There are four optional arguments:
|
|
63 If FULL is non-nil, absolute pathnames of the files are returned.
|
|
64 If MATCH is non-nil, only pathnames containing that regexp are returned.
|
|
65 If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
|
|
66 NOSORT is useful if you plan to sort the result yourself.
|
|
67 If FILES-ONLY is the symbol t, then only the "files" in the directory
|
|
68 will be returned; subdirectories will be excluded. If FILES-ONLY is not
|
|
69 nil and not t, then only the subdirectories will be returned. Otherwise,
|
|
70 if FILES-ONLY is nil (the default) then both files and subdirectories will
|
|
71 be returned.
|
|
72 */
|
|
73 (directory, full, match, nosort, files_only))
|
|
74 {
|
|
75 /* This function can GC */
|
|
76 DIR *d;
|
|
77 Lisp_Object list = Qnil;
|
|
78 Bytecount directorylen;
|
|
79 Lisp_Object handler;
|
|
80 struct re_pattern_buffer *bufp = NULL;
|
|
81 int speccount = specpdl_depth ();
|
|
82 char *statbuf, *statbuf_tail;
|
|
83
|
|
84 struct gcpro gcpro1, gcpro2;
|
|
85 GCPRO2 (directory, list);
|
|
86
|
|
87 /* If the file name has special constructs in it,
|
|
88 call the corresponding file handler. */
|
|
89 handler = Ffind_file_name_handler (directory, Qdirectory_files);
|
|
90 if (!NILP (handler))
|
|
91 {
|
|
92 UNGCPRO;
|
|
93 if (!NILP (files_only))
|
|
94 return call6 (handler, Qdirectory_files, directory, full, match,
|
|
95 nosort, files_only);
|
|
96 else
|
|
97 return call5 (handler, Qdirectory_files, directory, full, match,
|
|
98 nosort);
|
|
99 }
|
|
100
|
|
101 /* #### why do we do Fexpand_file_name after file handlers here,
|
|
102 but earlier everywhere else? */
|
|
103 directory = Fexpand_file_name (directory, Qnil);
|
|
104 directory = Ffile_name_as_directory (directory);
|
|
105 directorylen = XSTRING_LENGTH (directory);
|
|
106
|
|
107 statbuf = (char *)alloca (directorylen + MAXNAMLEN + 1);
|
|
108 memcpy (statbuf, XSTRING_DATA (directory), directorylen);
|
|
109 statbuf_tail = statbuf + directorylen;
|
|
110
|
|
111 /* XEmacs: this should come after Ffile_name_as_directory() to avoid
|
|
112 potential regexp cache smashage. It comes before the opendir()
|
|
113 because it might signal an error. */
|
|
114 if (!NILP (match))
|
|
115 {
|
|
116 CHECK_STRING (match);
|
|
117
|
|
118 /* MATCH might be a flawed regular expression. Rather than
|
|
119 catching and signalling our own errors, we just call
|
|
120 compile_pattern to do the work for us. */
|
446
|
121 bufp = compile_pattern (match, 0, Qnil, 0, ERROR_ME);
|
428
|
122 }
|
|
123
|
|
124 /* Now *bufp is the compiled form of MATCH; don't call anything
|
|
125 which might compile a new regexp until we're done with the loop! */
|
|
126
|
|
127 /* Do this opendir after anything which might signal an error.
|
|
128 NOTE: the above comment is old; previously, there was no
|
|
129 unwind-protection in case of error, but now there is. */
|
|
130 d = opendir ((char *) XSTRING_DATA (directory));
|
|
131 if (!d)
|
|
132 report_file_error ("Opening directory", list1 (directory));
|
|
133
|
460
|
134 regex_match_object = Qt;
|
|
135 regex_emacs_buffer = current_buffer;
|
|
136
|
428
|
137 record_unwind_protect (close_directory_unwind, make_opaque_ptr ((void *)d));
|
|
138
|
|
139 /* Loop reading blocks */
|
|
140 while (1)
|
|
141 {
|
|
142 DIRENTRY *dp = readdir (d);
|
|
143 int len;
|
|
144
|
|
145 if (!dp)
|
|
146 break;
|
|
147 len = NAMLEN (dp);
|
|
148 if (DIRENTRY_NONEMPTY (dp)
|
|
149 && (NILP (match)
|
|
150 || (0 <= re_search (bufp, dp->d_name, len, 0, len, 0))))
|
|
151 {
|
|
152 if (!NILP (files_only))
|
|
153 {
|
|
154 struct stat st;
|
|
155 int dir_p = 0;
|
|
156
|
|
157 memcpy (statbuf_tail, dp->d_name, len);
|
|
158 statbuf_tail[len] = 0;
|
|
159
|
442
|
160 if (xemacs_stat (statbuf, &st) == 0
|
428
|
161 && (st.st_mode & S_IFMT) == S_IFDIR)
|
|
162 dir_p = 1;
|
|
163
|
|
164 if (EQ (files_only, Qt) && dir_p)
|
|
165 continue;
|
|
166 else if (!EQ (files_only, Qt) && !dir_p)
|
|
167 continue;
|
|
168 }
|
|
169
|
|
170 {
|
|
171 Lisp_Object name =
|
|
172 make_string ((Bufbyte *)dp->d_name, len);
|
|
173 if (!NILP (full))
|
|
174 name = concat2 (directory, name);
|
|
175
|
|
176 list = Fcons (name, list);
|
|
177 }
|
|
178 }
|
|
179 }
|
|
180 unbind_to (speccount, Qnil); /* This will close the dir */
|
|
181
|
|
182 if (NILP (nosort))
|
|
183 list = Fsort (Fnreverse (list), Qstring_lessp);
|
|
184
|
|
185 RETURN_UNGCPRO (list);
|
|
186 }
|
|
187
|
|
188 static Lisp_Object file_name_completion (Lisp_Object file,
|
|
189 Lisp_Object directory,
|
|
190 int all_flag, int ver_flag);
|
|
191
|
|
192 DEFUN ("file-name-completion", Ffile_name_completion, 2, 2, 0, /*
|
444
|
193 Complete file name PARTIAL-FILENAME in directory DIRECTORY.
|
|
194 Return the longest prefix common to all file names in DIRECTORY
|
|
195 that start with PARTIAL-FILENAME.
|
|
196 If there is only one and PARTIAL-FILENAME matches it exactly, return t.
|
|
197 Return nil if DIRECTORY contains no name starting with PARTIAL-FILENAME.
|
428
|
198
|
444
|
199 File names which end with any member of `completion-ignored-extensions'
|
|
200 are not considered as possible completions for PARTIAL-FILENAME unless
|
|
201 there is no other possible completion. `completion-ignored-extensions'
|
|
202 is not applied to the names of directories.
|
428
|
203 */
|
444
|
204 (partial_filename, directory))
|
428
|
205 {
|
|
206 /* This function can GC. GC checked 1996.04.06. */
|
|
207 Lisp_Object handler;
|
|
208
|
|
209 /* If the directory name has special constructs in it,
|
|
210 call the corresponding file handler. */
|
|
211 handler = Ffind_file_name_handler (directory, Qfile_name_completion);
|
|
212 if (!NILP (handler))
|
444
|
213 return call3 (handler, Qfile_name_completion, partial_filename, directory);
|
428
|
214
|
|
215 /* If the file name has special constructs in it,
|
|
216 call the corresponding file handler. */
|
444
|
217 handler = Ffind_file_name_handler (partial_filename, Qfile_name_completion);
|
428
|
218 if (!NILP (handler))
|
444
|
219 return call3 (handler, Qfile_name_completion, partial_filename, directory);
|
428
|
220
|
444
|
221 return file_name_completion (partial_filename, directory, 0, 0);
|
428
|
222 }
|
|
223
|
|
224 DEFUN ("file-name-all-completions", Ffile_name_all_completions, 2, 2, 0, /*
|
444
|
225 Return a list of all completions of PARTIAL-FILENAME in DIRECTORY.
|
|
226 These are all file names in DIRECTORY which begin with PARTIAL-FILENAME.
|
428
|
227
|
|
228 File names which end with any member of `completion-ignored-extensions'
|
444
|
229 are not considered as possible completions for PARTIAL-FILENAME unless
|
|
230 there is no other possible completion. `completion-ignored-extensions'
|
|
231 is not applied to the names of directories.
|
428
|
232 */
|
444
|
233 (partial_filename, directory))
|
428
|
234 {
|
|
235 /* This function can GC. GC checked 1997.06.04. */
|
|
236 Lisp_Object handler;
|
|
237 struct gcpro gcpro1;
|
|
238
|
|
239 GCPRO1 (directory);
|
|
240 directory = Fexpand_file_name (directory, Qnil);
|
|
241 /* If the file name has special constructs in it,
|
|
242 call the corresponding file handler. */
|
|
243 handler = Ffind_file_name_handler (directory, Qfile_name_all_completions);
|
|
244 UNGCPRO;
|
|
245 if (!NILP (handler))
|
444
|
246 return call3 (handler, Qfile_name_all_completions, partial_filename,
|
428
|
247 directory);
|
|
248
|
444
|
249 return file_name_completion (partial_filename, directory, 1, 0);
|
428
|
250 }
|
|
251
|
|
252 static int
|
|
253 file_name_completion_stat (Lisp_Object directory, DIRENTRY *dp,
|
|
254 struct stat *st_addr)
|
|
255 {
|
|
256 Bytecount len = NAMLEN (dp);
|
|
257 Bytecount pos = XSTRING_LENGTH (directory);
|
|
258 int value;
|
|
259 char *fullname = (char *) alloca (len + pos + 2);
|
|
260
|
|
261 memcpy (fullname, XSTRING_DATA (directory), pos);
|
|
262 if (!IS_DIRECTORY_SEP (fullname[pos - 1]))
|
|
263 fullname[pos++] = DIRECTORY_SEP;
|
|
264
|
|
265 memcpy (fullname + pos, dp->d_name, len);
|
|
266 fullname[pos + len] = 0;
|
|
267
|
|
268 #ifdef S_IFLNK
|
|
269 /* We want to return success if a link points to a nonexistent file,
|
|
270 but we want to return the status for what the link points to,
|
|
271 in case it is a directory. */
|
|
272 value = lstat (fullname, st_addr);
|
|
273 if (S_ISLNK (st_addr->st_mode))
|
442
|
274 xemacs_stat (fullname, st_addr);
|
428
|
275 #else
|
442
|
276 value = xemacs_stat (fullname, st_addr);
|
428
|
277 #endif
|
|
278 return value;
|
|
279 }
|
|
280
|
|
281 static Lisp_Object
|
|
282 file_name_completion_unwind (Lisp_Object locative)
|
|
283 {
|
|
284 DIR *d;
|
|
285 Lisp_Object obj = XCAR (locative);
|
|
286
|
|
287 if (!NILP (obj))
|
|
288 {
|
|
289 d = (DIR *)get_opaque_ptr (obj);
|
|
290 closedir (d);
|
|
291 free_opaque_ptr (obj);
|
|
292 }
|
|
293 free_cons (XCONS (locative));
|
|
294 return Qnil;
|
|
295 }
|
|
296
|
|
297 static Lisp_Object
|
|
298 file_name_completion (Lisp_Object file, Lisp_Object directory, int all_flag,
|
|
299 int ver_flag)
|
|
300 {
|
|
301 /* This function can GC */
|
|
302 DIR *d = 0;
|
|
303 int matchcount = 0;
|
|
304 Lisp_Object bestmatch = Qnil;
|
|
305 Charcount bestmatchsize = 0;
|
|
306 struct stat st;
|
|
307 int passcount;
|
|
308 int speccount = specpdl_depth ();
|
|
309 Charcount file_name_length;
|
|
310 Lisp_Object locative;
|
|
311 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
312
|
|
313 GCPRO3 (file, directory, bestmatch);
|
|
314
|
|
315 CHECK_STRING (file);
|
|
316
|
442
|
317 #ifdef WIN32_NATIVE
|
428
|
318 /* Filename completion on Windows ignores case, since Windows
|
|
319 filesystems do. */
|
|
320 specbind (Qcompletion_ignore_case, Qt);
|
442
|
321 #endif /* WIN32_NATIVE */
|
428
|
322
|
|
323 #ifdef FILE_SYSTEM_CASE
|
|
324 file = FILE_SYSTEM_CASE (file);
|
|
325 #endif
|
|
326 directory = Fexpand_file_name (directory, Qnil);
|
|
327 file_name_length = XSTRING_CHAR_LENGTH (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 /* We cannot use close_directory_unwind() because we change the
|
|
338 directory. The old code used to just avoid signaling errors, and
|
|
339 call closedir, but it was wrong, because it made sane handling of
|
|
340 QUIT impossible and, besides, various utility functions like
|
|
341 regexp_ignore_completion_p can signal errors. */
|
|
342 locative = noseeum_cons (Qnil, Qnil);
|
|
343 record_unwind_protect (file_name_completion_unwind, locative);
|
|
344
|
|
345 for (passcount = !!all_flag; NILP (bestmatch) && passcount < 2; passcount++)
|
|
346 {
|
|
347 d = opendir ((char *) XSTRING_DATA (Fdirectory_file_name (directory)));
|
|
348 if (!d)
|
|
349 report_file_error ("Opening directory", list1 (directory));
|
|
350 XCAR (locative) = make_opaque_ptr ((void *)d);
|
|
351
|
|
352 /* Loop reading blocks */
|
|
353 while (1)
|
|
354 {
|
|
355 DIRENTRY *dp;
|
|
356 Bytecount len;
|
|
357 /* scmp() works in characters, not bytes, so we have to compute
|
|
358 this value: */
|
|
359 Charcount cclen;
|
|
360 int directoryp;
|
|
361 int ignored_extension_p = 0;
|
|
362 Bufbyte *d_name;
|
|
363
|
|
364 dp = readdir (d);
|
|
365 if (!dp) break;
|
|
366
|
|
367 /* Cast to Bufbyte* is OK, as readdir() Mule-encapsulates. */
|
|
368 d_name = (Bufbyte *) dp->d_name;
|
|
369 len = NAMLEN (dp);
|
|
370 cclen = bytecount_to_charcount (d_name, len);
|
|
371
|
|
372 QUIT;
|
|
373
|
|
374 if (! DIRENTRY_NONEMPTY (dp)
|
|
375 || cclen < file_name_length
|
|
376 || 0 <= scmp (d_name, XSTRING_DATA (file), file_name_length))
|
|
377 continue;
|
|
378
|
|
379 if (file_name_completion_stat (directory, dp, &st) < 0)
|
|
380 continue;
|
|
381
|
|
382 directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
|
|
383 if (directoryp)
|
|
384 {
|
|
385 #ifndef TRIVIAL_DIRECTORY_ENTRY
|
|
386 #define TRIVIAL_DIRECTORY_ENTRY(n) (!strcmp (n, ".") || !strcmp (n, ".."))
|
|
387 #endif
|
|
388 /* "." and ".." are never interesting as completions, but are
|
|
389 actually in the way in a directory containing only one file. */
|
|
390 if (!passcount && TRIVIAL_DIRECTORY_ENTRY (dp->d_name))
|
|
391 continue;
|
|
392 }
|
|
393 else
|
|
394 {
|
|
395 /* Compare extensions-to-be-ignored against end of this file name */
|
|
396 /* if name is not an exact match against specified string. */
|
|
397 if (!passcount && cclen > file_name_length)
|
|
398 {
|
|
399 Lisp_Object tem;
|
|
400 /* and exit this for loop if a match is found */
|
|
401 EXTERNAL_LIST_LOOP (tem, Vcompletion_ignored_extensions)
|
|
402 {
|
|
403 Lisp_Object elt = XCAR (tem);
|
|
404 Charcount skip;
|
|
405
|
|
406 CHECK_STRING (elt);
|
|
407
|
|
408 skip = cclen - XSTRING_CHAR_LENGTH (elt);
|
|
409 if (skip < 0) continue;
|
|
410
|
|
411 if (0 > scmp (charptr_n_addr (d_name, skip),
|
|
412 XSTRING_DATA (elt),
|
|
413 XSTRING_CHAR_LENGTH (elt)))
|
|
414 {
|
|
415 ignored_extension_p = 1;
|
|
416 break;
|
|
417 }
|
|
418 }
|
|
419 }
|
|
420 }
|
|
421
|
|
422 /* If an ignored-extensions match was found,
|
|
423 don't process this name as a completion. */
|
|
424 if (!passcount && ignored_extension_p)
|
|
425 continue;
|
|
426
|
|
427 if (!passcount && regexp_ignore_completion_p (d_name, Qnil, 0, cclen))
|
|
428 continue;
|
|
429
|
|
430 /* Update computation of how much all possible completions match */
|
|
431 matchcount++;
|
|
432
|
|
433 if (all_flag || NILP (bestmatch))
|
|
434 {
|
|
435 Lisp_Object name = Qnil;
|
|
436 struct gcpro ngcpro1;
|
|
437 NGCPRO1 (name);
|
|
438 /* This is a possible completion */
|
|
439 name = make_string (d_name, len);
|
|
440 if (directoryp) /* Completion is a directory; end it with '/' */
|
|
441 name = Ffile_name_as_directory (name);
|
|
442 if (all_flag)
|
|
443 {
|
|
444 bestmatch = Fcons (name, bestmatch);
|
|
445 }
|
|
446 else
|
|
447 {
|
|
448 bestmatch = name;
|
|
449 bestmatchsize = XSTRING_CHAR_LENGTH (name);
|
|
450 }
|
|
451 NUNGCPRO;
|
|
452 }
|
|
453 else
|
|
454 {
|
|
455 Charcount compare = min (bestmatchsize, cclen);
|
|
456 Bufbyte *p1 = XSTRING_DATA (bestmatch);
|
|
457 Bufbyte *p2 = d_name;
|
|
458 Charcount matchsize = scmp (p1, p2, compare);
|
|
459
|
|
460 if (matchsize < 0)
|
|
461 matchsize = compare;
|
|
462 if (completion_ignore_case)
|
|
463 {
|
|
464 /* If this is an exact match except for case,
|
|
465 use it as the best match rather than one that is not
|
|
466 an exact match. This way, we get the case pattern
|
|
467 of the actual match. */
|
|
468 if ((matchsize == cclen
|
|
469 && matchsize + !!directoryp
|
|
470 < XSTRING_CHAR_LENGTH (bestmatch))
|
|
471 ||
|
|
472 /* If there is no exact match ignoring case,
|
|
473 prefer a match that does not change the case
|
|
474 of the input. */
|
|
475 (((matchsize == cclen)
|
|
476 ==
|
|
477 (matchsize + !!directoryp
|
|
478 == XSTRING_CHAR_LENGTH (bestmatch)))
|
|
479 /* If there is more than one exact match aside from
|
|
480 case, and one of them is exact including case,
|
|
481 prefer that one. */
|
|
482 && 0 > scmp_1 (p2, XSTRING_DATA (file),
|
|
483 file_name_length, 0)
|
|
484 && 0 <= scmp_1 (p1, XSTRING_DATA (file),
|
|
485 file_name_length, 0)))
|
|
486 {
|
|
487 bestmatch = make_string (d_name, len);
|
|
488 if (directoryp)
|
|
489 bestmatch = Ffile_name_as_directory (bestmatch);
|
|
490 }
|
|
491 }
|
|
492
|
|
493 /* If this directory all matches,
|
|
494 see if implicit following slash does too. */
|
|
495 if (directoryp
|
|
496 && compare == matchsize
|
|
497 && bestmatchsize > matchsize
|
|
498 && IS_ANY_SEP (charptr_emchar_n (p1, matchsize)))
|
|
499 matchsize++;
|
|
500 bestmatchsize = matchsize;
|
|
501 }
|
|
502 }
|
|
503 closedir (d);
|
|
504 free_opaque_ptr (XCAR (locative));
|
|
505 XCAR (locative) = Qnil;
|
|
506 }
|
|
507
|
|
508 unbind_to (speccount, Qnil);
|
|
509
|
|
510 UNGCPRO;
|
|
511
|
|
512 if (all_flag || NILP (bestmatch))
|
|
513 return bestmatch;
|
|
514 if (matchcount == 1 && bestmatchsize == file_name_length)
|
|
515 return Qt;
|
|
516 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize));
|
|
517 }
|
|
518
|
|
519
|
|
520 static Lisp_Object user_name_completion (Lisp_Object user,
|
|
521 int all_flag,
|
|
522 int *uniq);
|
|
523
|
|
524 DEFUN ("user-name-completion", Fuser_name_completion, 1, 1, 0, /*
|
444
|
525 Complete user name from PARTIAL-USERNAME.
|
|
526 Return the longest prefix common to all user names starting with
|
|
527 PARTIAL-USERNAME. If there is only one and PARTIAL-USERNAME matches
|
|
528 it exactly, returns t. Return nil if there is no user name starting
|
|
529 with PARTIAL-USERNAME.
|
428
|
530 */
|
444
|
531 (partial_username))
|
428
|
532 {
|
444
|
533 return user_name_completion (partial_username, 0, NULL);
|
428
|
534 }
|
|
535
|
|
536 DEFUN ("user-name-completion-1", Fuser_name_completion_1, 1, 1, 0, /*
|
444
|
537 Complete user name from PARTIAL-USERNAME.
|
428
|
538
|
|
539 This function is identical to `user-name-completion', except that
|
|
540 the cons of the completion and an indication of whether the
|
|
541 completion was unique is returned.
|
|
542
|
444
|
543 The car of the returned value is the longest prefix common to all user
|
|
544 names that start with PARTIAL-USERNAME. If there is only one and
|
|
545 PARTIAL-USERNAME matches it exactly, the car is t. The car is nil if
|
|
546 there is no user name starting with PARTIAL-USERNAME. The cdr of the
|
|
547 result is non-nil if and only if the completion returned in the car
|
|
548 was unique.
|
428
|
549 */
|
444
|
550 (partial_username))
|
428
|
551 {
|
|
552 int uniq;
|
444
|
553 Lisp_Object completed = user_name_completion (partial_username, 0, &uniq);
|
428
|
554 return Fcons (completed, uniq ? Qt : Qnil);
|
|
555 }
|
|
556
|
|
557 DEFUN ("user-name-all-completions", Fuser_name_all_completions, 1, 1, 0, /*
|
444
|
558 Return a list of all user name completions from PARTIAL-USERNAME.
|
|
559 These are all the user names which begin with PARTIAL-USERNAME.
|
428
|
560 */
|
444
|
561 (partial_username))
|
428
|
562 {
|
444
|
563 return user_name_completion (partial_username, 1, NULL);
|
428
|
564 }
|
|
565
|
440
|
566 struct user_name
|
|
567 {
|
|
568 Bufbyte *ptr;
|
|
569 size_t len;
|
|
570 };
|
|
571
|
|
572 struct user_cache
|
|
573 {
|
|
574 struct user_name *user_names;
|
428
|
575 int length;
|
|
576 int size;
|
|
577 EMACS_TIME last_rebuild_time;
|
|
578 };
|
|
579 static struct user_cache user_cache;
|
|
580
|
|
581 static void
|
|
582 free_user_cache (struct user_cache *cache)
|
|
583 {
|
|
584 int i;
|
|
585 for (i = 0; i < cache->length; i++)
|
440
|
586 xfree (cache->user_names[i].ptr);
|
|
587 xfree (cache->user_names);
|
|
588 xzero (*cache);
|
428
|
589 }
|
|
590
|
|
591 static Lisp_Object
|
440
|
592 user_name_completion_unwind (Lisp_Object cache_incomplete_p)
|
428
|
593 {
|
528
|
594 #ifndef WIN32_NATIVE
|
440
|
595 endpwent ();
|
|
596 speed_up_interrupts ();
|
528
|
597 #endif
|
428
|
598
|
440
|
599 if (! NILP (XCAR (cache_incomplete_p)))
|
|
600 free_user_cache (&user_cache);
|
|
601
|
|
602 free_cons (XCONS (cache_incomplete_p));
|
428
|
603
|
|
604 return Qnil;
|
|
605 }
|
|
606
|
440
|
607 #define USER_CACHE_TTL (24*60*60) /* Time to live: 1 day, in seconds */
|
428
|
608
|
|
609 static Lisp_Object
|
|
610 user_name_completion (Lisp_Object user, int all_flag, int *uniq)
|
|
611 {
|
|
612 /* This function can GC */
|
|
613 int matchcount = 0;
|
|
614 Lisp_Object bestmatch = Qnil;
|
|
615 Charcount bestmatchsize = 0;
|
|
616 Charcount user_name_length;
|
|
617 EMACS_TIME t;
|
|
618 int i;
|
|
619 struct gcpro gcpro1, gcpro2;
|
|
620
|
|
621 GCPRO2 (user, bestmatch);
|
|
622
|
|
623 CHECK_STRING (user);
|
|
624
|
|
625 user_name_length = XSTRING_CHAR_LENGTH (user);
|
|
626
|
|
627 /* Cache user name lookups because it tends to be quite slow.
|
|
628 * Rebuild the cache occasionally to catch changes */
|
|
629 EMACS_GET_TIME (t);
|
440
|
630 if (user_cache.user_names &&
|
428
|
631 (EMACS_SECS (t) - EMACS_SECS (user_cache.last_rebuild_time)
|
440
|
632 > USER_CACHE_TTL))
|
|
633 free_user_cache (&user_cache);
|
428
|
634
|
440
|
635 if (!user_cache.user_names)
|
428
|
636 {
|
528
|
637 #ifndef WIN32_NATIVE
|
428
|
638 struct passwd *pwd;
|
528
|
639 #else
|
|
640 DWORD entriesread;
|
|
641 DWORD totalentries;
|
|
642 DWORD resume_handle = 0;
|
|
643 #endif
|
|
644
|
440
|
645 Lisp_Object cache_incomplete_p = noseeum_cons (Qt, Qnil);
|
|
646 int speccount = specpdl_depth ();
|
|
647
|
528
|
648 record_unwind_protect (user_name_completion_unwind, cache_incomplete_p);
|
|
649 #ifndef WIN32_NATIVE
|
428
|
650 slow_down_interrupts ();
|
|
651 setpwent ();
|
|
652 while ((pwd = getpwent ()))
|
|
653 {
|
|
654 QUIT;
|
440
|
655 DO_REALLOC (user_cache.user_names, user_cache.size,
|
|
656 user_cache.length + 1, struct user_name);
|
|
657 TO_INTERNAL_FORMAT (C_STRING, pwd->pw_name,
|
|
658 MALLOC,
|
|
659 (user_cache.user_names[user_cache.length].ptr,
|
|
660 user_cache.user_names[user_cache.length].len),
|
|
661 Qnative);
|
|
662 user_cache.length++;
|
428
|
663 }
|
528
|
664 #else
|
|
665 do
|
|
666 {
|
|
667 USER_INFO_0 *bufptr;
|
|
668 NET_API_STATUS status_status_statui_statum_statu;
|
|
669 int i;
|
|
670
|
|
671 QUIT;
|
|
672 status_status_statui_statum_statu =
|
|
673 NetUserEnum (NULL, 0, 0, (LPBYTE *) &bufptr, 1024, &entriesread,
|
|
674 &totalentries, &resume_handle);
|
|
675 if (status_status_statui_statum_statu != NERR_Success &&
|
|
676 status_status_statui_statum_statu != ERROR_MORE_DATA)
|
|
677 invalid_operation ("Error enumerating users",
|
|
678 make_int (GetLastError ()));
|
|
679 for (i = 0; i < entriesread; i++)
|
|
680 {
|
|
681 int nout =
|
|
682 WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK,
|
|
683 bufptr[i].usri0_name,
|
|
684 -1, 0, 0, "~", 0);
|
|
685 void *outp = alloca (nout);
|
|
686 WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK,
|
|
687 bufptr[i].usri0_name, -1,
|
|
688 (LPSTR) outp, nout, "~", 0);
|
|
689 DO_REALLOC (user_cache.user_names, user_cache.size,
|
|
690 user_cache.length + 1, struct user_name);
|
|
691 TO_INTERNAL_FORMAT (C_STRING, outp,
|
|
692 MALLOC,
|
|
693 (user_cache.
|
|
694 user_names[user_cache.length].ptr,
|
|
695 user_cache.
|
|
696 user_names[user_cache.length].len),
|
|
697 Qmswindows_tstr);
|
|
698 user_cache.length++;
|
|
699 }
|
|
700 NetApiBufferFree (bufptr);
|
|
701 }
|
|
702 while (entriesread != totalentries);
|
|
703 #endif
|
|
704
|
440
|
705 XCAR (cache_incomplete_p) = Qnil;
|
|
706 unbind_to (speccount, Qnil);
|
|
707
|
428
|
708 EMACS_GET_TIME (user_cache.last_rebuild_time);
|
|
709 }
|
|
710
|
|
711 for (i = 0; i < user_cache.length; i++)
|
|
712 {
|
440
|
713 Bufbyte *u_name = user_cache.user_names[i].ptr;
|
|
714 Bytecount len = user_cache.user_names[i].len;
|
428
|
715 /* scmp() works in chars, not bytes, so we have to compute this: */
|
|
716 Charcount cclen = bytecount_to_charcount (u_name, len);
|
|
717
|
|
718 QUIT;
|
|
719
|
|
720 if (cclen < user_name_length
|
|
721 || 0 <= scmp_1 (u_name, XSTRING_DATA (user), user_name_length, 0))
|
|
722 continue;
|
|
723
|
|
724 matchcount++; /* count matching completions */
|
|
725
|
|
726 if (all_flag || NILP (bestmatch))
|
|
727 {
|
|
728 Lisp_Object name = Qnil;
|
|
729 struct gcpro ngcpro1;
|
|
730 NGCPRO1 (name);
|
|
731 /* This is a possible completion */
|
|
732 name = make_string (u_name, len);
|
|
733 if (all_flag)
|
|
734 {
|
|
735 bestmatch = Fcons (name, bestmatch);
|
|
736 }
|
|
737 else
|
|
738 {
|
|
739 bestmatch = name;
|
|
740 bestmatchsize = XSTRING_CHAR_LENGTH (name);
|
|
741 }
|
|
742 NUNGCPRO;
|
|
743 }
|
|
744 else
|
|
745 {
|
|
746 Charcount compare = min (bestmatchsize, cclen);
|
|
747 Bufbyte *p1 = XSTRING_DATA (bestmatch);
|
|
748 Bufbyte *p2 = u_name;
|
|
749 Charcount matchsize = scmp_1 (p1, p2, compare, 0);
|
|
750
|
|
751 if (matchsize < 0)
|
|
752 matchsize = compare;
|
|
753
|
|
754 bestmatchsize = matchsize;
|
|
755 }
|
|
756 }
|
|
757
|
|
758 UNGCPRO;
|
|
759
|
|
760 if (uniq)
|
|
761 *uniq = (matchcount == 1);
|
|
762
|
|
763 if (all_flag || NILP (bestmatch))
|
|
764 return bestmatch;
|
|
765 if (matchcount == 1 && bestmatchsize == user_name_length)
|
|
766 return Qt;
|
|
767 return Fsubstring (bestmatch, Qzero, make_int (bestmatchsize));
|
|
768 }
|
|
769
|
|
770
|
|
771 Lisp_Object
|
442
|
772 make_directory_hash_table (const char *path)
|
428
|
773 {
|
|
774 DIR *d;
|
|
775 if ((d = opendir (path)))
|
|
776 {
|
|
777 DIRENTRY *dp;
|
|
778 Lisp_Object hash =
|
|
779 make_lisp_hash_table (20, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
|
|
780
|
|
781 while ((dp = readdir (d)))
|
|
782 {
|
|
783 Bytecount len = NAMLEN (dp);
|
|
784 if (DIRENTRY_NONEMPTY (dp))
|
|
785 /* Cast to Bufbyte* is OK, as readdir() Mule-encapsulates. */
|
|
786 Fputhash (make_string ((Bufbyte *) dp->d_name, len), Qt, hash);
|
|
787 }
|
|
788 closedir (d);
|
|
789 return hash;
|
|
790 }
|
|
791 else
|
|
792 return Qnil;
|
|
793 }
|
|
794
|
|
795 Lisp_Object
|
|
796 wasteful_word_to_lisp (unsigned int item)
|
|
797 {
|
|
798 /* Compatibility: in other versions, file-attributes returns a LIST
|
|
799 of two 16 bit integers... */
|
|
800 Lisp_Object cons = word_to_lisp (item);
|
|
801 XCDR (cons) = Fcons (XCDR (cons), Qnil);
|
|
802 return cons;
|
|
803 }
|
|
804
|
|
805 DEFUN ("file-attributes", Ffile_attributes, 1, 1, 0, /*
|
|
806 Return a list of attributes of file FILENAME.
|
|
807 Value is nil if specified file cannot be opened.
|
|
808 Otherwise, list elements are:
|
|
809 0. t for directory, string (name linked to) for symbolic link, or nil.
|
|
810 1. Number of links to file.
|
|
811 2. File uid.
|
|
812 3. File gid.
|
|
813 4. Last access time, as a list of two integers.
|
|
814 First integer has high-order 16 bits of time, second has low 16 bits.
|
|
815 5. Last modification time, likewise.
|
|
816 6. Last status change time, likewise.
|
|
817 7. Size in bytes. (-1, if number is out of range).
|
|
818 8. File modes, as a string of ten letters or dashes as in ls -l.
|
|
819 9. t iff file's gid would change if file were deleted and recreated.
|
|
820 10. inode number.
|
|
821 11. Device number.
|
|
822
|
|
823 If file does not exist, returns nil.
|
|
824 */
|
|
825 (filename))
|
|
826 {
|
|
827 /* This function can GC. GC checked 1997.06.04. */
|
|
828 Lisp_Object values[12];
|
|
829 Lisp_Object directory = Qnil;
|
|
830 struct stat s;
|
|
831 char modes[10];
|
|
832 Lisp_Object handler;
|
|
833 struct gcpro gcpro1, gcpro2;
|
|
834
|
|
835 GCPRO2 (filename, directory);
|
|
836 filename = Fexpand_file_name (filename, Qnil);
|
|
837
|
|
838 /* If the file name has special constructs in it,
|
|
839 call the corresponding file handler. */
|
|
840 handler = Ffind_file_name_handler (filename, Qfile_attributes);
|
|
841 if (!NILP (handler))
|
|
842 {
|
|
843 UNGCPRO;
|
|
844 return call2 (handler, Qfile_attributes, filename);
|
|
845 }
|
|
846
|
|
847 if (lstat ((char *) XSTRING_DATA (filename), &s) < 0)
|
|
848 {
|
|
849 UNGCPRO;
|
|
850 return Qnil;
|
|
851 }
|
|
852
|
|
853 #ifdef BSD4_2
|
|
854 directory = Ffile_name_directory (filename);
|
|
855 #endif
|
|
856
|
442
|
857 #if 0 /* #### shouldn't this apply to WIN32_NATIVE and maybe CYGWIN? */
|
428
|
858 {
|
|
859 char *tmpnam = (char *) XSTRING_DATA (Ffile_name_nondirectory (filename));
|
|
860 int l = strlen (tmpnam);
|
|
861
|
|
862 if (l >= 5
|
|
863 && S_ISREG (s.st_mode)
|
|
864 && (stricmp (&tmpnam[l - 4], ".com") == 0 ||
|
|
865 stricmp (&tmpnam[l - 4], ".exe") == 0 ||
|
|
866 stricmp (&tmpnam[l - 4], ".bat") == 0))
|
|
867 {
|
|
868 s.st_mode |= S_IEXEC;
|
|
869 }
|
|
870 }
|
442
|
871 #endif
|
428
|
872
|
|
873 switch (s.st_mode & S_IFMT)
|
|
874 {
|
|
875 default:
|
|
876 values[0] = Qnil;
|
|
877 break;
|
|
878 case S_IFDIR:
|
|
879 values[0] = Qt;
|
|
880 break;
|
|
881 #ifdef S_IFLNK
|
|
882 case S_IFLNK:
|
|
883 values[0] = Ffile_symlink_p (filename);
|
|
884 break;
|
|
885 #endif
|
|
886 }
|
|
887 values[1] = make_int (s.st_nlink);
|
|
888 values[2] = make_int (s.st_uid);
|
|
889 values[3] = make_int (s.st_gid);
|
|
890 values[4] = wasteful_word_to_lisp (s.st_atime);
|
|
891 values[5] = wasteful_word_to_lisp (s.st_mtime);
|
|
892 values[6] = wasteful_word_to_lisp (s.st_ctime);
|
|
893 values[7] = make_int ((EMACS_INT) s.st_size);
|
|
894 /* If the size is out of range, give back -1. */
|
|
895 /* #### Fix when Emacs gets bignums! */
|
|
896 if (XINT (values[7]) != s.st_size)
|
|
897 values[7] = make_int (-1);
|
|
898 filemodestring (&s, modes);
|
|
899 values[8] = make_string ((Bufbyte *) modes, 10);
|
|
900 #if defined (BSD4_2) || defined (BSD4_3) /* file gid will be dir gid */
|
|
901 {
|
|
902 struct stat sdir;
|
|
903
|
442
|
904 if (!NILP (directory) && xemacs_stat ((char *) XSTRING_DATA (directory), &sdir) == 0)
|
428
|
905 values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
|
|
906 else /* if we can't tell, assume worst */
|
|
907 values[9] = Qt;
|
|
908 }
|
|
909 #else /* file gid will be egid */
|
|
910 values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
|
|
911 #endif /* BSD4_2 or BSD4_3 */
|
|
912 values[10] = make_int (s.st_ino);
|
|
913 values[11] = make_int (s.st_dev);
|
|
914 UNGCPRO;
|
|
915 return Flist (countof (values), values);
|
|
916 }
|
|
917
|
|
918
|
|
919 /************************************************************************/
|
|
920 /* initialization */
|
|
921 /************************************************************************/
|
|
922
|
|
923 void
|
|
924 syms_of_dired (void)
|
|
925 {
|
|
926 defsymbol (&Qdirectory_files, "directory-files");
|
|
927 defsymbol (&Qfile_name_completion, "file-name-completion");
|
|
928 defsymbol (&Qfile_name_all_completions, "file-name-all-completions");
|
|
929 defsymbol (&Qfile_attributes, "file-attributes");
|
|
930
|
|
931 DEFSUBR (Fdirectory_files);
|
|
932 DEFSUBR (Ffile_name_completion);
|
|
933 DEFSUBR (Ffile_name_all_completions);
|
|
934 DEFSUBR (Fuser_name_completion);
|
|
935 DEFSUBR (Fuser_name_completion_1);
|
|
936 DEFSUBR (Fuser_name_all_completions);
|
|
937 DEFSUBR (Ffile_attributes);
|
|
938 }
|
|
939
|
|
940 void
|
|
941 vars_of_dired (void)
|
|
942 {
|
|
943 DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions /*
|
|
944 *Completion ignores filenames ending in any string in this list.
|
|
945 This variable does not affect lists of possible completions,
|
|
946 but does affect the commands that actually do completions.
|
|
947 It is used by the functions `file-name-completion' and
|
|
948 `file-name-all-completions'.
|
|
949 */ );
|
|
950 Vcompletion_ignored_extensions = Qnil;
|
|
951 }
|