[MLton-user] Path of Executable

Wesley W. Terpstra wesley at terpstra.ca
Mon Jan 8 17:46:07 PST 2007


On Jan 9, 2007, at 12:54 AM, Neal Glew wrote:
> Is there a function in any of the libraries available with Mlton for
> getting the path of the executable the program is being run from?

Assuming you just need the relative path:
> val () = print (CommandLine.name () ^ "\n")

If you need an absolute path, you can do:
> val cwd = OS.FileSys.getDir ()
> val exe = CommandLine.name ()
>
> (* the easy way to get a canonical path (might be wrong with  
> symlinks: *)
> val full = OS.Path.mkAbsolute { path = exe, relativeTo = cwd }
> val () = print (full ^ "\n")
> val dir_only = OS.Path.dir full (* remove the executable name *)
> val () = print (dir_only ^ "\n")
>
> (* or, if you need to leave it in non-canonical form: *)
> val full = if OS.Path.isRelative exe then OS.Path.concat (cwd, exe)  
> else exe
> val () = print (full ^ "\n")
> val dir_only = OS.Path.dir full
> val () = print (dir_only ^ "\n")

Here's an example output:
> kiwiw:~/muffin terpstra$ ../foo
> /Users/terpstra/foo
> /Users/terpstra
> /Users/terpstra/muffin/../foo
> /Users/terpstra/muffin/..





More information about the MLton-user mailing list