2 * libdpkg - Debian packaging suite library routines
3 * progname.c - program name handling functions
5 * Copyright © 2011 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 #ifdef HAVE_GETPROCS64
25 #include <sys/types.h>
33 #include <dpkg/path.h>
34 #include <dpkg/progname.h>
36 static const char *progname
;
39 * Set the program name.
41 * This function will set the program name which will be used by error
42 * reporting functions, among others.
44 * @param name The new program name.
47 dpkg_set_progname(const char *name
)
49 progname
= path_basename(name
);
52 #if defined(HAVE___PROGNAME)
53 extern const char *__progname
;
57 * Get the program name.
59 * The program name might have been set previously by dpkg_set_progname(),
60 * but if not this function will try to initialize it by system dependent
61 * means, so it's half safe to not call dpkg_set_progname() at all. At worst
62 * the function might return NULL in that case.
64 * @return A pointer to a static buffer with the program name.
67 dpkg_get_progname(void)
69 if (progname
== NULL
) {
70 #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
71 progname
= program_invocation_short_name
;
72 #elif defined(HAVE___PROGNAME)
73 progname
= __progname
;
74 #elif defined(HAVE_GETPROGNAME)
75 progname
= getprogname();
76 #elif defined(HAVE_GETEXECNAME)
77 /* getexecname(3) returns an absolute path, normalize it. */
78 dpkg_set_progname(getexecname());
79 #elif defined(HAVE_GETPROCS64)
80 struct progentry64 pe
;
83 if (getprocs64(&pe
, sizeof(pe
), NULL
, 0, &pid
, 1) >= 0)
84 progname
= strdup(pe
.pi_comm
);