;; some package around file-stats for returning the type of a file

;;AUTOLOAD: file-type
;;AUTODOC: file-type "return the type of a file"
;;(file-type <name>)
;;
;; Returns an atom being either:
;; directory character-special block-special file symbolic-link socket fifo
;; or () if not found
;; last stats can be found in file-type:stats global var
;; Name can also be the result of a previous file-stats
;;e.g.:
;;     (file-type "/tmp")     ==> 'directory
;;     (file-type "~/.emacs") ==> 'file


(defun file-type (name &optional do-not-follow-symbolic-links
    &aux
    pos
  )
  (if (and (typep name Struct) (getn name 'mode))
    (setq file-type:stats name)
    (setq file-type:stats (file-stats name do-not-follow-symbolic-links))
  )
  (if file-type:stats (progn
      (if (setq pos (seek
	    file-type:plist
	    (logand S_IFMT (getn file-type:stats 'mode))
	))
	(getn file-type:plist (+ pos 1))
))))

(setq file-type:plist
  `(
    ,S_IFREG file
    ,S_IFDIR directory
    ,S_IFLNK symbolic-link
    ,S_IFCHR character-special
    ,S_IFBLK block-special
    ,S_IFSOCK socket
    ,S_IFIFO fifo
))
