#include "getprogname.h"
#if !HAVE_GETPROGNAME
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <windows.h>
#include <stdlib.h>
static INIT_ONCE getprogname_init_once = INIT_ONCE_STATIC_INIT;
static TCHAR progname[_MAX_FNAME];
static BOOL CALLBACK
getprogname_init_once_handler(PINIT_ONCE InitOnce, PVOID Parameter,
PVOID *lpContext)
{
TCHAR path[MAX_PATH];
DWORD length = GetModuleFileName(NULL, path, sizeof(path));
if (length < 0) {
progname[0] = '\0';
return TRUE;
} else {
const char *filename;
path[MAX_PATH - 1] = '\0';
filename = strrchr(path, '\\');
if (filename != NULL) {
filename++;
} else {
filename = path;
}
strcpy_s(progname, sizeof(progname), filename);
return TRUE;
}
}
const char *
getprogname(void)
{
(void)InitOnceExecuteOnce(&getprogname_init_once,
getprogname_init_once_handler,
NULL,
NULL);
return progname;
}
#endif
#endif