Print this page
OS-1626 tar -C support on extract would be nice

@@ -559,10 +559,11 @@
 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;
 static  int     uflag;
 static  int     errflag;

@@ -1136,12 +1137,26 @@
                                 } else {
                                         Iflag = 1;
                                         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) {
                         mt = dup(0);
                         ++bflag;
                         /* try to recover from short reads when reading stdin */
                         ++Bflag;

@@ -1156,10 +1171,16 @@
                                 pid = uncompress_file();
                                 wait_pid(pid);
                         }
                 }
                 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"));
 
                         doxtract(argv);