summaryrefslogtreecommitdiff
path: root/features/progname/progname.c
blob: c025cfed993015d1fb786a3044d33805495cb2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2014 by Aleksey Cheusov
 * See LICENSE file in the distribution.
 */

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <mkc_progname.h>

static const char *__prog = NULL;

const char * getprogname (void)
{
	if (__prog)
		return __prog;

#ifdef HAVE_FUNC0_GETEXECNAME_STDLIB_H
	/* SunOS */
	setprogname (getexecname ());
	return getprogname ();
#elif defined(HAVE_VAR_PROGRAM_INVOCATION_SHORT_NAME_ERRNO_H)
	return program_invocation_short_name;
#else
	return "<unset_progname>";
#endif
}

void setprogname (const char *progname)
{
	const char *s = strrchr (progname, '/');
	__prog = progname;

	if (s)
		__prog = s + 1;
}