6.20.5.2 General Search Paths

Your application may need to search files in several directories, like included does. To facilitate this, Gforth allows you to define and use your own search paths, by providing generic equivalents of the Forth search path words:

open-path-file ( addr1 u1 path-addr – wfileid addr2 u2 0 | ior  ) gforth-0.2 “open-path-file”

Look in path path-addr for the file specified by addr1 u1. If found, the resulting path and an (read-only) open file descriptor are returned. If the file is not found, ior is what came back from the last attempt at opening the file (in the current implementation).

file>path ( c-addr1 u1 path-addr – c-addr2 u2  ) gforth-1.0 “file>path”

Searches for a file with the name c-addr1 u1 in path stored in path-addr. If successful, c-addr u2 is the absolute file name or the file name relative to the current working directory. Throws an exception if the file cannot be opened.

clear-path ( path-addr –  ) gforth-0.5 “clear-path”

Set the path path-addr to empty.

also-path ( c-addr len path-addr –  ) gforth-0.4 “also-path”

add the directory c-addr len to path-addr.

.path ( path-addr –  ) gforth-0.4 “.path”

Display the contents of the search path path-addr.

path+ ( path-addr  "dir" –  ) gforth-0.4 “path+”

Add the directory dir to the search path path-addr.

path= ( path-addr "dir1|dir2|dir3" –  ) gforth-0.4 “path-equals”

Make a complete new search path; the path separator is |.

Here’s an example of creating a custom search path:

variable mypath \ no special allocation required, just a variable
mypath path= /lib|/usr/lib \ assign initial directories
mypath path+ /usr/local/lib \ append directory
mypath .path \ output:"/lib /usr/lib /usr/local/lib"

Search file and show resulting path:

s" libm.so" mypath open-path-file throw type close-file \ output:"/lib/libm.so"