Fixing clitar.c so that tar to stdout works correctly.
[samba.git] / source3 / client / clitar.c
index 3169c37962f8d54bdff45800c6e2c6876e533a6d..1f453da492af6c189b177c070a70f74e9bf69ba7 100644 (file)
@@ -3,6 +3,7 @@
    Version 1.9.
    Tar Extensions
    Copyright (C) Ricky Poulten 1995-1998
+   Copyright (C) Richard Sharpe 1998
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -19,8 +20,7 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 /* The following changes developed by Richard Sharpe for Canon Information
-   Systems Research Australia (CISRA) are Copyright (C) 1998 by CISRA and are 
-   made available under the terms of the GPL as listed above:
+   Systems Research Australia (CISRA)
 
    1. Restore can now restore files with long file names
    2. Save now saves directory information so that we can restore 
@@ -38,9 +38,6 @@
 
 #include "includes.h"
 #include "clitar.h"
-#ifdef HAVE_REGEX_H
-#include <regex.h>
-#endif
 
 typedef struct file_info_struct file_info2;
 
@@ -100,15 +97,17 @@ BOOL tar_re_search=False;
 regex_t *preg;
 #endif
 /* Dump files with System attribute */
-BOOL tar_system=False;
+BOOL tar_system=True;
 /* Dump files with Hidden attribute */
 BOOL tar_hidden=True;
 /* Be noisy - make a catalogue */
 BOOL tar_noisy=True;
+BOOL tar_real_noisy=True;
 
 char tar_type='\0';
 static char **cliplist=NULL;
 static int clipn=0;
+static BOOL must_free_cliplist = False;
 
 extern file_info def_finfo;
 extern BOOL lowercase;
@@ -125,49 +124,51 @@ int tarhandle;
 
 static void writetarheader(int f,  char *aname, int size, time_t mtime,
                           char *amode, unsigned char ftype);
-static void do_atar();
-static void do_tar();
+static void do_atar(char *rname,char *lname,file_info *finfo1);
+static void do_tar(file_info *finfo);
 static void oct_it(long value, int ndgs, char *p);
 static void fixtarname(char *tptr, char *fp, int l);
 static int dotarbuf(int f, char *b, int n);
-static void dozerobuf();
-static void dotareof();
-static void initarbuf();
-static int do_setrattr();
+static void dozerobuf(int f, int n);
+static void dotareof(int f);
+static void initarbuf(void);
+static int do_setrattr(char *fname, int attr, int setit);
 
 /* restore functions */
-static long readtarheader();
+static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix);
 static long unoct(char *p, int ndgs);
-static void do_tarput();
-static void unfixtarname(char *tptr, char *fp, int l);
+static void do_tarput(void);
+static void unfixtarname(char *tptr, char *fp, int l, BOOL first);
 
 /*
  * tar specific utitlities
  */
 
+#if 0 /* Removed to get around gcc 'defined but not used' error. */
+
 /*
  * Stack routines, push_dir, pop_dir, top_dir_name
  */
 
-BOOL push_dir(stack *dir_stack, file_info2 *dir)
+static BOOL push_dir(stack *tar_dir_stack, file_info2 *dir)
 {
-  dir -> next = dir_stack -> top;
+  dir -> next = tar_dir_stack -> top;
   dir -> prev = NULL;
-  dir_stack -> items++;
-  dir_stack -> top = dir;
+  tar_dir_stack -> items++;
+  tar_dir_stack -> top = dir;
   return(True);
 
 }
 
-file_info2 *pop_dir(stack *dir_stack)
+static file_info2 *pop_dir(stack *tar_dir_stack)
 {
   file_info2 *ptr;
   
-  ptr = dir_stack -> top;
-  if (dir_stack -> top != NULL) {
+  ptr = tar_dir_stack -> top;
+  if (tar_dir_stack -> top != NULL) {
 
-    dir_stack -> top = dir_stack -> top -> next;
-    dir_stack -> items--;
+    tar_dir_stack -> top = tar_dir_stack -> top -> next;
+    tar_dir_stack -> items--;
 
   }
 
@@ -175,20 +176,26 @@ file_info2 *pop_dir(stack *dir_stack)
 
 }
 
-char *top_dir_name(stack *dir_stack)
+static char *top_dir_name(stack *tar_dir_stack)
 {
 
-  return(dir_stack -> top != NULL?dir_stack -> top -> name:NULL);
+  return(tar_dir_stack -> top != NULL?tar_dir_stack -> top -> name:NULL);
 
 }
 
-BOOL sub_dir(char *dir1, char *dir2)
+static BOOL sub_dir(char *dir1, char *dir2)
 {
 
+  return(True);
+
 }
 
-/* Create a string of size size+1 (for the null) */
-char * string_create_s(int size)
+#endif /* Removed to get around gcc 'defined but not used' error. */
+
+/*******************************************************************
+Create  a string of size size+1 (for the null)
+*******************************************************************/
+static char *string_create_s(int size)
 {
   char *tmp;
 
@@ -229,7 +236,7 @@ static void writetarheader(int f,  char *aname, int size, time_t mtime,
          }
          writetarheader(f, "/./@LongLink", l+1, 0, "     0 \0", 'L');
          memset(b, 0, l+TBLOCK+100);
-         fixtarname(b, aname, l+1);
+         fixtarname(b, aname, l);
          i = strlen(b)+1;
          DEBUG(5, ("File name in tar file: %s, size=%i, \n", b, strlen(b)));
          dotarbuf(f, b, TBLOCK*(((i-1)/TBLOCK)+1));
@@ -245,7 +252,7 @@ static void writetarheader(int f,  char *aname, int size, time_t mtime,
   /* write out a "standard" tar format header */
 
   hb.dbuf.name[NAMSIZ-1]='\0';
-  strcpy(hb.dbuf.mode, amode);
+  safe_strcpy(hb.dbuf.mode, amode, strlen(amode));
   oct_it(0L, 8, hb.dbuf.uid);
   oct_it(0L, 8, hb.dbuf.gid);
   oct_it((long) size, 13, hb.dbuf.size);
@@ -296,6 +303,12 @@ static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix)
   if (fchk != chk)
     {
       DEBUG(0, ("checksums don't match %d %d\n", fchk, chk));
+/*      for (i = 0; i < sizeof(hb -> dummy); i++) {
+       fprintf(stdout, "%2X ", hb -> dummy[i]);
+      }
+      fprintf(stdout, "\n");
+      fprintf(stdout, "%s\n", hb -> dummy);
+      fprintf(stdout, "Tarbuf = %X, hb = %X\n", (int)tarbuf, (int)hb);*/
       return -1;
     }
 
@@ -306,11 +319,11 @@ static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix)
 
   }
 
-  strcpy(finfo->name, prefix);
+  safe_strcpy(finfo->name, prefix, strlen(prefix) + strlen(hb -> dbuf.name) + 3);
 
   /* use l + 1 to do the null too; do prefix - prefcnt to zap leading slash */
   unfixtarname(finfo->name + strlen(prefix), hb->dbuf.name,
-              strlen(hb->dbuf.name) + 1);
+              strlen(hb->dbuf.name) + 1, True);
 
 /* can't handle some links at present */
   if ((hb->dbuf.linkflag != '0') && (hb -> dbuf.linkflag != '5')) {
@@ -556,7 +569,7 @@ static int strslashcmp(char *s1, char *s2)
 do_setrtime, set time on a file or dir ...
 **********************************************************************/
 
-static int do_setrtime(char *fname, int mtime)
+static int do_setrtime(char *fname, int mtime, BOOL err_silent)
 {
   char *inbuf, *outbuf, *p;
   char *name;
@@ -571,9 +584,14 @@ static int do_setrtime(char *fname, int mtime)
 
   }
 
-  strcpy(name, fname);
-  strcpy(fname, "\\");
-  strcat(fname, name);
+  if (*fname != '\\')
+    safe_strcpy(name, "\\", strlen(fname) + 1);
+  else
+    safe_strcpy(name, "", strlen(fname) + 1);
+  safe_strcat(name, fname, strlen(fname) + 1);
+
+  if (fname[strlen(name) - 1] == '\\')
+    name[strlen(name) - 1] = '\0';
 
   inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
   outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
@@ -581,12 +599,13 @@ static int do_setrtime(char *fname, int mtime)
   if (!inbuf || !outbuf) {
 
     DEBUG(0, ("Could not allocate memory for inbuf or outbuf while changing time on: %s\n", fname));
+    free(name);
     return False;
 
   }
 
   memset(outbuf, 0, smb_size);
-  set_message(outbuf, 8, 4 + strlen(fname), True);
+  set_message(outbuf, 8, 4 + strlen(name), True);
   CVAL(outbuf, smb_com) = SMBsetatr;
   SSVAL(outbuf, smb_tid, cnum);
   cli_setup_pkt(outbuf);
@@ -596,7 +615,7 @@ static int do_setrtime(char *fname, int mtime)
 
   p = smb_buf(outbuf);
   *p++ = 4;
-  strcpy(p, fname);
+  safe_strcpy(p, name, strlen(name));
   p+= (strlen(fname)+1);
 
   *p++ = 4;
@@ -607,12 +626,15 @@ static int do_setrtime(char *fname, int mtime)
 
   if (CVAL(inbuf,smb_rcls) != 0)
     {
-      DEBUG(0,("%s setting attributes on file %s\n",
-           smb_errstr(inbuf), fname));
-      free(inbuf);free(outbuf);
+      if (!err_silent) {
+       DEBUG(0,("%s setting attributes on file %s\n",
+                smb_errstr(inbuf), fname));
+      }
+      free(name);free(inbuf);free(outbuf);
       return(False);
     }
 
+  free(name);
   free(inbuf);free(outbuf);
   return(True);
 
@@ -628,12 +650,19 @@ static int do_setrattr(char *fname, int attr, int setit)
    */
   char *inbuf,*outbuf;
   char *p;
-  pstring name;
+  char *name;
   int fattr;
 
-  strcpy(name,fname);
-  strcpy(fname,"\\");
-  strcat(fname,name);
+  name = (char *)malloc(strlen(fname) + 1 + 1);
+  if (name == NULL) {
+
+     DEBUG(0, ("Failed to allocate space in do_setrattr while setting time on file: %s", fname));
+     return False;
+
+  }
+
+  safe_strcpy(name, "\\", strlen(fname) + 1);
+  safe_strcat(name, fname, strlen(fname) + 1);
 
   inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
   outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
@@ -641,6 +670,7 @@ static int do_setrattr(char *fname, int attr, int setit)
   if (!inbuf || !outbuf)
     {
       DEBUG(0,("out of memory\n"));
+      free(name);
       return False;
     }
 
@@ -654,8 +684,8 @@ static int do_setrattr(char *fname, int attr, int setit)
 
   p = smb_buf(outbuf);
   *p++ = 4;
-  strcpy(p,fname);
-  p += (strlen(fname)+1);
+  safe_strcpy(p,name, strlen(name));
+  p += (strlen(name)+1);
   
   *p++ = 4;
   *p++ = 0;
@@ -683,7 +713,7 @@ static int do_setrattr(char *fname, int attr, int setit)
 
   /* clear out buffer and start again */
   memset(outbuf,0,smb_size);
-  set_message(outbuf,8,4 + strlen(fname),True);
+  set_message(outbuf,8,4 + strlen(name),True);
   CVAL(outbuf,smb_com) = SMBsetatr;
   SSVAL(outbuf,smb_tid,cnum);
   cli_setup_pkt(outbuf);
@@ -692,8 +722,8 @@ static int do_setrattr(char *fname, int attr, int setit)
   
   p = smb_buf(outbuf);
   *p++ = 4;      
-  strcpy(p,fname);
-  p += (strlen(fname)+1);
+  safe_strcpy(p,name, strlen(name));
+  p += (strlen(name)+1);
   
   *p++ = 4;
   *p++ = 0;
@@ -704,11 +734,12 @@ static int do_setrattr(char *fname, int attr, int setit)
   if (CVAL(inbuf,smb_rcls) != 0)
     {
       DEBUG(0,("%s setting attributes on file %s\n",
-           smb_errstr(inbuf), fname));
-      free(inbuf);free(outbuf);
+           smb_errstr(inbuf), name));
+      free(name);free(inbuf);free(outbuf);
       return(False);
     }
 
+  free(name);
   free(inbuf);free(outbuf);
   return(True);
 }
@@ -733,7 +764,7 @@ static BOOL smbcreat(file_info2 finfo, int *fnum, char *inbuf, char *outbuf)
   
   p = smb_buf(outbuf);
   *p++ = 4;      
-  strcpy(p,finfo.name);
+  safe_strcpy(p,finfo.name, strlen(finfo.name));
   
   send_smb(Client,outbuf);
   client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
@@ -841,7 +872,7 @@ static BOOL smbchkpath(char *fname, char *inbuf, char *outbuf)
 
   p = smb_buf(outbuf);
   *p++ = 4;
-  strcpy(p,fname);
+  safe_strcpy(p,fname, strlen(fname));
 
   send_smb(Client,outbuf);
   client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
@@ -868,7 +899,7 @@ static BOOL smbmkdir(char *fname, char *inbuf, char *outbuf)
   
   p = smb_buf(outbuf);
   *p++ = 4;      
-  strcpy(p,fname);
+  safe_strcpy(p,fname, strlen(fname));
   
   send_smb(Client,outbuf);
   client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
@@ -910,7 +941,7 @@ static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
 
   /* fname copied to ffname so can strtok */
 
-  strcpy(ffname, fname);
+  safe_strcpy(ffname, fname, strlen(fname));
 
   /* do a `basename' on ffname, so don't try and make file name directory */
   if ((basehack=strrchr(ffname, '\\')) == NULL)
@@ -922,7 +953,7 @@ static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
 
   while (p)
     {
-      strcat(partpath, p);
+      safe_strcat(partpath, p, strlen(fname) + 1);
 
       if (!smbchkpath(partpath, inbuf, outbuf)) {
        if (!smbmkdir(partpath, inbuf, outbuf))
@@ -935,7 +966,7 @@ static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
 
       }
 
-      strcat(partpath, "\\");
+      safe_strcat(partpath, "\\", strlen(fname) + 1);
       p = strtok(NULL,"/\\");
     }
 
@@ -970,7 +1001,7 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
   uint32 nread=0;
   char *p, ftype;
   char *inbuf,*outbuf;
-  file_info finfo;
+  file_info2 finfo;
   BOOL close_done = False;
   BOOL shallitime=True;
   BOOL ignore_close_error = False;
@@ -982,10 +1013,25 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
 
   ftype = '0'; /* An ordinary file ... */
 
-  if (finfo1) 
-    finfo = *finfo1;
-  else
-    finfo = def_finfo;
+  if (finfo1) {
+    finfo.size  = finfo1 -> size;
+    finfo.mode  = finfo1 -> mode;
+    finfo.uid   = finfo1 -> uid;
+    finfo.gid   = finfo1 -> gid;
+    finfo.mtime = finfo1 -> mtime;
+    finfo.atime = finfo1 -> atime;
+    finfo.ctime = finfo1 -> ctime;
+  }
+  else {
+    finfo.size  = def_finfo.size;
+    finfo.mode  = def_finfo.mode;
+    finfo.uid   = def_finfo.uid;
+    finfo.gid   = def_finfo.gid;
+    finfo.mtime = def_finfo.mtime;
+    finfo.atime = def_finfo.atime;
+    finfo.ctime = def_finfo.ctime;
+  }
+
 
   inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
   outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
@@ -1011,7 +1057,7 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
   SSVAL(outbuf,smb_vwv8,1);
 
   p = smb_buf(outbuf);
-  strcpy(p,rname);
+  safe_strcpy(p, rname, strlen(rname));
   p = skip_string(p,1);
 
   dos_clean_name(rname);
@@ -1049,7 +1095,16 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       return;
     }
 
-  strcpy(finfo.name,rname);
+  finfo.name = string_create_s(strlen(rname));
+  if (finfo.name == NULL) {
+
+    DEBUG(0, ("Unable to allocate space for finfo.name in do_atar\n"));
+    free(inbuf); free(outbuf);
+    return;
+
+  }
+
+  safe_strcpy(finfo.name,rname, strlen(rname));
   if (!finfo1)
     {
       finfo.mode = SVAL(inbuf,smb_vwv3);
@@ -1355,17 +1410,17 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       get_total_time_ms += this_time;
       get_total_size += finfo.size;
 
-      /* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
-      DEBUG(3,("(%g kb/s) (average %g kb/s)\n",
-              finfo.size / MAX(0.001, (1.024*this_time)),
-              get_total_size / MAX(0.001, (1.024*get_total_time_ms))));
       if (tar_noisy)
        {
-         printf("%10d (%7.1f kb/s) %s\n",
+         DEBUG(0, ("%10d (%7.1f kb/s) %s\n",
               finfo.size, finfo.size / MAX(0.001, (1.024*this_time)),
-               finfo.name);
+               finfo.name));
        }
 
+      /* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
+      DEBUG(3,("(%g kb/s) (average %g kb/s)\n",
+              finfo.size / MAX(0.001, (1.024*this_time)),
+              get_total_size / MAX(0.001, (1.024*get_total_time_ms))));
     }
   
   free(inbuf);free(outbuf);
@@ -1385,11 +1440,13 @@ static void do_tar(file_info *finfo)
   if (!tar_excl && clipn) {
     pstring exclaim;
 
-    strcpy(exclaim, cur_dir);
+    DEBUG(5, ("Excl: strlen(cur_dir) = %i\n", strlen(cur_dir)));
+
+    safe_strcpy(exclaim, cur_dir, sizeof(pstring));
     *(exclaim+strlen(exclaim)-1)='\0';
 
-    strcat(exclaim, "\\");
-    strcat(exclaim, finfo->name);
+    safe_strcat(exclaim, "\\", sizeof(pstring));
+    safe_strcat(exclaim, finfo->name, sizeof(exclaim));
 
     DEBUG(5, ("...tar_re_search: %d\n", tar_re_search));
 
@@ -1419,27 +1476,34 @@ static void do_tar(file_info *finfo)
          return;
        }
 
-      strcpy(saved_curdir,cur_dir);
+      safe_strcpy(saved_curdir, cur_dir, sizeof(saved_curdir));
+
+      DEBUG(5, ("Sizeof(cur_dir)=%i, strlen(cur_dir)=%i, strlen(finfo->name)=%i\nname=%s,cur_dir=%s\n", sizeof(cur_dir), strlen(cur_dir), strlen(finfo->name), finfo->name, cur_dir));
 
-      strcat(cur_dir,finfo->name);
-      strcat(cur_dir,"\\");
+      safe_strcat(cur_dir,finfo->name, sizeof(cur_dir));
+      safe_strcat(cur_dir,"\\", sizeof(cur_dir));
 
       DEBUG(5, ("Writing a dir, Name = %s\n", cur_dir));
 
       /* write a tar directory, don't bother with mode - just set it to
        * 40755 */
       writetarheader(tarhandle, cur_dir, 0, finfo->mtime, "040755 \0", '5');
+      if (tar_noisy) {
+
+          DEBUG(0, ("                directory %s\n", cur_dir));
+
+      }
       ntarf++;  /* Make sure we have a file on there */
-      strcpy(mtar_mask,cur_dir);
-      strcat(mtar_mask,"*");
+      safe_strcpy(mtar_mask,cur_dir, sizeof(pstring));
+      safe_strcat(mtar_mask,"*", sizeof(pstring));
       /*      do_dir((char *)inbuf,(char *)outbuf,mtar_mask,attribute,do_tar,recurse,True); */
-             strcpy(cur_dir,saved_curdir);
+      safe_strcpy(cur_dir,saved_curdir, sizeof(pstring));
       free(inbuf);free(outbuf);
     }
   else
     {
-      strcpy(rname,cur_dir);
-      strcat(rname,finfo->name);
+      safe_strcpy(rname,cur_dir, sizeof(pstring));
+      safe_strcat(rname,finfo->name, sizeof(pstring));
       do_atar(rname,finfo->name,finfo);
     }
 }
@@ -1447,14 +1511,24 @@ static void do_tar(file_info *finfo)
 /****************************************************************************
 Convert from UNIX to DOS file names
 ***************************************************************************/
-static void unfixtarname(char *tptr, char *fp, int l)
+static void unfixtarname(char *tptr, char *fp, int l, BOOL first)
 {
   /* remove '.' from start of file name, convert from unix /'s to
-   * dos \'s in path. Kill any absolute path names.
+   * dos \'s in path. Kill any absolute path names. But only if first!
    */
 
-  if (*fp == '.') fp++;
-  if (*fp == '\\' || *fp == '/') fp++;
+  DEBUG(5, ("firstb=%X, secondb=%X, len=%i\n", tptr, fp, l));
+
+  if (first) {
+    if (*fp == '.') {
+      fp++;
+      l--;
+    }
+    if (*fp == '\\' || *fp == '/') {
+      fp++;
+      l--;
+    }
+  }
 
   while (l > 0) {
     int skip;
@@ -1478,17 +1552,19 @@ static void unfixtarname(char *tptr, char *fp, int l)
   }
 }
 
+#if 0 /* Removed to get around gcc 'defined but not used' error. */
+
 /****************************************************************************
 Move to the next block in the buffer, which may mean read in another set of
 blocks.
 ****************************************************************************/
-int next_block(char *tarbuf, char *bufferp, int bufsiz)
+static int next_block(char *ltarbuf, char *bufferp, int bufsiz)
 {
   int bufread, total = 0;
 
-  if (bufferp >= (tarbuf + bufsiz)) {
+  if (bufferp >= (ltarbuf + bufsiz)) {
     
-    for (bufread = read(tarhandle, tarbuf, bufsiz); total += bufread; total < bufsiz) {
+    for (bufread = read(tarhandle, ltarbuf, bufsiz); total < bufsiz; total += bufread) {
 
       if (bufread <= 0) { /* An error, return false */
        return (total > 0 ? -2 : bufread);
@@ -1496,7 +1572,7 @@ int next_block(char *tarbuf, char *bufferp, int bufsiz)
 
     }
 
-    bufferp = tarbuf;
+    bufferp = ltarbuf;
 
   }
   else {
@@ -1505,34 +1581,42 @@ int next_block(char *tarbuf, char *bufferp, int bufsiz)
 
   }
 
+  return(0);
+
 }
 
-int skip_file()
+static int skip_file(int skip)
 {
 
+  return(0);
 }
 
-int get_file(file_info2 finfo)
+static int get_file(file_info2 finfo)
 {
 
+  return(0);
 
 }
 
-int get_dir(file_info2 finfo)
+static int get_dir(file_info2 finfo)
 {
 
+  return(0);
+
 }
 
-char * get_longfilename(file_info2 finfo)
+static char * get_longfilename(file_info2 finfo)
 {
 
+  return(NULL);
+
 }
 
 static char * bufferp;
 
-static void do_tarput2()
+static void do_tarput2(void)
 {
-  file_info2 finfo;
+  file_info2 finfo, *finfo2;
   struct timeval tp_start;
   char *inbuf, *outbuf, *longfilename = NULL;
   int skip = False;
@@ -1541,6 +1625,17 @@ static void do_tarput2()
 
   bufferp = tarbuf + tbufsiz;  /* init this to force first read */
 
+  if (push_dir(&dir_stack, &finfo)) {
+
+    finfo2 = pop_dir(&dir_stack);
+    inbuf = top_dir_name(&dir_stack); /* FIXME */
+    if (sub_dir(inbuf, finfo2 -> name)){
+
+      DEBUG(0, (""));
+
+    }
+  }
+
   inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
   outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
 
@@ -1615,7 +1710,7 @@ static void do_tarput2()
 
     if (skip) {
 
-      skip_file(finfo);
+      skip_file(finfo.size);
       continue;
 
     }
@@ -1637,7 +1732,7 @@ static void do_tarput2()
       break;
 
     default:
-      skip_file(finfo);  /* Don't handle these yet */
+      skip_file(finfo.size);  /* Don't handle these yet */
       break;
 
     }
@@ -1646,6 +1741,7 @@ static void do_tarput2()
 
 
 }
+#endif /* Removed to get around gcc 'defined but not used' error. */
 
 static void do_tarput()
 {
@@ -1676,7 +1772,7 @@ static void do_tarput()
 
   /* These should be the only reads in clitar.c */
   while ((bufread=read(tarhandle, tarbuf, tbufsiz))>0) {
-    char *bufferp, *endofbuffer;
+    char *buffer_p, *endofbuffer;
     int chunk;
 
     /* Code to handle a short read.
@@ -1698,13 +1794,13 @@ static void do_tarput()
       if (lread<=0) break;
     }
 
-    bufferp=tarbuf; 
+    buffer_p=tarbuf; 
     endofbuffer=tarbuf+bufread;
 
     if (tskip) {
       if (fsize<bufread) {
        tskip=False;
-       bufferp+=fsize;
+       buffer_p+=fsize;
        fsize=0;
       } else {
        if (fsize==bufread) tskip=False;
@@ -1719,10 +1815,10 @@ static void do_tarput()
           int next_header = 1;  /* Want at least one header */
           while (next_header) 
             {  
-            if (bufferp >= endofbuffer) {
+            if (buffer_p >= endofbuffer) {
 
               bufread = read(tarhandle, tarbuf, tbufsiz);
-              bufferp = tarbuf;
+              buffer_p = tarbuf;
 
             }
             next_header = 0;    /* Don't want the next one ... */
@@ -1733,11 +1829,12 @@ static void do_tarput()
              finfo.name = NULL;
 
            }
-           switch (readtarheader((union hblock *) bufferp, &finfo, cur_dir))
+           DEBUG(5, ("Tarbuf=%X, buffer=%X, endofbuf=%X\n", tarbuf, buffer_p, endofbuffer));
+           switch (readtarheader((union hblock *) buffer_p, &finfo, cur_dir))
              {
              case -2:             /* something dodgy but not fatal about this */
                DEBUG(0, ("skipping %s...\n", finfo.name));
-               bufferp+=TBLOCK;   /* header - like a link */
+               buffer_p+=TBLOCK;   /* header - like a link */
                continue;
              case -1:
                DEBUG(0, ("abandoning restore, -1 from readtarheader\n"));
@@ -1774,13 +1871,13 @@ static void do_tarput()
             /* Check if a long-link. We do this before the clip checking
                because clip-checking should clip on real name - RJS */
 
-            if (((union hblock *)bufferp) -> dbuf.linkflag == 'L') {
+            if (((union hblock *)buffer_p) -> dbuf.linkflag == 'L') {
+             int file_len, first = 0; char *cp;
 
               /* Skip this header, but pick up length, get the name and 
                  fix the name and skip the name. Hmmm, what about end of
                  buffer??? */
 
-             DEBUG(5, ("Buffer size = %i\n", finfo.size + strlen(cur_dir) +1));
               longname = malloc(finfo.size + strlen(cur_dir) + 1);
               if (longname == NULL) {
 
@@ -1791,28 +1888,39 @@ static void do_tarput()
                  return;
               }
 
-              bufferp += TBLOCK;   /* Skip that longlink header */
+
+             bzero(longname, finfo.size + strlen(cur_dir) +1);
+
+              buffer_p += TBLOCK;   /* Skip that longlink header */
 
               /* This needs restructuring ... */
 
-             if (bufferp >= endofbuffer) {
+              safe_strcpy(longname, cur_dir, strlen(cur_dir) + 1); 
+             cp = longname + strlen(cur_dir);
+             file_len = finfo.size;
 
-               bufread = read(tarhandle, tarbuf, tbufsiz);
+             DEBUG(5, ("longname=%0X, cp=%0X, file_len=%i\n", longname, cp, file_len));
 
-               bufferp = tarbuf;
+             while (file_len > 0) {
 
-              }
+               if (buffer_p >= endofbuffer) {
+
+                 bufread = read(tarhandle, tarbuf, tbufsiz);
 
-              strncpy(longname, cur_dir, strlen(cur_dir) + 1); 
-              unfixtarname(longname+strlen(cur_dir), bufferp, finfo.size);
-             DEBUG(5, ("UnfixedName: %s, buffer: %s\n", longname, bufferp));
+                 buffer_p = tarbuf;
 
-              /* Next rounds up to next TBLOCK and takes care of us being right
-                 on a TBLOCK boundary */
+               }
+
+               unfixtarname(cp, buffer_p, file_len >= TBLOCK?TBLOCK:file_len, first == 0);
 
-              bufferp += (((finfo.size - 1)/TBLOCK)+1)*TBLOCK;
-              next_header = 1;  /* Force read of next header */
+               first++;              /* Not the first anymore */
+               cp = cp + strlen(cp); /* Move to end of string */
+               buffer_p += TBLOCK;
+               file_len -= TBLOCK;
+               DEBUG(5, ("cp=%0X, file_len=%i\n", cp, file_len));
+               next_header = 1;  /* Force read of next header */
 
+             }
             }
           }
          tskip=clipn
@@ -1823,18 +1931,18 @@ static void do_tarput()
                || (tar_re_search && mask_match(finfo.name, cliplist[0], True, False)));
 #endif
          if (tskip) {
-           bufferp+=TBLOCK;
+           buffer_p+=TBLOCK;
            if (finfo.mode & aDIR)
              continue;
            else if ((fsize=finfo.size) % TBLOCK) {
              fsize+=TBLOCK-(fsize%TBLOCK);
            }
-           if (fsize<endofbuffer-bufferp) {
-             bufferp+=fsize;
+           if (fsize<endofbuffer-buffer_p) {
+             buffer_p+=fsize;
              fsize=0;
              continue;
            } else {
-             fsize-=endofbuffer-bufferp;
+             fsize-=endofbuffer-buffer_p;
              break;
            }
          }
@@ -1845,6 +1953,8 @@ static void do_tarput()
            {
 
              DEBUG(5, ("Creating directory: %s\n", finfo.name));
+             DEBUG(0, ("restore tar dir  %s of size %d bytes\n",
+                       finfo.name, finfo.size));
 
              if (!ensurepath(finfo.name, inbuf, outbuf))
                {
@@ -1856,17 +1966,19 @@ static void do_tarput()
                {
                  /* Now we update the creation date ... */
 
-                 DEBUG(0, ("Updating creation date on %s\n", finfo.name));
+                 DEBUG(5, ("Updating creation date on %s\n", finfo.name));
 
-                 if (!do_setrtime(finfo.name, finfo.mtime)) {
+                 if (!do_setrtime(finfo.name, finfo.mtime, True)) {
 
-                    DEBUG(0, ("Could not set time on file: %s\n", finfo.name));
-                    return;
+                   if (tar_real_noisy) {
+                     DEBUG(0, ("Could not set time on file: %s\n", finfo.name));
+                   }
+                    /*return;  - Win 95 does not like setting time on dirs */
 
                   }
 
                  ntarf++;
-                 bufferp+=TBLOCK;
+                 buffer_p+=TBLOCK;
                  continue;
                }
            }
@@ -1882,7 +1994,7 @@ static void do_tarput()
            }
 
          DEBUG(0 ,("restore tar file %s of size %d bytes\n",
-                  finfo.name,finfo.size));
+                  finfo.name, finfo.size));
 
          /*          if (!finfo.size) {
            if (!smbshut(finfo, fnum, inbuf, outbuf)){
@@ -1893,13 +2005,13 @@ static void do_tarput()
            } */
 
          nread=0;
-         if ((bufferp+=TBLOCK) >= endofbuffer) break;    
+         if ((buffer_p+=TBLOCK) >= endofbuffer) break;   
        } /* if (!fsize) */
        
       /* write out the file in chunk sized chunks - don't
        * go past end of buffer though */
-      chunk=(fsize-nread < endofbuffer - bufferp)
-       ? fsize - nread : endofbuffer - bufferp;
+      chunk=(fsize-nread < endofbuffer - buffer_p)
+       ? fsize - nread : endofbuffer - buffer_p;
       
       while (chunk > 0) {
        int minichunk=MIN(chunk, max_xmit-200);
@@ -1909,7 +2021,7 @@ static void do_tarput()
                      nread, /* offset low */
                      0, /* offset high - not implemented */
                      fsize-nread, /* left - only hint to server */
-                     bufferp,
+                     buffer_p,
                      inbuf,
                      outbuf))
          {
@@ -1919,7 +2031,7 @@ static void do_tarput()
          }
        DEBUG(5, ("chunk writing fname=%s fnum=%d nread=%d minichunk=%d chunk=%d size=%d\n", finfo.name, fnum, nread, minichunk, chunk, fsize));
        
-       bufferp+=minichunk; nread+=minichunk;
+       buffer_p+=minichunk; nread+=minichunk;
        chunk-=minichunk;
       }
 
@@ -1931,14 +2043,14 @@ static void do_tarput()
              free(inbuf);free(outbuf);
              return;
            }
-         if (fsize % TBLOCK) bufferp+=TBLOCK - (fsize % TBLOCK);
-         DEBUG(5, ("bufferp is now %d (psn=%d)\n",
-                   (long) bufferp, (long)(bufferp - tarbuf)));
+         if (fsize % TBLOCK) buffer_p+=TBLOCK - (fsize % TBLOCK);
+         DEBUG(5, ("buffer_p is now %d (psn=%d)\n",
+                   (long) buffer_p, (long)(buffer_p - tarbuf)));
          ntarf++;
          fsize=0;
 
        }
-    } while (bufferp < endofbuffer);
+    } while (buffer_p < endofbuffer);
   }
 
   DEBUG(0, ("premature eof on tar file ?\n"));
@@ -2035,8 +2147,8 @@ void cmd_setmode(char *dum_in, char *dum_out)
       return;
     }
 
-  strcpy(fname, cur_dir);
-  strcat(fname, buf);
+  safe_strcpy(fname, cur_dir, sizeof(pstring));
+  safe_strcat(fname, buf, sizeof(pstring));
 
   while (next_token(NULL,buf,NULL)) {
     q=buf;
@@ -2103,7 +2215,12 @@ int process_tar(char *inbuf, char *outbuf)
   initarbuf();
   switch(tar_type) {
   case 'x':
+
+#if 0
+    do_tarput2();
+#else
     do_tarput();
+#endif
     free(tarbuf);
     close(tarhandle);
     break;
@@ -2123,29 +2240,29 @@ int process_tar(char *inbuf, char *outbuf)
        if (strrchr(cliplist[i], '\\')) {
          pstring saved_dir;
          
-         strcpy(saved_dir, cur_dir);
+         safe_strcpy(saved_dir, cur_dir, sizeof(pstring));
          
          if (*cliplist[i]=='\\') {
-           strcpy(tarmac, cliplist[i]);
+           safe_strcpy(tarmac, cliplist[i], sizeof(pstring));
          } else {
-           strcpy(tarmac, cur_dir);
-           strcat(tarmac, cliplist[i]);
+           safe_strcpy(tarmac, cur_dir, sizeof(pstring));
+           safe_strcat(tarmac, cliplist[i], sizeof(pstring));
          }
-         strcpy(cur_dir, tarmac);
+         safe_strcpy(cur_dir, tarmac, sizeof(pstring));
          *(strrchr(cur_dir, '\\')+1)='\0';
 
          do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse, True);
-         strcpy(cur_dir,saved_dir);
+         safe_strcpy(cur_dir,saved_dir, sizeof(pstring));
        } else {
-         strcpy(tarmac, cur_dir);
-         strcat(tarmac, cliplist[i]);
+         safe_strcpy(tarmac, cur_dir, sizeof(pstring));
+         safe_strcat(tarmac, cliplist[i], sizeof(pstring));
          do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse, True);
        }
       }
     } else {
       pstring mask;
-      strcpy(mask,cur_dir);
-      strcat(mask,"\\*");
+      safe_strcpy(mask,cur_dir, sizeof(pstring));
+      safe_strcat(mask,"\\*", sizeof(pstring));
       do_dir((char *)inbuf,(char *)outbuf,mask,attribute,do_tar,recurse, True);
     }
     
@@ -2158,6 +2275,17 @@ int process_tar(char *inbuf, char *outbuf)
     break;
   }
 
+  if (must_free_cliplist) {
+    int i;
+    for (i = 0; i < clipn; ++i) {
+      free(cliplist[i]);
+    }
+    free(cliplist);
+    cliplist = NULL;
+    clipn = 0;
+    must_free_cliplist = False;
+  }
+
   return(0);
 }
 
@@ -2183,6 +2311,114 @@ int clipfind(char **aret, int ret, char *tok)
   return 0;
 }
 
+/****************************************************************************
+Read list of files to include from the file and initialize cliplist
+accordingly.
+***************************************************************************/
+static int read_inclusion_file(char *filename)
+{
+  FILE *inclusion = NULL;
+  char buf[MAXPATHLEN + 1];
+  char *inclusion_buffer = NULL;
+  int inclusion_buffer_size = 0;
+  int inclusion_buffer_sofar = 0;
+  char *p;
+  char *tmpstr;
+  int i;
+  int error = 0;
+
+  clipn = 0;
+  buf[MAXPATHLEN] = '\0'; /* guarantee null-termination */
+  if ((inclusion = fopen(filename, "r")) == NULL) {
+    /* XXX It would be better to include a reason for failure, but without
+     * autoconf, it's hard to use strerror, sys_errlist, etc.
+     */
+    DEBUG(0,("Unable to open inclusion file %s\n", filename));
+    return 0;
+  }
+
+  while ((! error) && (fgets(buf, sizeof(buf)-1, inclusion))) {
+    if (inclusion_buffer == NULL) {
+      inclusion_buffer_size = 1024;
+      if ((inclusion_buffer = malloc(inclusion_buffer_size)) == NULL) {
+       DEBUG(0,("failure allocating buffer to read inclusion file\n"));
+       error = 1;
+       break;
+      }
+    }
+    
+    if (buf[strlen(buf)-1] == '\n') {
+      buf[strlen(buf)-1] = '\0';
+    }
+    
+    if ((strlen(buf) + 1 + inclusion_buffer_sofar) >= inclusion_buffer_size) {
+      inclusion_buffer_size *= 2;
+      inclusion_buffer = Realloc(inclusion_buffer,inclusion_buffer_size);
+      if (! inclusion_buffer) {
+       DEBUG(0,("failure enlarging inclusion buffer to %d bytes\n",
+                inclusion_buffer_size));
+       error = 1;
+       break;
+      }
+    }
+    
+    safe_strcpy(inclusion_buffer + inclusion_buffer_sofar, buf, inclusion_buffer_size - inclusion_buffer_sofar);
+    inclusion_buffer_sofar += strlen(buf) + 1;
+    clipn++;
+  }
+  fclose(inclusion);
+
+  if (! error) {
+    /* Allocate an array of clipn + 1 char*'s for cliplist */
+    cliplist = malloc((clipn + 1) * sizeof(char *));
+    if (cliplist == NULL) {
+      DEBUG(0,("failure allocating memory for cliplist\n"));
+      error = 1;
+    } else {
+      cliplist[clipn] = NULL;
+      p = inclusion_buffer;
+      for (i = 0; (! error) && (i < clipn); i++) {
+       /* set current item to NULL so array will be null-terminated even if
+        * malloc fails below. */
+       cliplist[i] = NULL;
+       if ((tmpstr = (char *)malloc(strlen(p)+1)) == NULL) {
+         DEBUG(0, ("Could not allocate space for a cliplist item, # %i\n", i));
+         error = 1;
+       } else {
+         unfixtarname(tmpstr, p, strlen(p) + 1, True);
+         cliplist[i] = tmpstr;
+         if ((p = strchr(p, '\000')) == NULL) {
+           DEBUG(0,("INTERNAL ERROR: inclusion_buffer is of unexpected contents.\n"));
+           abort();
+         }
+       }
+       ++p;
+      }
+      must_free_cliplist = True;
+    }
+  }
+
+  if (inclusion_buffer) {
+    free(inclusion_buffer);
+  }
+  if (error) {
+    if (cliplist) {
+      char **pp;
+      /* We know cliplist is always null-terminated */
+      for (pp = cliplist; *pp; ++pp) {
+        free(*pp);
+      }
+      free(cliplist);
+      cliplist = NULL;
+      must_free_cliplist = False;
+    }
+    return 0;
+  }
+  
+  /* cliplist and its elements are freed at the end of process_tar. */
+  return 1;
+}
+
 /****************************************************************************
 Parse tar arguments. Sets tar_type, tar_excl, etc.
 ***************************************************************************/
@@ -2243,18 +2479,25 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
       break;
     case 'I':
       if (tar_clipfl) {
-       DEBUG(0,("Only one of I,X must be specified\n"));
+       DEBUG(0,("Only one of I,X,F must be specified\n"));
        return 0;
       }
       tar_clipfl='I';
       break;
     case 'X':
       if (tar_clipfl) {
-       DEBUG(0,("Only one of I,X must be specified\n"));
+       DEBUG(0,("Only one of I,X,F must be specified\n"));
        return 0;
       }
       tar_clipfl='X';
       break;
+    case 'F':
+      if (tar_clipfl) {
+       DEBUG(0,("Only one of I,X,F must be specified\n"));
+       return 0;
+      }
+      tar_clipfl='F';
+      break;
     case 'r':
       DEBUG(0, ("tar_re_search set\n"));
       tar_re_search = True;
@@ -2269,9 +2512,19 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
     return 0;
   }
 
+  /* tar_excl is true if cliplist lists files to be included.
+   * Both 'I' and 'F' mean include. */
   tar_excl=tar_clipfl!='X';
 
-  if (Optind+1<argc && !tar_re_search) { /* For backwards compatibility */
+  if (tar_clipfl=='F') {
+    if (argc-Optind-1 != 1) {
+      DEBUG(0,("Option F must be followed by exactly one filename.\n"));
+      return 0;
+    }
+    if (! read_inclusion_file(argv[Optind+1])) {
+      return 0;
+    }
+  } else if (Optind+1<argc && !tar_re_search) { /* For backwards compatibility */
     char *tmpstr;
     char **tmplist;
     int clipcount;
@@ -2297,13 +2550,14 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
              );
         return 0;
       }
-      unfixtarname(tmpstr, cliplist[clipcount], strlen(cliplist[clipcount]) + 1);
+      unfixtarname(tmpstr, cliplist[clipcount], strlen(cliplist[clipcount]) + 1, True);
       tmplist[clipcount] = tmpstr;
       DEBUG(5, ("Processed an item, %s\n", tmpstr));
 
       DEBUG(5, ("Cliplist is: %s\n", cliplist[0]));
     }
     cliplist = tmplist;
+    must_free_cliplist = True;
   }
 
   if (Optind+1<argc && tar_re_search) {  /* Doing regular expression seaches */