For the _CWD$ replacement, you can do this:
' Need to use DECLARE LIBRARY for the C function getcwd(string, stringSize).
DECLARE LIBRARY ""
FUNCTION getcwd$ (buffer$, BYVAL buflen)
END DECLARE
' Wrapper function for making usage of getcwd$ more BASIC-like.
DECLARE FUNCTION qb64cwd$ ()
' Print the current working directory.
PRINT qb64cwd$
FUNCTION qb64cwd$ ()
' 32768 characters should be more than large enough on any OS.
REDIM s AS STRING * 32768
qb64cwd$ = getcwd$(s, 32768)
END FUNCTION
While you don't really need to have a wrapper function, the C function requires you to pass a string with a sufficient amount of writable memory. That is, getcwd
doesn't allocate memory. It expects you to pass a sufficient amount of memory, and QB64's dynamically sized STRING
type won't do that, so a wrapper is used to create a fixed-length string of sufficient size and pass that to the function. wrapper does that well enough to suffice in most cases. Note that this should also work on OS X and Linux (and pretty much any other POSIX-like system that QB64 runs on, possibly even including Android). I haven't tested on those systems, but it should work since getcwd
is a POSIX function.
What happens when that number isn't large enough? Well, QB64 doesn't allow arrays to be passed to library functions, and you can't use STRING * variable
unless variable
is CONST variable = ...
. That means you can't enlarge the string and try again. You should probably raise an error if that happens to to tell you that something went wrong (e.g. ERROR 75
).