chiark / gitweb /
Prep v238: Uncomment now needed headers and unmask now needed functions in src/shared...
[elogind.git] / src / shared / musl_missing.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of elogind.
4
5   Copyright 2017 Sven Eden
6
7   elogind is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   elogind is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with elogind; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <string.h>
23
24 #include "alloc-util.h"
25
26 #ifndef __GLIBC__
27 char *program_invocation_name       = NULL;
28 char *program_invocation_short_name = NULL;
29 #endif // __GLIBC__
30
31 #include "musl_missing.h"
32
33 static void elogind_free_program_name(void) {
34         if (program_invocation_name)
35                 program_invocation_name       = mfree(program_invocation_name);
36         if (program_invocation_short_name)
37                 program_invocation_short_name = mfree(program_invocation_short_name);
38 }
39
40 void elogind_set_program_name(const char* pcall) {
41         assert(pcall && pcall[0]);
42
43         if ( ( program_invocation_name
44             && strcmp(program_invocation_name, pcall))
45           || ( program_invocation_short_name
46             && strcmp(program_invocation_short_name, basename(pcall)) ) )
47                 elogind_free_program_name();
48
49         if (NULL == program_invocation_name)
50                 program_invocation_name       = strdup(pcall);
51         if (NULL == program_invocation_short_name)
52                 program_invocation_short_name = strdup(basename(pcall));
53
54 #ifndef __GLIBC__
55         atexit(elogind_free_program_name);
56 #endif // __GLIBC__
57 }
58