summaryrefslogtreecommitdiff
path: root/src/progname.c
blob: bebf7141705c3ee3997c392c6e2879f15b8c2003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright © 2006 Robert Millan
 * Copyright © 2010-2012 Guillem Jover <guillem@hadrons.org>
 * Copyright © 2018 Facebook, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Rejected in glibc
 * <https://sourceware.org/ml/libc-alpha/2006-03/msg00125.html>.
 */

#include <sys/param.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
#include <Windows.h>
#include <shlwapi.h>
#endif

#ifdef _WIN32
#define LIBBSD_IS_PATHNAME_SEPARATOR(c) ((c) == '/' || (c) == '\\')
#else
#define LIBBSD_IS_PATHNAME_SEPARATOR(c) ((c) == '/')
#endif

#ifdef HAVE___PROGNAME
extern const char *__progname;
#else
static const char *__progname = NULL;
#endif

const char *
getprogname(void)
{
#if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
	if (__progname == NULL)
		__progname = program_invocation_short_name;
#elif defined(HAVE_GETEXECNAME)
	/* getexecname(3) returns an absolute pathname, normalize it. */
	if (__progname == NULL)
		setprogname(getexecname());
#elif defined(_WIN32)
	if (__progname == NULL) {
		WCHAR *wpath = NULL;
		WCHAR *wname = NULL;
		WCHAR *wext = NULL;
		DWORD wpathsiz = MAX_PATH / 2;
		DWORD len, i;
		char *mbname = NULL;
		int mbnamesiz;

		/* Use the Unicode version of this function to support long
		 * paths. MAX_PATH isn't actually the maximum length of a
		 * path in this case. */
		do {
			WCHAR *wpathnew;

			wpathsiz *= 2;
			wpathsiz = MIN(wpathsiz, UNICODE_STRING_MAX_CHARS);
			wpathnew = reallocarray(wpath, wpathsiz, sizeof(*wpath));
			if (wpathnew == NULL)
				goto done;
			wpath = wpathnew;

			len = GetModuleFileNameW(NULL, wpath, wpathsiz);
			if (wpathsiz == UNICODE_STRING_MAX_CHARS)
				goto done;
		} while (wpathsiz == len);
		if (len == 0)
			goto done;

		/* GetModuleFileNameW() retrieves an absolute path. Locate the
		 * filename now to only convert necessary characters and save
		 * memory. */
		wname = wpath;
		for (i = len; i > 0; i--) {
			if (LIBBSD_IS_PATHNAME_SEPARATOR(wpath[i - 1])) {
				wname = wpath + i;
				break;
			}
		}

		/* Remove any trailing extension, such as '.exe', to make the
		 * behavior mach the non-Windows systems. */
		wext = PathFindExtensionW(wname);
		wext[0] = '\0';

		mbnamesiz = WideCharToMultiByte(CP_UTF8, 0, wname, -1, NULL,
		                                0, NULL, NULL);
		if (mbnamesiz == 0)
			goto done;
		mbname = malloc(mbnamesiz);
		if (mbname == NULL)
			goto done;
		mbnamesiz = WideCharToMultiByte(CP_UTF8, 0, wname, -1, mbname,
		                                mbnamesiz, NULL, NULL);
		if (mbnamesiz == 0)
			goto done;
		__progname = mbname;
		mbname = NULL;

done:
		free(wpath);
		free(mbname);
	}
#endif

	return __progname;
}

void
setprogname(const char *progname)
{
	size_t i;

	for (i = strlen(progname); i > 0; i--) {
		if (LIBBSD_IS_PATHNAME_SEPARATOR(progname[i - 1])) {
			__progname = progname + i;
			return;
		}
	}
	__progname = progname;
}