| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | /* |
| 3 | * License: GPLv3+ |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* This library was based on and inspired by the gnulib code. |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "messages.h" |
| 27 | |
| 28 | /* String containing name the program is called with. |
| 29 | To be initialized by main(). */ |
| 30 | const char *program_name = NULL; |
| 31 | |
| 32 | /* This string can be used in the final output message */ |
| 33 | const char *program_name_short = NULL; |
| 34 | |
| 35 | /* Set program_name, based on argv[0]. |
| 36 | argv0 must be a string allocated with indefinite extent, and must not be |
| 37 | modified after this call. */ |
| 38 | void |
| 39 | set_program_name (const char *argv0) |
| 40 | { |
| 41 | const char *slash, *underscore; |
| 42 | |
| 43 | if (argv0 == NULL) |
| 44 | { |
| 45 | /* It's a bug in the invoking program. Help diagnosing it. */ |
| 46 | plugin_error (STATE_UNKNOWN, 0, |
| 47 | "A NULL argv[0] was passed through an exec system call"); |
| 48 | } |
| 49 | |
| 50 | slash = strrchr (argv0, '/'); |
| 51 | program_name = (slash != NULL ? slash + 1 : argv0); |
| 52 | |
| 53 | underscore = strchr (program_name, '_'); |
| 54 | program_name_short = (underscore != NULL ? underscore + 1 : program_name); |
| 55 | } |