summaryrefslogtreecommitdiff
path: root/system/pkgconf/personality.patch
blob: e7ff9ad9a70fb0e03e5f4dc8d148ea4c1b7711a0 (plain) (blame)
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
From d926e75b345a6948e4e976828d3331cd7f0db660 Mon Sep 17 00:00:00 2001
From: Emil Renner Berthing <esmil@mailme.dk>
Date: Wed, 6 Mar 2019 16:03:32 +0100
Subject: [PATCH] main: fix personalities when argv[0] contains path

---
 cli/main.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/cli/main.c b/cli/main.c
index b52cc85..caa2525 100644
--- a/cli/main.c
+++ b/cli/main.c
@@ -740,21 +740,30 @@ dump_personality(const pkgconf_cross_personality_t *p)
 static pkgconf_cross_personality_t *
 deduce_personality(char *argv[])
 {
-	char *workbuf = strdup(argv[0]), *i;
-	pkgconf_cross_personality_t *out = pkgconf_cross_personality_default(), *deduced;
+	const char *argv0 = argv[0];
+	char *i, *prefix;
+	pkgconf_cross_personality_t *out;
+
+	i = strrchr(argv0, '/');
+	if (i != NULL)
+		argv0 = i + 1;
+
+#if defined(_WIN32) || defined(_WIN64)
+	i = strrchr(argv0, '\\');
+	if (i != NULL)
+		argv0 = i + 1;
+#endif
 
-	i = strstr(workbuf, "-pkg");
+	i = strstr(argv0, "-pkg");
 	if (i == NULL)
-		goto finish;
-
-	*i = 0;
+		return pkgconf_cross_personality_default();
 
-	deduced = pkgconf_cross_personality_find(workbuf);
-	if (deduced != NULL)
-		out = deduced;
+	prefix = strndup(argv0, i - argv0);
+	out = pkgconf_cross_personality_find(prefix);
+	free(prefix);
+	if (out == NULL)
+		return pkgconf_cross_personality_default();
 
-finish:
-	free(workbuf);
 	return out;
 }