If you're on using a glibc, then:
#define _GNU_SOURCE
#include <errno.h>
extern char *program_invocation_name;
extern char *program_invocation_short_name;
See program_invocation_name(3)
Under most Unices, __progname
is also defined by the libc.
The sole portable way is to use argv[0]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…