I found out the next issue in this simple code:
let () =
print_endline "Hello";
print_endline (Unix.getlogin ())
Running in the normal case, with ./a.out gives:
Hello
ricardo
But running like ./a.out </dev/null makes Unix.getlogin fail:
Hello
Fatal error: exception Unix.Unix_error(20, "getlogin", "")
Any idea why this happens?
Redirecting the input of a program overrides its controlling terminal. Without a controlling terminal, there is no login to be found:
You can, however, still find a user’s name (perhaps) by getting the user’s id (
getuid) and looking up his passwd entry (related docs) (getpwuid), then finding his username in it.