summaryrefslogtreecommitdiff
path: root/lib/getprogname.c
diff options
context:
space:
mode:
authorPino Toscano <ptoscano@redhat.com>2016-08-18 15:18:22 +0200
committerJim Meyering <meyering@fb.com>2016-09-05 09:21:54 -0700
commit653a5be9759c9757a2509dad5057064e97899517 (patch)
treec89490809054cabe5084b936be32eeb949db3723 /lib/getprogname.c
parent73affcd234bd83a5d8635362db5b483569e9b0f4 (diff)
downloadgnulib-653a5be9759c9757a2509dad5057064e97899517.tar.gz
getprogname: new module
This provides a LGPL module for getting the name of the current program, using the same API found on *BSD systems. * lib/getprogname.c, lib/getprogname.h, m4/getprogname.m4: * modules/getprogname: New files. * MODULES.html.sh (Misc): Add getprogname. * NEWS: Document the deprecation of the 'progname' module.
Diffstat (limited to 'lib/getprogname.c')
-rw-r--r--lib/getprogname.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/getprogname.c b/lib/getprogname.c
new file mode 100644
index 0000000000..ab2628388e
--- /dev/null
+++ b/lib/getprogname.c
@@ -0,0 +1,45 @@
+/* Program name management.
+ Copyright (C) 2016 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include "getprogname.h"
+
+#include <errno.h> /* get program_invocation_name declaration */
+#include <string.h>
+
+
+#ifndef HAVE_GETPROGNAME
+const char *
+getprogname (void)
+{
+#if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
+ return program_invocation_short_name;
+#elif HAVE_DECL_PROGRAM_INVOCATION_NAME
+ const char *base = program_invocation_name;
+ const char *slash;
+
+ slash = strrchr (base, '/');
+ if (slash != NULL)
+ base = slash + 1;
+
+ return base;
+#else
+ #error "getprogname module not ported to this OS"
+#endif
+}
+#endif