xref: /freebsd/contrib/xz/src/common/tuklib_progname.c (revision c57c26179033f64c2011a2d2a904ee3fa62e826a)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       tuklib_progname.c
6 /// \brief      Program name to be displayed in messages
7 //
8 //  Author:     Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #include "tuklib_progname.h"
13 #include <string.h>
14 
15 
16 #ifndef HAVE_PROGRAM_INVOCATION_NAME
17 char *progname = NULL;
18 #endif
19 
20 
21 extern void
22 tuklib_progname_init(char **argv)
23 {
24 #ifdef TUKLIB_DOSLIKE
25 	// On these systems, argv[0] always has the full path and .exe
26 	// suffix even if the user just types the plain program name.
27 	// We modify argv[0] to make it nicer to read.
28 
29 	// Strip the leading path.
30 	char *p = argv[0] + strlen(argv[0]);
31 	while (argv[0] < p && p[-1] != '/' && p[-1] != '\\')
32 		--p;
33 
34 	argv[0] = p;
35 
36 	// Strip the .exe suffix.
37 	p = strrchr(p, '.');
38 	if (p != NULL)
39 		*p = '\0';
40 
41 	// Make it lowercase.
42 	for (p = argv[0]; *p != '\0'; ++p)
43 		if (*p >= 'A' && *p <= 'Z')
44 			*p = *p - 'A' + 'a';
45 #endif
46 
47 	progname = argv[0];
48 	return;
49 }
50