macOS port: fix errno and libc loading across stdlib

ober

55ee3e5e68b0e91cb136e241ae0559ec9f68de40

diff --git a/lib/std/misc/process.sls b/lib/std/misc/process.sls
index 5f75f07..2acaaf5 100644
--- a/lib/std/misc/process.sls
+++ b/lib/std/misc/process.sls
@@ -258,7 +258,9 @@
           #f
           (or (guard (e [#t #f]) (load-shared-object "libc.so.7"))
               (guard (e [#t #f]) (load-shared-object "libc.so.6"))
-              (load-shared-object "libc.so")))))
+              (guard (e [#t #f]) (load-shared-object "libc.so"))
+              (guard (e [#t #f]) (load-shared-object #f))  ;; macOS: resolve from current process
+              #f))))
 
   (define c-kill (foreign-procedure "kill" (int int) int))
   (define c-isatty (foreign-procedure "isatty" (int) int))
diff --git a/lib/std/net/repl.sls b/lib/std/net/repl.sls
index 968befc..2db2e8c 100644
--- a/lib/std/net/repl.sls
+++ b/lib/std/net/repl.sls
@@ -31,9 +31,17 @@
   (define c-getsockname (foreign-procedure "getsockname" (int void* void*) int))
   (define c-fcntl     (foreign-procedure "fcntl" (int int int) int))
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
 
   ;; Constants (values differ between Linux and FreeBSD)
diff --git a/lib/std/net/tcp-raw.sls b/lib/std/net/tcp-raw.sls
index 27e1d1e..6ec06ec 100644
--- a/lib/std/net/tcp-raw.sls
+++ b/lib/std/net/tcp-raw.sls
@@ -37,9 +37,17 @@
 
   ;; errno
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
   (define EINTR 4)
 
diff --git a/lib/std/net/tcp.sls b/lib/std/net/tcp.sls
index 1e8eb97..9bf07ab 100644
--- a/lib/std/net/tcp.sls
+++ b/lib/std/net/tcp.sls
@@ -51,9 +51,12 @@
 
   ;; errno access for EINTR retry
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+              (and (>= (string-length mt) 3)
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+        (foreign-procedure "__error" () void*)
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
   (define EINTR 4)
   (define *freebsd?* (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb)))
diff --git a/lib/std/os/fcntl.sls b/lib/std/os/fcntl.sls
index 8901dca..9f5e0e0 100644
--- a/lib/std/os/fcntl.sls
+++ b/lib/std/os/fcntl.sls
@@ -28,9 +28,17 @@
 
   ;; errno access for error reporting
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
 
   ;; ========== Constants ==========
diff --git a/lib/std/os/flock.sls b/lib/std/os/flock.sls
index ab1c830..9b2a637 100644
--- a/lib/std/os/flock.sls
+++ b/lib/std/os/flock.sls
@@ -24,9 +24,17 @@
 
   ;; errno access
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
   (define *freebsd?* (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb)))
   (define EWOULDBLOCK (if *freebsd?* 35 11))
diff --git a/lib/std/os/posix.sls b/lib/std/os/posix.sls
index 1b4601f..3b07306 100644
--- a/lib/std/os/posix.sls
+++ b/lib/std/os/posix.sls
@@ -98,9 +98,17 @@
   ;; errno is thread-local via __errno_location on Linux / __error on FreeBSD
   (define c-errno-location
     (guard (e [#t #f])
-      (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-        (foreign-procedure "__error" () void*)
-        (foreign-procedure "__errno_location" () void*))))
+      (let ((mt (symbol->string (machine-type))))
+
+        (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+                (and (>= (string-length mt) 3)
+
+                     (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+          (foreign-procedure "__error" () void*)
+
+          (foreign-procedure "__errno_location" () void*)))))
 
   (define (posix-errno)
     (if c-errno-location
diff --git a/lib/std/os/signalfd.sls b/lib/std/os/signalfd.sls
index ae7d969..2c65ddc 100644
--- a/lib/std/os/signalfd.sls
+++ b/lib/std/os/signalfd.sls
@@ -56,9 +56,17 @@
 
   ;; errno access
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
   (define EINTR 4)
   (define EAGAIN (if *freebsd?* 35 11))
diff --git a/lib/std/security/landlock.sls b/lib/std/security/landlock.sls
index d8145c5..4f4119e 100644
--- a/lib/std/security/landlock.sls
+++ b/lib/std/security/landlock.sls
@@ -56,9 +56,17 @@
 
   (define c-errno
     (guard (e [#t (lambda () 0)])
-      (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-        (foreign-procedure "__error" () void*)
-        (foreign-procedure "__errno_location" () void*))))
+      (let ((mt (symbol->string (machine-type))))
+
+        (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+                (and (>= (string-length mt) 3)
+
+                     (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+          (foreign-procedure "__error" () void*)
+
+          (foreign-procedure "__errno_location" () void*)))))
 
   (define (get-errno)
     (guard (e [#t 0])
diff --git a/lib/std/security/seccomp.sls b/lib/std/security/seccomp.sls
index fd6223f..af58d8e 100644
--- a/lib/std/security/seccomp.sls
+++ b/lib/std/security/seccomp.sls
@@ -47,9 +47,17 @@
 
   (define c-errno
     (guard (e [#t (lambda () 0)])
-      (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-        (foreign-procedure "__error" () void*)
-        (foreign-procedure "__errno_location" () void*))))
+      (let ((mt (symbol->string (machine-type))))
+
+        (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+                (and (>= (string-length mt) 3)
+
+                     (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+          (foreign-procedure "__error" () void*)
+
+          (foreign-procedure "__errno_location" () void*)))))
 
   (define (get-errno)
     (guard (e [#t 0])
diff --git a/lib/std/web/fastcgi.sls b/lib/std/web/fastcgi.sls
index 9de5b82..5f4e501 100644
--- a/lib/std/web/fastcgi.sls
+++ b/lib/std/web/fastcgi.sls
@@ -44,9 +44,17 @@
   (define c-inet-pton (foreign-procedure "inet_pton" (int string void*) int))
   (define c-fcntl     (foreign-procedure "fcntl" (int int int) int))
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
 
   (define AF_INET 2)
diff --git a/lib/std/web/rack.sls b/lib/std/web/rack.sls
index 3589a40..e3f3b43 100644
--- a/lib/std/web/rack.sls
+++ b/lib/std/web/rack.sls
@@ -39,9 +39,17 @@
   (define c-inet-pton (foreign-procedure "inet_pton" (int string void*) int))
   (define c-fcntl     (foreign-procedure "fcntl" (int int int) int))
   (define c-errno-location
-    (if (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
-      (foreign-procedure "__error" () void*)
-      (foreign-procedure "__errno_location" () void*)))
+    (let ((mt (symbol->string (machine-type))))
+
+      (if (or (memq (machine-type) '(a6fb ta6fb i3fb ti3fb arm64fb))
+
+              (and (>= (string-length mt) 3)
+
+                   (string=? (substring mt (- (string-length mt) 3) (string-length mt)) "osx")))
+
+        (foreign-procedure "__error" () void*)
+
+        (foreign-procedure "__errno_location" () void*))))
   (define (get-errno) (foreign-ref 'int (c-errno-location) 0))
 
   (define AF_INET 2)