changeset 13922:90e496e52838

3474 tar should support -C on extract Reviewed by: Richard Lowe <richlowe@richlowe.net> Reviewed by: Robert Mustacchi <rm@joyent.com> Approved by: Gordon Ross <gwr@nexenta.com>
author Joshua M. Clulow <jmc@joyent.com>
date Thu, 17 Jan 2013 10:12:47 -0800
parents 9d721847e469
children a443a77ab02c
files usr/src/cmd/tar/tar.c usr/src/man/man1/tar.1
diffstat 2 files changed, 29 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/tar/tar.c	Wed Jan 16 12:36:06 2013 -0800
+++ b/usr/src/cmd/tar/tar.c	Thu Jan 17 10:12:47 2013 -0800
@@ -21,6 +21,7 @@
 /*
  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright 2012 Milan Jurik. All rights reserved.
+ * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  */
 
 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
@@ -561,6 +562,7 @@
 static	struct stat stbuf;
 
 static	char	*myname;
+static	char	*xtract_chdir = NULL;
 static	int	checkflag = 0;
 static	int	Xflag, Fflag, iflag, hflag, Bflag, Iflag;
 static	int	rflag, xflag, vflag, tflag, mt, svmt, cflag, mflag, pflag;
@@ -1138,6 +1140,20 @@
 					argv[argc] = argv[argc+2];
 					build_table(include_tbl, argv[++argc]);
 				}
+			} else if (strcmp(argv[argc], "-C") == 0) {
+				if (!argv[argc+1]) {
+					(void) fprintf(stderr, gettext("tar: "
+					    "missing argument for -C flag\n"));
+					done(2);
+				} else if (xtract_chdir != NULL) {
+					(void) fprintf(stderr, gettext("tar: "
+					    "extract should have only one -C "
+					    "flag\n"));
+					done(2);
+				} else {
+					argv[argc] = argv[argc+2];
+					xtract_chdir = argv[++argc];
+				}
 			}
 		}
 		if (strcmp(usefile, "-") == 0) {
@@ -1158,6 +1174,12 @@
 			}
 		}
 		if (xflag) {
+			if (xtract_chdir != NULL) {
+				if (tar_chdir(xtract_chdir) < 0) {
+					vperror(1, gettext("can't change "
+					    "directories to %s"), xtract_chdir);
+				}
+			}
 			if (Aflag && vflag)
 				(void) printf(gettext(
 				    "Suppressing absolute pathnames.\n"));
--- a/usr/src/man/man1/tar.1	Wed Jan 16 12:36:06 2013 -0800
+++ b/usr/src/man/man1/tar.1	Thu Jan 17 10:12:47 2013 -0800
@@ -2,6 +2,7 @@
 .\" Copyright 1989 AT&T
 .\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved
 .\" Copyright 2012 Milan Jurik. All rights reserved.
+.\" Copyright (c) 2013, Joyent, Inc. All rights reserved.
 .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
 .\" http://www.opengroup.org/bookstore/.
@@ -10,7 +11,7 @@
 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
-.TH TAR 1 "May 9, 2012"
+.TH TAR 1 "Jan 16, 2013"
 .SH NAME
 tar \- create tape archives and add or extract files
 .SH SYNOPSIS
@@ -43,7 +44,7 @@
 .LP
 .nf
 \fBtar\fR x[BeFhilmnopqTvw@/[0-7]][fk][X...][j|J|z|Z] [\fItarfile\fR] [\fIsize\fR]
-     [\fIexclude-file\fR]... [\fIfile\fR]...
+     [\fIexclude-file\fR]... [\(miC \fIdirectory\fR] [\fIfile\fR]...
 .fi
 
 .SH DESCRIPTION
@@ -81,6 +82,10 @@
 short relative path names for \fIfile\fR. If \fIfile\fR is "\fB\&.\fR", archive
 all files in \fIdirectory\fR. This operand enables archiving files from
 multiple directories not related by a close common parent.
+.sp
+This option may also be passed once to \fBx\fR (extract).  In this case the
+program will \fBchdir\fR to \fIdirectory\fR after opening the archive, but
+before extracting its contents.
 .RE
 
 .sp