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/>.
27 #include <dpkg/path.h>
28 #include <dpkg/progname.h>
30 static const char *progname
;
33 * Set the program name.
35 * This function will set the program name which will be used by error
36 * reporting functions, among others.
38 * @param name The new program name.
41 dpkg_set_progname(const char *name
)
43 progname
= path_basename(name
);
46 #if defined(HAVE___PROGNAME)
47 extern const char *__progname
;
51 * Get the program name.
53 * The program name might have been set previously by dpkg_set_progname(),
54 * but if not this function will try to initialize it by system dependent
55 * means, so it's half safe to not call dpkg_set_progname() at all. At worst
56 * the function might return NULL in that case.
58 * @return A pointer to a static buffer with the program name.
61 dpkg_get_progname(void)
63 if (progname
== NULL
) {
64 #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
65 progname
= program_invocation_short_name
;
66 #elif defined(HAVE___PROGNAME)
67 progname
= __progname
;
68 #elif defined(HAVE_GETPROGNAME)
69 progname
= getprogname();
70 #elif defined(HAVE_GETEXECNAME)
71 /* getexecname(3) returns an absolute path, normalize it. */
72 dpkg_set_progname(getexecname());