1 /* Program name management.
2 Copyright (C) 2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "getprogname.h"
22 #include <errno.h> /* get program_invocation_name declaration */
23 #include <stdlib.h> /* get __argv declaration */
27 # include <procinfo.h>
33 #ifndef HAVE_GETPROGNAME
38 # if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
39 return program_invocation_short_name;
40 # elif HAVE_DECL_PROGRAM_INVOCATION_NAME
41 return last_component (program_invocation_name);
42 # elif HAVE_GETEXECNAME
43 const char *p = getexecname ();
46 return last_component (p);
47 # elif HAVE_DECL___ARGV
48 const char *p = __argv && __argv[0] ? __argv[0] : "?";
49 return last_component (p);
50 # elif HAVE_VAR___PROGNAME
51 /* Be careful to declare this only when we absolutely need it
52 (OpenBSD 5.1), rather than when it's available. Otherwise,
53 its mere declaration makes program_invocation_short_name
54 malfunction (have zero length) with Fedora 25's glibc. */
55 extern char *__progname;
56 const char *p = __progname;
57 return p && p[0] ? p : "?";
59 /* Idea by Bastien ROUCARIÈS <address@hidden>,
60 http://lists.gnu.org/archive/html/bug-gnulib/2010-12/msg00095.html
62 ibm.biz/knowctr#ssw_aix_53/com.ibm.aix.basetechref/doc/basetrf1/getprocs.htm
69 pid_t pid = getpid ();
70 struct procentry64 procs;
71 p = (0 < getprocs64 (&procs, sizeof procs, NULL, 0, &pid, 1)
72 ? strdup (procs.pi_comm)
79 # error "getprogname module not ported to this OS"