From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1121419-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 7A92F13997F
	for <garchives@archives.gentoo.org>; Sun, 10 Nov 2019 11:05:05 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 35E93E0A45;
	Sun, 10 Nov 2019 11:05:04 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 066BAE0A04
	for <gentoo-commits@lists.gentoo.org>; Sun, 10 Nov 2019 11:05:04 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id B6DE934CD08
	for <gentoo-commits@lists.gentoo.org>; Sun, 10 Nov 2019 00:59:01 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 0E43C8A2
	for <gentoo-commits@lists.gentoo.org>; Sun, 10 Nov 2019 00:59:00 +0000 (UTC)
From: "Anthony G. Basile" <blueness@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Anthony G. Basile" <blueness@gentoo.org>
Message-ID: <1573347525.5f4c2424f1b8c4741631c9eae2d5cd6bfbe8c7b9.blueness@gentoo>
Subject: [gentoo-commits] proj/elfix:elfix-0.9.x commit in: misc/install-xattr/
X-VCS-Repository: proj/elfix
X-VCS-Files: misc/install-xattr/install-xattr.c
X-VCS-Directories: misc/install-xattr/
X-VCS-Committer: blueness
X-VCS-Committer-Name: Anthony G. Basile
X-VCS-Revision: 5f4c2424f1b8c4741631c9eae2d5cd6bfbe8c7b9
X-VCS-Branch: elfix-0.9.x
Date: Sun, 10 Nov 2019 00:59:00 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: 98f77f52-ed45-4bed-8ae4-8b18501fb2b0
X-Archives-Hash: 9ff467c0adb6499454bde8df413751ea

commit:     5f4c2424f1b8c4741631c9eae2d5cd6bfbe8c7b9
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Sun Nov 10 00:52:31 2019 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Sun Nov 10 00:58:45 2019 +0000
URL:        https://gitweb.gentoo.org/proj/elfix.git/commit/?id=5f4c2424

install-xattr: fix mangling of parameter order by getopt_long()

See: https://bugs.gentoo.org/699550

Signed-off-by: Anthony G. Basile <blueness <AT> gentoo.org>

 misc/install-xattr/install-xattr.c | 53 +++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 21 deletions(-)

diff --git a/misc/install-xattr/install-xattr.c b/misc/install-xattr/install-xattr.c
index 1c39e69..66530f9 100644
--- a/misc/install-xattr/install-xattr.c
+++ b/misc/install-xattr/install-xattr.c
@@ -234,6 +234,8 @@ main(int argc, char* argv[])
 	int i;
 	int status;                    /* exit status of child "install" process                       */
 
+	char** argv_copy;              /* copy argv to avoid mangling by getopt_long()                 */
+
 	int opts_directory = 0;        /* if -d was given, then all arguments are directories          */
 	int opts_target_directory = 0; /* if -t was given, then we're installing to a target directory */
 	int target_is_directory = 0;   /* is the target a directory?                                   */
@@ -270,23 +272,32 @@ main(int argc, char* argv[])
 
 	opterr = 0; /* we skip many legitimate flags, so silence any warning */
 
-	while (1) {
-		static struct option long_options[] = {
-			{           "directory",       no_argument, 0, 'd'},
-			{    "target-directory", required_argument, 0, 't'},
-			{               "group", required_argument, 0, 'g'},
-			{                "mode", required_argument, 0, 'm'},
-			{               "owner", required_argument, 0, 'o'},
-			{              "suffix", required_argument, 0, 'S'},
-			{             "context", optional_argument, 0, 'Z'},
-			{              "backup", optional_argument, 0, 'b'},
-			{                "help",       no_argument, 0,  0 },
-			{                     0,                 0, 0,  0 }
-		};
+	static struct option long_options[] = {
+		{           "directory",       no_argument, 0, 'd'},
+		{    "target-directory", required_argument, 0, 't'},
+		{               "group", required_argument, 0, 'g'},
+		{                "mode", required_argument, 0, 'm'},
+		{               "owner", required_argument, 0, 'o'},
+		{              "suffix", required_argument, 0, 'S'},
+		{             "context", optional_argument, 0, 'Z'},
+		{              "backup", optional_argument, 0, 'b'},
+		{                "help",       no_argument, 0,  0 },
+		{                     0,                 0, 0,  0 }
+	};
+
+	/* We copy argv[] because getopts_long() mangles the order of the arguments.
+	 * We pass the original argv[] to install in the fork() while we use
+	 * argv_copy[] for the copying of the xattrs since optind assumes a reorder
+	 * parameter list.
+	 */
+	argv_copy = (char **)malloc(argc*sizeof(char *));
 
-		int option_index;
-		int c = getopt_long(argc, argv, "dt:g:m:o:S:Zb", long_options, &option_index);
+	for (i = 0; i < argc; i++)
+		argv_copy[i] = strdup(argv[i]);
 
+	while (1) {
+		int option_index;
+		int c = getopt_long(argc, argv_copy, "dt:g:m:o:S:Zb", long_options, &option_index);
  
 		if (c == -1)
 			break;
@@ -365,7 +376,7 @@ main(int argc, char* argv[])
 				goto done;
 
 			if (!opts_target_directory) {
-				target = argv[last];
+				target = argv_copy[last];
 				if (stat(target, &s) != 0) {
 					err(1, "failed to stat %s", target);
 					return EXIT_FAILURE;
@@ -384,8 +395,8 @@ main(int argc, char* argv[])
 					last++;
 
 				for (i = first; i < last; i++) {
-					if (stat(argv[i], &s) != 0) {
-						err(1, "failed to stat %s", argv[i]);
+					if (stat(argv_copy[i], &s) != 0) {
+						err(1, "failed to stat %s", argv_copy[i]);
 						return EXIT_FAILURE;
 					}
 					/* We reproduce install's behavior and skip
@@ -395,12 +406,12 @@ main(int argc, char* argv[])
 					if (S_ISDIR(s.st_mode))
 						continue;
 
-					path = path_join(target, basename(argv[i]));
-					copyxattr(argv[i], path);
+					path = path_join(target, basename(argv_copy[i]));
+					copyxattr(argv_copy[i], path);
 					free(path);
 				}
 			} else
-				copyxattr(argv[first], target);
+				copyxattr(argv_copy[first], target);
 
 
  done: