From c92bf99ee60c0b1fe1289fef2b4034126c7867a5 Mon Sep 17 00:00:00 2001 From: "Joshua M. Clulow" Date: Thu, 17 Jan 2013 10:12:47 -0800 Subject: [PATCH] 3474 tar should support -C on extract Reviewed by: Richard Lowe Reviewed by: Robert Mustacchi Approved by: Gordon Ross --- usr/src/cmd/tar/tar.c | 22 ++++++++++++++++++++++ usr/src/man/man1/tar.1 | 9 +++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/usr/src/cmd/tar/tar.c b/usr/src/cmd/tar/tar.c index 6cb084e9d0..9e2aef3bee 100644 --- a/usr/src/cmd/tar/tar.c +++ b/usr/src/cmd/tar/tar.c @@ -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 void detect_compress(void); 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 @@ main(int argc, char *argv[]) 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 @@ main(int argc, char *argv[]) } } 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")); diff --git a/usr/src/man/man1/tar.1 b/usr/src/man/man1/tar.1 index fd21f6c53c..0829a9c1c3 100644 --- a/usr/src/man/man1/tar.1 +++ b/usr/src/man/man1/tar.1 @@ -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 @@ tar \- create tape archives and add or extract files .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 @@ performs the \fBc\fR (create) or \fBr\fR (replace) operation on \fIfile\fR. Use 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 -- 2.11.4.GIT