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