Function: ange-ftp-canonize-filename

ange-ftp-canonize-filename is a byte-compiled function defined in ange-ftp.el.gz.

Signature

(ange-ftp-canonize-filename N)

Documentation

Take a string N and short-circuit //, /. and /..

Source Code

;; Defined in /usr/src/emacs/lisp/net/ange-ftp.el.gz
(defun ange-ftp-canonize-filename (n)
  "Take a string N and short-circuit //, /. and /.."
  (if (string-match "[^:]+//" n)		;don't upset Apollo users
      (setq n (substring n (1- (match-end 0)))))
  (let ((parsed (ange-ftp-ftp-name n)))
    (if parsed
	(let ((host (car parsed))
	      (user (nth 1 parsed))
	      (name (nth 2 parsed)))

	  ;; See if remote name is absolute.  If so then just expand it and
	  ;; replace the name component of the overall name.
	  (cond ((string-match "\\`/" name)
		 name)

		;; Name starts with ~ or ~user.  Resolve that part of the name
		;; making it absolute then re-expand it.
		((string-match "\\`~[^/]*" name)
		 (let* ((tilda (match-string 0 name))
			(rest (substring name (match-end 0)))
			(dir (ange-ftp-expand-dir host user tilda)))
		   (if dir
                       ;; C-x d /ftp:anonymous@ftp.gnu.org:~/ RET
                       ;; seems to cause `rest' to sometimes be empty.
                       ;; Maybe it's an error for `rest' to be empty here,
                       ;; but until we figure this out, this quick fix
                       ;; seems to do the trick.
		       (setq name (cond ((string-equal rest "") dir)
					((string-equal dir "/") rest)
					(t (concat dir rest))))
		     (error "User \"%s\" is not known"
			    (substring tilda 1)))))

		;; relative name.  Tack on homedir and re-expand.
		(t
		 (let ((dir (ange-ftp-expand-dir host user "~")))
		   (if dir
		       (setq name (concat
				   (ange-ftp-real-file-name-as-directory dir)
				   name))
		     (error "Unable to obtain CWD")))))

	  ;; If name starts with //, preserve that, for apollo system.
	  (unless (string-match "\\`//" name)
            (if (not (eq system-type 'windows-nt))
                (setq name (ange-ftp-real-expand-file-name name))
              ;; Windows UNC default dirs do not make sense for ftp.
              (setq name (if (and default-directory
				  (string-match "\\`//" default-directory))
                             (ange-ftp-real-expand-file-name name "c:/")
                           (ange-ftp-real-expand-file-name name)))
              ;; Strip off possible drive specifier.
              (if (string-match "\\`[a-zA-Z]:" name)
                  (setq name (substring name 2))))
            (if (string-match "\\`//" name)
                (setq name (substring name 1))))

	  ;; Now substitute the expanded name back into the overall filename.
	  (ange-ftp-replace-name-component n name))

      ;; non-ange-ftp name.  Just expand normally.
      (if (eq (string-to-char n) ?/)
	  (ange-ftp-real-expand-file-name n)
	(ange-ftp-real-expand-file-name
	 (ange-ftp-real-file-name-nondirectory n)
	 (ange-ftp-real-file-name-directory n))))))