tar: add support for --strip-components=N

function                                             old     new   delta
data_extract_all                                     882     995    +113
tar_longopts                                         290     309     +19
tar_main                                             938     942      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 136/0)             Total: 136 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/archival/tar.c b/archival/tar.c
index aa03ba9..566ba34 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -152,9 +152,12 @@
 # define FNM_LEADING_DIR 0
 #endif
 
-
-//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
-#define DBG(...) ((void)0)
+#if 0
+# define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
+#else
+# define DBG(...) ((void)0)
+#endif
+#define DBG_OPTION_PARSING 0
 
 
 #define block_buf bb_common_bufsiz1
@@ -855,6 +858,7 @@
 	IF_FEATURE_SEAMLESS_Z(   OPTBIT_COMPRESS    ,)
 	IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
+	OPTBIT_STRIP_COMPONENTS,
 	OPTBIT_NORECURSION,
 	IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND   ,)
 	OPTBIT_NUMERIC_OWNER,
@@ -879,12 +883,13 @@
 	OPT_GZIP         = IF_FEATURE_SEAMLESS_GZ(  (1 << OPTBIT_GZIP        )) + 0, // z
 	OPT_XZ           = IF_FEATURE_SEAMLESS_XZ(  (1 << OPTBIT_XZ          )) + 0, // J
 	OPT_COMPRESS     = IF_FEATURE_SEAMLESS_Z(   (1 << OPTBIT_COMPRESS    )) + 0, // Z
-	OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
-	OPT_NORECURSION     = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION    )) + 0, // no-recursion
-	OPT_2COMMAND        = IF_FEATURE_TAR_TO_COMMAND(  (1 << OPTBIT_2COMMAND       )) + 0, // to-command
-	OPT_NUMERIC_OWNER   = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER  )) + 0, // numeric-owner
-	OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
-	OPT_OVERWRITE       = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE      )) + 0, // overwrite
+	OPT_NOPRESERVE_TIME  = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
+	OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
+	OPT_NORECURSION      = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION    )) + 0, // no-recursion
+	OPT_2COMMAND         = IF_FEATURE_TAR_TO_COMMAND(  (1 << OPTBIT_2COMMAND       )) + 0, // to-command
+	OPT_NUMERIC_OWNER    = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER  )) + 0, // numeric-owner
+	OPT_NOPRESERVE_PERM  = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
+	OPT_OVERWRITE        = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE      )) + 0, // overwrite
 
 	OPT_ANY_COMPRESS = (OPT_BZIP2 | OPT_LZMA | OPT_GZIP | OPT_XZ | OPT_COMPRESS),
 };
@@ -928,6 +933,7 @@
 # if ENABLE_FEATURE_TAR_NOPRESERVE_TIME
 	"touch\0"               No_argument       "m"
 # endif
+	"strip-components\0"	Required_argument "\xf9"
 	"no-recursion\0"	No_argument       "\xfa"
 # if ENABLE_FEATURE_TAR_TO_COMMAND
 	"to-command\0"		Required_argument "\xfb"
@@ -973,11 +979,15 @@
 		"tt:vv:" // count -t,-v
 		IF_FEATURE_TAR_FROM("X::T::") // cumulative lists
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
-		"\xff::" // cumulative lists for --exclude
+		"\xff::" // --exclude=PATTERN is a list
 #endif
 		IF_FEATURE_TAR_CREATE("c:") "t:x:" // at least one of these is reqd
 		IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
-		IF_NOT_FEATURE_TAR_CREATE("t--x:x--t"); // mutually exclusive
+		IF_NOT_FEATURE_TAR_CREATE("t--x:x--t") // mutually exclusive
+#if ENABLE_FEATURE_TAR_LONG_OPTIONS
+		":\xf9+" // --strip-components=NUM
+#endif
+	;
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
 	applet_long_options = tar_longopts;
 #endif
@@ -1018,10 +1028,14 @@
 		IF_FEATURE_SEAMLESS_XZ(  "J"   )
 		IF_FEATURE_SEAMLESS_Z(   "Z"   )
 		IF_FEATURE_TAR_NOPRESERVE_TIME("m")
+		IF_FEATURE_TAR_LONG_OPTIONS("\xf9:") // --strip-components
 		, &base_dir // -C dir
 		, &tar_filename // -f filename
 		IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
 		IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X
+#if ENABLE_FEATURE_TAR_LONG_OPTIONS
+		, &tar_handle->tar__strip_components // --strip-components
+#endif
 		IF_FEATURE_TAR_TO_COMMAND(, &(tar_handle->tar__to_command)) // --to-command
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
 		, &excludes // --exclude
@@ -1029,11 +1043,49 @@
 		, &verboseFlag // combined count for -t and -v
 		, &verboseFlag // combined count for -t and -v
 		);
-	//bb_error_msg("opt:%08x", opt);
+#if DBG_OPTION_PARSING
+	bb_error_msg("opt: 0x%08x", opt);
+# define showopt(o) bb_error_msg("opt & %s(%x): %x", #o, o, opt & o);
+	showopt(OPT_TEST            );
+	showopt(OPT_EXTRACT         );
+	showopt(OPT_BASEDIR         );
+	showopt(OPT_TARNAME         );
+	showopt(OPT_2STDOUT         );
+	showopt(OPT_NOPRESERVE_OWNER);
+	showopt(OPT_P               );
+	showopt(OPT_VERBOSE         );
+	showopt(OPT_KEEP_OLD        );
+	showopt(OPT_CREATE          );
+	showopt(OPT_DEREFERENCE     );
+	showopt(OPT_BZIP2           );
+	showopt(OPT_LZMA            );
+	showopt(OPT_INCLUDE_FROM    );
+	showopt(OPT_EXCLUDE_FROM    );
+	showopt(OPT_GZIP            );
+	showopt(OPT_XZ              );
+	showopt(OPT_COMPRESS        );
+	showopt(OPT_NOPRESERVE_TIME );
+	showopt(OPT_STRIP_COMPONENTS);
+	showopt(OPT_NORECURSION     );
+	showopt(OPT_2COMMAND        );
+	showopt(OPT_NUMERIC_OWNER   );
+	showopt(OPT_NOPRESERVE_PERM );
+	showopt(OPT_OVERWRITE       );
+	showopt(OPT_ANY_COMPRESS    );
+	bb_error_msg("base_dir:'%s'", base_dir);
+	bb_error_msg("tar_filename:'%s'", tar_filename);
+	bb_error_msg("verboseFlag:%d", verboseFlag);
+	bb_error_msg("tar_handle->tar__to_command:'%s'", tar_handle->tar__to_command);
+	bb_error_msg("tar_handle->tar__strip_components:%u", tar_handle->tar__strip_components);
+	return 0;
+# undef showopt
+#endif
 	argv += optind;
 
-	if (verboseFlag) tar_handle->action_header = header_verbose_list;
-	if (verboseFlag == 1) tar_handle->action_header = header_list;
+	if (verboseFlag)
+		tar_handle->action_header = header_verbose_list;
+	if (verboseFlag == 1)
+		tar_handle->action_header = header_list;
 
 	if (opt & OPT_EXTRACT)
 		tar_handle->action_data = data_extract_all;