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?

  • Eshell's eshell-pred-file-type boils down to the same thing, FWIW.
    – Basil
    Jul 18 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 at 0: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 at 2: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 at 12:42
  • @Basil This is important info. Thanks!
    – PRouleau
    Jul 19 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.