Function: file-in-directory-p
file-in-directory-p is a byte-compiled function defined in
files.el.gz.
Signature
(file-in-directory-p FILE DIR)
Documentation
Return non-nil if DIR is a parent directory of FILE.
Value is non-nil if FILE is inside DIR or inside a subdirectory of DIR. A directory is considered to be a "parent" of itself. DIR must be an existing directory, otherwise the function returns nil.
Other relevant functions are documented in the file group.
Probably introduced at or before Emacs version 24.1.
Shortdoc
;; file
(file-in-directory-p "/tmp/foo" "/tmp/")
e.g. => t
Source Code
;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun file-in-directory-p (file dir)
"Return non-nil if DIR is a parent directory of FILE.
Value is non-nil if FILE is inside DIR or inside a subdirectory of DIR.
A directory is considered to be a \"parent\" of itself.
DIR must be an existing directory, otherwise the function returns nil."
(let ((handler (or (find-file-name-handler file 'file-in-directory-p)
(find-file-name-handler dir 'file-in-directory-p))))
(if handler
(funcall handler 'file-in-directory-p file dir)
(when (file-directory-p dir) ; DIR must exist.
(setq file (file-truename file)
dir (file-truename dir))
(let ((ls1 (split-string file "/" t))
(ls2 (split-string dir "/" t))
(root
(cond
;; A UNC on Windows systems, or a "super-root" on Apollo.
((string-match "\\`//" file) "//")
((string-match "\\`/" file) "/")
(t "")))
(mismatch nil))
(while (and ls1 ls2 (not mismatch))
(if (string-equal (car ls1) (car ls2))
(setq root (concat root (car ls1) "/"))
(setq mismatch t))
(setq ls1 (cdr ls1)
ls2 (cdr ls2)))
(unless mismatch
(file-equal-p root dir)))))))