help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Efficiently checking the initial contents of a file


From: Thien-Thi Nguyen
Subject: Re: Efficiently checking the initial contents of a file
Date: Thu, 22 May 2008 18:58:44 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

() Nordlöw <per.nordlow@gmail.com>
() Mon, 19 May 2008 00:20:26 -0700 (PDT)

   Is I see it the most general and efficient solution
   to this problem would be to

Hmm, i tend to find "most general" and "most efficient"
to be mutually exclusive.  Perhaps we have different
ideas of what is general and what is efficient.

Here is a relatively efficient and highly parsimonious
way, made by squeezing the ideas previously presented
by others into a lower-bound wrap-check:

(defun elf-p (filename)
  (when (< 4 (nth 7 (file-attributes filename)))
    (with-temp-buffer
      (insert-file-contents filename nil 0 4)
      (string= "ELF" (buffer-string)))))

For a "most general" way, i'm in the process of
implementing a file(1)-workalike in Emacs Lisp.  For a
peek at its design, you can see the sexp-based ruleset
(and a Scheme prototype linked therefrom) at:

http://www.gnuvola.org/data/  (de-uglified magic file)

Lastly, if the original problem uses `elf-p' as a
filter, unless you labor under a pitifully degrading
(eg., usloth) environment, you may find the "best way"
is to use `file-executable-p' instead.

thi




reply via email to

[Prev in Thread] Current Thread [Next in Thread]