commit e2a597589b80958442e7945e46c3f8704a16cedd
parent a139a262c112b90622070a895ccc6480a46b39ab
Author: Daniel Barlow <dan@telent.net>
Date: Fri, 6 Sep 2024 00:08:40 +0100
anoia.fs.find-executable looks for bin in colon-sep list of directories
Diffstat:
1 file changed, 18 insertions(+), 0 deletions(-)
diff --git a/pkgs/anoia/fs.fnl b/pkgs/anoia/fs.fnl
@@ -1,4 +1,5 @@
(local ll (require :lualinux))
+(import-macros { : define-tests : expect : expect= } :anoia.assert)
(local S_IFMT 0xf000)
(local S_IFSOCK 0xc000)
@@ -88,6 +89,22 @@
(values pid cin-d cout-s))
(nil err) (error (.. "popen pipe out: " err)))
(nil err) (error (.. "popen pipe in: " err))))
+;; lualinux doesn't publish access(2), this is not exactly
+;; the same but will suffice until we can add it
+(fn executable? [f]
+ (let [statbuf {}
+ stat (ll.lstat f statbuf 1)]
+ (and stat (> (band (. stat 3) 73) 0)))) ; \0111
+
+(fn find-executable [exe search-path]
+ (accumulate [full-path nil
+ p (string.gmatch search-path "(.-):")]
+ (or full-path (let [f (.. p "/" exe)] (and (executable? f) f)))))
+
+(define-tests
+ (let [p (find-executable "yes" (os.getenv "PATH"))]
+ (expect (string.match p "coreutils.+bin/yes$"))))
+
{
: mktree
@@ -96,5 +113,6 @@
: dir
: file-type
: popen2
+ : find-executable
:symlink (fn [from to] (ll.symlink from to))
}