Print this page
3474 tar should support -C on extract
Reviewed by: Robert Mustacchi <rm@joyent.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/tar/tar.c
          +++ new/usr/src/cmd/tar/tar.c
↓ open down ↓ 13 lines elided ↑ open up ↑
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
  23   23   * Copyright 2012 Milan Jurik. All rights reserved.
       24 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  24   25   */
  25   26  
  26   27  /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T     */
  27   28  /*        All Rights Reserved   */
  28   29  
  29   30  /*      Copyright (c) 1987, 1988 Microsoft Corporation  */
  30   31  /*        All Rights Reserved   */
  31   32  
  32   33  /*
  33   34   * Portions of this source code were derived from Berkeley 4.3 BSD
↓ open down ↓ 520 lines elided ↑ open up ↑
 554  555  static char *gz_suffix(void);
 555  556  static char *xz_suffix(void);
 556  557  static char *add_suffix();
 557  558  static void wait_pid(pid_t);
 558  559  static void verify_compress_opt(const char *t);
 559  560  static void detect_compress(void);
 560  561  
 561  562  static  struct stat stbuf;
 562  563  
 563  564  static  char    *myname;
      565 +static  char    *xtract_chdir = NULL;
 564  566  static  int     checkflag = 0;
 565  567  static  int     Xflag, Fflag, iflag, hflag, Bflag, Iflag;
 566  568  static  int     rflag, xflag, vflag, tflag, mt, svmt, cflag, mflag, pflag;
 567  569  static  int     uflag;
 568  570  static  int     errflag;
 569  571  static  int     oflag;
 570  572  static  int     bflag, Aflag;
 571  573  static  int     Pflag;                  /* POSIX conformant archive */
 572  574  static  int     Eflag;                  /* Allow files greater than 8GB */
 573  575  static  int     atflag;                 /* traverse extended attributes */
↓ open down ↓ 557 lines elided ↑ open up ↑
1131 1133                          if (strcmp(argv[argc], "-I") == 0) {
1132 1134                                  if (!argv[argc+1]) {
1133 1135                                          (void) fprintf(stderr, gettext(
1134 1136                                          "tar: missing argument for -I flag\n"));
1135 1137                                          done(2);
1136 1138                                  } else {
1137 1139                                          Iflag = 1;
1138 1140                                          argv[argc] = argv[argc+2];
1139 1141                                          build_table(include_tbl, argv[++argc]);
1140 1142                                  }
     1143 +                        } else if (strcmp(argv[argc], "-C") == 0) {
     1144 +                                if (!argv[argc+1]) {
     1145 +                                        (void) fprintf(stderr, gettext("tar: "
     1146 +                                            "missing argument for -C flag\n"));
     1147 +                                        done(2);
     1148 +                                } else if (xtract_chdir != NULL) {
     1149 +                                        (void) fprintf(stderr, gettext("tar: "
     1150 +                                            "extract should have only one -C "
     1151 +                                            "flag\n"));
     1152 +                                        done(2);
     1153 +                                } else {
     1154 +                                        argv[argc] = argv[argc+2];
     1155 +                                        xtract_chdir = argv[++argc];
     1156 +                                }
1141 1157                          }
1142 1158                  }
1143 1159                  if (strcmp(usefile, "-") == 0) {
1144 1160                          mt = dup(0);
1145 1161                          ++bflag;
1146 1162                          /* try to recover from short reads when reading stdin */
1147 1163                          ++Bflag;
1148 1164                  } else if ((mt = open(usefile, 0)) < 0)
1149 1165                          vperror(1, "%s", usefile);
1150 1166  
1151 1167                  /* Decompress if the file is compressed */
1152 1168  
1153 1169                  if (strcmp(usefile, "-") != 0) {
1154 1170                          check_compression();
1155 1171                          if (compress_opt != NULL) {
1156 1172                                  pid = uncompress_file();
1157 1173                                  wait_pid(pid);
1158 1174                          }
1159 1175                  }
1160 1176                  if (xflag) {
     1177 +                        if (xtract_chdir != NULL) {
     1178 +                                if (tar_chdir(xtract_chdir) < 0) {
     1179 +                                        vperror(1, gettext("can't change "
     1180 +                                            "directories to %s"), xtract_chdir);
     1181 +                                }
     1182 +                        }
1161 1183                          if (Aflag && vflag)
1162 1184                                  (void) printf(gettext(
1163 1185                                      "Suppressing absolute pathnames.\n"));
1164 1186  
1165 1187                          doxtract(argv);
1166 1188                  } else if (tflag)
1167 1189                          dotable(argv);
1168 1190          }
1169 1191          else
1170 1192                  usage();
↓ open down ↓ 8284 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX