dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / progname.c
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * progname.c - program name handling functions
4 *
5 * Copyright © 2011 Guillem Jover <guillem@debian.org>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include <config.h>
22 #include <compat.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26
27 #include <dpkg/path.h>
28 #include <dpkg/progname.h>
29
30 static const char *progname;
31
32 /**
33 * Set the program name.
34 *
35 * This function will set the program name which will be used by error
36 * reporting functions, among others.
37 *
38 * @param name The new program name.
39 */
40 void
41 dpkg_set_progname(const char *name)
42 {
43 progname = path_basename(name);
44 }
45
46 #if defined(HAVE___PROGNAME)
47 extern const char *__progname;
48 #endif
49
50 /**
51 * Get the program name.
52 *
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.
57 *
58 * @return A pointer to a static buffer with the program name.
59 */
60 const char *
61 dpkg_get_progname(void)
62 {
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());
73 #endif
74 }
75
76 return progname;
77 }