6.18.4 Directories

You can split a file name into a directory and base component:

basename ( c-addr1 u1 – c-addr2 u2  ) gforth-0.7 “basename”

Given a file name c-addr1 u1, c-addr2 u2 is the part of it with any leading directory components removed.

dirname ( c-addr1 u1 – c-addr1 u2  ) gforth-0.7 “dirname”

C-addr1 u2 is the directory name of the file name c-addr1 u1, including the final /. If caddr1 u1 does not contain a /, u2=0.

You can open and read directories similar to files. Reading gives you one directory entry at a time; you can match that to a filename (with wildcards).

open-dir ( c-addr u – wdirid wior ) gforth-0.5 “open-dir”

Open the directory specified by c-addr, u and return dir-id for futher access to it.

read-dir ( c-addr u1 wdirid – u2 flag wior ) gforth-0.5 “read-dir”

Attempt to read the next entry from the directory specified by dir-id to the buffer of length u1 at address c-addr. If the attempt fails because there is no more entries, ior=0, flag=0, u2=0, and the buffer is unmodified. If the attempt to read the next entry fails because of any other reason, return ior<>0. If the attempt succeeds, store file name to the buffer at c-addr and return ior=0, flag=true and u2 equal to the size of the file name. If the length of the file name is greater than u1, store first u1 characters from file name into the buffer and indicate "name too long" with ior, flag=true, and u2=u1.

close-dir ( wdirid – wior ) gforth-0.5 “close-dir”

Close the directory specified by dir-id.

filename-match ( c-addr1 u1 c-addr2 u2 – flag ) gforth-0.5 “match-file”

match the file name c_addr1 u1 with the pattern c_addr2 u2. Patterns match char by char except for the special characters ’*’ and ’?’, which are wildcards for several (’*’) or one (’?’) character.

get-dir ( c-addr1 u1 – c-addr2 u2 ) gforth-0.7 “get-dir”

Store the current directory in the buffer specified by c-addr1, u1. If the buffer size is not sufficient, return 0 0

set-dir ( c-addr u – wior ) gforth-0.7 “set-dir”

Change the current directory to c-addr, u. Return an error if this is not possible

=mkdir ( c-addr u wmode – wior ) gforth-0.7 “equals-mkdir”

Create directory c-addr u with mode wmode.

mkdir-parents ( c-addr u mode – ior  ) gforth-0.7 “mkdir-parents”

create the directory c-addr u and all its parents with mode mode (modified by umask)