comparison lisp/files.el @ 41794:570eb21cf9d8

(insert-directory): If the df output does not look right, don't try to use it. Other cleanups in overall code structure.
author Richard M. Stallman <rms@gnu.org>
date Mon, 03 Dec 2001 00:02:52 +0000
parents 7da74fc76e19
children ce19ab149767
comparison
equal deleted inserted replaced
41793:1adf81b0d0e4 41794:570eb21cf9d8
3574 whose name is in the variable `insert-directory-program'. 3574 whose name is in the variable `insert-directory-program'.
3575 If WILDCARD, it also runs the shell specified by `shell-file-name'." 3575 If WILDCARD, it also runs the shell specified by `shell-file-name'."
3576 ;; We need the directory in order to find the right handler. 3576 ;; We need the directory in order to find the right handler.
3577 (let ((handler (find-file-name-handler (expand-file-name file) 3577 (let ((handler (find-file-name-handler (expand-file-name file)
3578 'insert-directory))) 3578 'insert-directory)))
3579 (if handler 3579 (if handler
3580 (funcall handler 'insert-directory file switches 3580 (funcall handler 'insert-directory file switches
3581 wildcard full-directory-p) 3581 wildcard full-directory-p)
3582 (if (eq system-type 'vax-vms) 3582 (if (eq system-type 'vax-vms)
3583 (vms-read-directory file switches (current-buffer)) 3583 (vms-read-directory file switches (current-buffer))
3584 (let* ((coding-system-for-read 3584 (let (result available)
3585 (and enable-multibyte-characters 3585
3586 (or file-name-coding-system 3586 ;; Read the actual directory using `insert-directory-program'.
3587 default-file-name-coding-system))) 3587 ;; RESULT gets the status code.
3588 ;; This is to control encoding the arguments in call-process. 3588 (let ((coding-system-for-read
3589 (coding-system-for-write coding-system-for-read) 3589 (and enable-multibyte-characters
3590 (result 3590 (or file-name-coding-system
3591 (if wildcard 3591 default-file-name-coding-system)))
3592 ;; Run ls in the directory of the file pattern we asked for 3592 ;; This is to control encoding the arguments in call-process.
3593 (let ((default-directory 3593 (coding-system-for-write coding-system-for-read))
3594 (if (file-name-absolute-p file) 3594 (setq result
3595 (file-name-directory file) 3595 (if wildcard
3596 (file-name-directory (expand-file-name file)))) 3596 ;; Run ls in the directory part of the file pattern
3597 (pattern (file-name-nondirectory file))) 3597 ;; using the last component as argument.
3598 (call-process 3598 (let ((default-directory
3599 shell-file-name nil t nil 3599 (if (file-name-absolute-p file)
3600 "-c" (concat (if (memq system-type '(ms-dos windows-nt)) 3600 (file-name-directory file)
3601 "" 3601 (file-name-directory (expand-file-name file))))
3602 "\\") ; Disregard Unix shell aliases! 3602 (pattern (file-name-nondirectory file)))
3603 insert-directory-program 3603 (call-process
3604 " -d " 3604 shell-file-name nil t nil
3605 (if (stringp switches) 3605 "-c"
3606 switches 3606 (concat (if (memq system-type '(ms-dos windows-nt))
3607 (mapconcat 'identity switches " ")) 3607 ""
3608 " -- " 3608 "\\") ; Disregard Unix shell aliases!
3609 ;; Quote some characters that have 3609 insert-directory-program
3610 ;; special meanings in shells; but 3610 " -d "
3611 ;; don't quote the wildcards--we 3611 (if (stringp switches)
3612 ;; want them to be special. We 3612 switches
3613 ;; also currently don't quote the 3613 (mapconcat 'identity switches " "))
3614 ;; quoting characters in case 3614 " -- "
3615 ;; people want to use them 3615 ;; Quote some characters that have
3616 ;; explicitly to quote wildcard 3616 ;; special meanings in shells; but
3617 ;; characters. 3617 ;; don't quote the wildcards--we want
3618 (shell-quote-wildcard-pattern pattern)))) 3618 ;; them to be special. We also
3619 ;; SunOS 4.1.3, SVr4 and others need the "." to list the 3619 ;; currently don't quote the quoting
3620 ;; directory if FILE is a symbolic link. 3620 ;; characters in case people want to
3621 (apply 'call-process 3621 ;; use them explicitly to quote
3622 insert-directory-program nil t nil 3622 ;; wildcard characters.
3623 (append 3623 (shell-quote-wildcard-pattern pattern))))
3624 (if (listp switches) switches 3624 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
3625 (unless (equal switches "") 3625 ;; directory if FILE is a symbolic link.
3626 ;; Split the switches at any spaces so we can 3626 (apply 'call-process
3627 ;; pass separate options as separate args. 3627 insert-directory-program nil t nil
3628 (split-string switches))) 3628 (append
3629 ;; Avoid lossage if FILE starts with `-'. 3629 (if (listp switches) switches
3630 '("--") 3630 (unless (equal switches "")
3631 (progn 3631 ;; Split the switches at any spaces so we can
3632 (if (string-match "\\`~" file) 3632 ;; pass separate options as separate args.
3633 (setq file (expand-file-name file))) 3633 (split-string switches)))
3634 (list 3634 ;; Avoid lossage if FILE starts with `-'.
3635 (if full-directory-p 3635 '("--")
3636 (concat (file-name-as-directory file) ".") 3636 (progn
3637 file)))))))) 3637 (if (string-match "\\`~" file)
3638 (setq file (expand-file-name file)))
3639 (list
3640 (if full-directory-p
3641 (concat (file-name-as-directory file) ".")
3642 file))))))))
3643
3644 ;; If `insert-directory-program' failed, signal an error.
3638 (if (/= result 0) 3645 (if (/= result 0)
3639 ;; We get here if `insert-directory-program' failed.
3640 ;; On non-Posix systems, we cannot open a directory, so 3646 ;; On non-Posix systems, we cannot open a directory, so
3641 ;; don't even try, because that will always result in 3647 ;; don't even try, because that will always result in
3642 ;; the ubiquitous "Access denied". Instead, show them 3648 ;; the ubiquitous "Access denied". Instead, show the
3643 ;; the `ls' command line and let them guess what went 3649 ;; command line so the user can try to guess what went wrong.
3644 ;; wrong.
3645 (if (and (file-directory-p file) 3650 (if (and (file-directory-p file)
3646 (memq system-type '(ms-dos windows-nt))) 3651 (memq system-type '(ms-dos windows-nt)))
3647 (error 3652 (error
3648 "Reading directory: \"%s %s -- %s\" exited with status %s" 3653 "Reading directory: \"%s %s -- %s\" exited with status %s"
3649 insert-directory-program 3654 insert-directory-program
3650 (if (listp switches) (concat switches) switches) 3655 (if (listp switches) (concat switches) switches)
3651 file result) 3656 file result)
3652 ;; Unix. Access the file to get a suitable error. 3657 ;; Unix. Access the file to get a suitable error.
3653 (access-file file "Reading directory")) 3658 (access-file file "Reading directory")
3654 ;; Replace "total" with "used", to avoid confusion. 3659 (error "Listing directory failed but `access-file' worked")))
3655 ;; Add in the amount of free space. 3660
3656 (save-excursion 3661 ;; Try to insert the amount of free space.
3657 (goto-char (point-min)) 3662 (save-excursion
3658 (when (re-search-forward "^total" nil t) 3663 (goto-char (point-min))
3664 ;; First find the line to put it on.
3665 (when (re-search-forward "^total" nil t)
3666 ;; Try to find the number of free blocks.
3667 (save-match-data
3668 (with-temp-buffer
3669 (call-process "df" nil t nil ".")
3670 ;; Usual format is a header line
3671 ;; followed by a line of numbers.
3672 (goto-char (point-min))
3673 (forward-line 1)
3674 (if (not (eobp))
3675 (progn
3676 ;; Move to the end of the "available blocks" number.
3677 (skip-chars-forward "^ \t")
3678 (forward-word 3)
3679 ;; Copy it into AVAILABLE.
3680 (let ((end (point)))
3681 (forward-word -1)
3682 (setq available (buffer-substring (point) end)))))))
3683 (when available
3684 ;; Replace "total" with "used", to avoid confusion.
3659 (replace-match "used") 3685 (replace-match "used")
3660 (end-of-line) 3686 (end-of-line)
3661 (let (available) 3687 (insert " available " available)))))))))
3662 (with-temp-buffer
3663 (call-process "df" nil t nil ".")
3664 (goto-char (point-min))
3665 (forward-line 1)
3666 (skip-chars-forward "^ \t")
3667 (forward-word 3)
3668 (let ((end (point)))
3669 (forward-word -1)
3670 (setq available (buffer-substring (point) end))))
3671 (insert " available " available))))))))))
3672 3688
3673 (defun insert-directory-safely (file switches 3689 (defun insert-directory-safely (file switches
3674 &optional wildcard full-directory-p) 3690 &optional wildcard full-directory-p)
3675 "Insert directory listing for FILE, formatted according to SWITCHES. 3691 "Insert directory listing for FILE, formatted according to SWITCHES.
3676 3692