- Erlang
Select your favorite languages :
- Or search :
int main(int argc, char* argv[])
{
std::cout << std::filesystem::path(argv[0]).filename() << '\n';
}
Require C++17
program p
implicit none
character(len=:),allocatable :: s
integer :: stat,l,i,j,k
call get_command_argument (0,length=l)
allocate(character(len=l) :: s)
call get_command_argument (0,s,status=stat)
if (stat == 0) then
i=index(s,'/',back=.true.)
j=index(s,'\',back=.true.)
k=max(i,j)
if(k.ne.0)s=s(k+1:)
print *, "The program's name is " // trim (s)
endif
end program p
simplifying assumption is that both / and \ are not allowed in a filename but if present are assumed to delimit the leaf name
fn get_exec_name() -> Option<String> {
std::env::current_exe()
.ok()
.and_then(|pb| pb.file_name().map(|s| s.to_os_string()))
.and_then(|s| s.into_string().ok())
}
fn main() -> () {
let s = get_exec_name().unwrap();
println!("{}", s);
}
programming-idioms.org