2

Are there predefined Emacs functions to check if a file is a Unix socket?

The best I could come up with is the following code.

(defun socket-p (fname)
  "Return non-nil if FNAME is a Unix Socket, nil otherwise."
  (eq (string-to-char (nth 8 (file-attributes fname))) ?s))

Is there something else in the built-in Emacs or its core libraries?

PRouleau
  • 744
  • 3
  • 10
  • Eshell's `eshell-pred-file-type` boils down to the same thing, FWIW. – Basil Jul 18 '21 at 21:23
  • 2
    The `(nth 8 ...)` should probably be `(file-attribute-modes ...)` but other than that, I don't know of any other way. – NickD Jul 19 '21 at 00:18
  • files-attribute-modes being a defsubst in files.el that is always available is indeed a better way. I did not know about it. Thanks! – PRouleau Jul 19 '21 at 02:34
  • 1
    @PRouleau Those convenience accessors for `file-attributes` were added in Emacs 26, so you'll still find a lot of code using `nth`. – Basil Jul 19 '21 at 12:42
  • @Basil This is important info. Thanks! – PRouleau Jul 19 '21 at 14:35

0 Answers0