summaryrefslogtreecommitdiff
path: root/features/progname/progname.c
diff options
context:
space:
mode:
Diffstat (limited to 'features/progname/progname.c')
-rw-r--r--features/progname/progname.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/features/progname/progname.c b/features/progname/progname.c
new file mode 100644
index 0000000..c025cfe
--- /dev/null
+++ b/features/progname/progname.c
@@ -0,0 +1,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;
+}