use double instead of SMB_BIG_UINT for dir_total and ttarf
[samba.git] / source3 / client / clitar.c
index 98364b77ed20b331d57d1ff53283628e579aabe8..a7586fcb5e033c52d7b086f00708d56ada2b5f34 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 
 #include "includes.h"
 #include "clitar.h"
 
+static int clipfind(char **aret, int ret, char *tok);
+
 typedef struct file_info_struct file_info2;
 
 struct file_info_struct
 {
-  int size;
-  int mode;
+  size_t size;
+  uint16 mode;
   int uid;
   int gid;
   /* These times are normally kept in GMT */
@@ -66,25 +68,25 @@ typedef struct
 
 stack dir_stack = {NULL, 0}; /* Want an empty stack */
 
-extern BOOL recurse;
-
 #define SEPARATORS " \t\n\r"
 extern int DEBUGLEVEL;
-extern int Client;
+extern struct cli_state *cli;
+extern FILE *dbf;
 
 /* These defines are for the do_setrattr routine, to indicate
  * setting and reseting of file attributes in the function call */
 #define ATTRSET 1
 #define ATTRRESET 0
 
-static int attribute = aDIR | aSYSTEM | aHIDDEN;
+static uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
 
 #ifndef CLIENT_TIMEOUT
 #define CLIENT_TIMEOUT (30*1000)
 #endif
 
-static char *tarbuf;
-static int tp, ntarf, tbufsiz, ttarf;
+static char *tarbuf, *buffer_p;
+static int tp, ntarf, tbufsiz;
+static SMB_BIG_UINT ttarf;
 /* Incremental mode */
 BOOL tar_inc=False;
 /* Reset archive bit */
@@ -96,20 +98,24 @@ BOOL tar_re_search=False;
 #ifdef HAVE_REGEX_H
 regex_t *preg;
 #endif
+/* Do not dump anything, just calculate sizes */
+BOOL dry_run=False;
 /* 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=False;  /* Don't want to be really noisy by default */
 
 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;
-extern int cnum;
+extern uint16 cnum;
 extern BOOL readbraw_supported;
 extern int max_xmit;
 extern pstring cur_dir;
@@ -130,68 +136,21 @@ static int dotarbuf(int f, char *b, int n);
 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(union hblock *hb, file_info2 *finfo, char *prefix);
 static long unoct(char *p, int ndgs);
 static void do_tarput(void);
-static void unfixtarname(char *tptr, char *fp, int l);
+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
- */
-
-static BOOL push_dir(stack *tar_dir_stack, file_info2 *dir)
-{
-  dir -> next = tar_dir_stack -> top;
-  dir -> prev = NULL;
-  tar_dir_stack -> items++;
-  tar_dir_stack -> top = dir;
-  return(True);
-
-}
-
-static file_info2 *pop_dir(stack *tar_dir_stack)
-{
-  file_info2 *ptr;
-  
-  ptr = tar_dir_stack -> top;
-  if (tar_dir_stack -> top != NULL) {
-
-    tar_dir_stack -> top = tar_dir_stack -> top -> next;
-    tar_dir_stack -> items--;
-
-  }
-
-  return ptr;
-
-}
-
-static char *top_dir_name(stack *tar_dir_stack)
-{
-
-  return(tar_dir_stack -> top != NULL?tar_dir_stack -> top -> name:NULL);
-
-}
-
-static BOOL sub_dir(char *dir1, char *dir2)
-{
-
-  return(True);
-
-}
-
-#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)
+/*******************************************************************
+Create  a string of size size+1 (for the null)
+*******************************************************************/
+static char *string_create_s(int size)
 {
   char *tmp;
 
@@ -232,7 +191,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));
@@ -248,7 +207,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);
@@ -293,12 +252,13 @@ static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix)
 
   fchk=unoct(hb->dbuf.chksum, sizeof(hb->dbuf.chksum));
 
-  DEBUG(5, ("checksum totals chk=%d fchk=%d chksum=%s\n",
+  DEBUG(5, ("checksum totals chk=%ld fchk=%ld chksum=%s\n",
            chk, fchk, hb->dbuf.chksum));
 
   if (fchk != chk)
     {
-      DEBUG(0, ("checksums don't match %d %d\n", fchk, chk));
+      DEBUG(0, ("checksums don't match %ld %ld\n", fchk, chk));
+      dump_data(5, (char *)hb - TBLOCK, TBLOCK *3);
       return -1;
     }
 
@@ -309,13 +269,13 @@ 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 */
+  /* can't handle some links at present */
   if ((hb->dbuf.linkflag != '0') && (hb -> dbuf.linkflag != '5')) {
     if (hb->dbuf.linkflag == 0) {
       DEBUG(6, ("Warning: NULL link flag (gnu tar archive ?) %s\n",
@@ -362,6 +322,9 @@ static int dotarbuf(int f, char *b, int n)
 {
   int fail=1, writ=n;
 
+  if (dry_run) {
+    return writ;
+  }
   /* This routine and the next one should be the only ones that do write()s */
   if (tp + n >= tbufsiz)
     {
@@ -398,6 +361,9 @@ static void dozerobuf(int f, int n)
    * used to round files to nearest block
    * and to do tar EOFs */
 
+  if (dry_run)
+    return;
+  
   if (n+tp >= tbufsiz)
     {
       memset(tarbuf+tp, 0, tbufsiz-tp);
@@ -415,7 +381,7 @@ static void dozerobuf(int f, int n)
 /****************************************************************************
 Malloc tape buffer
 ****************************************************************************/
-static void initarbuf()
+static void initarbuf(void)
 {
   /* initialize tar buffer */
   tbufsiz=blocksize*TBLOCK;
@@ -430,13 +396,16 @@ Write two zero blocks at end of file
 ****************************************************************************/
 static void dotareof(int f)
 {
-  struct stat stbuf;
+  SMB_STRUCT_STAT stbuf;
   /* Two zero blocks at end of file, write out full buffer */
 
+  if (dry_run)
+    return;
+
   (void) dozerobuf(f, TBLOCK);
   (void) dozerobuf(f, TBLOCK);
 
-  if (fstat(f, &stbuf) == -1)
+  if (sys_fstat(f, &stbuf) == -1)
     {
       DEBUG(0, ("Couldn't stat file handle\n"));
       return;
@@ -512,7 +481,7 @@ static long unoct(char *p, int ndgs)
 
   while (--ndgs)
     {
-      if (isdigit(*p))
+      if (isdigit((int)*p))
         value = (value << 3) | (long) (*p - '0');
 
       p++;
@@ -552,344 +521,11 @@ static int strslashcmp(char *s1, char *s2)
   return *s1-*s2;
 }
 
-/*
- * general smb utility functions
- */
-/**********************************************************************
-do_setrtime, set time on a file or dir ...
-**********************************************************************/
-
-static int do_setrtime(char *fname, int mtime)
-{
-  char *inbuf, *outbuf, *p;
-  char *name;
-
-  DEBUG(5, ("Setting time on: %s, fnlen=%i.\n", fname, strlen(fname)));
-
-  name = (char *)malloc(strlen(fname) + 1 + 1);
-  if (name == NULL) {
-
-     DEBUG(0, ("Failed to allocate space while setting time on file: %s", fname));
-     return False;
-
-  }
-
-  strcpy(name, fname);
-  strcpy(fname, "\\");
-  strcat(fname, name);
-
-  inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-
-  if (!inbuf || !outbuf) {
-
-    DEBUG(0, ("Could not allocate memory for inbuf or outbuf while changing time on: %s\n", fname));
-    return False;
-
-  }
-
-  memset(outbuf, 0, smb_size);
-  set_message(outbuf, 8, 4 + strlen(fname), True);
-  CVAL(outbuf, smb_com) = SMBsetatr;
-  SSVAL(outbuf, smb_tid, cnum);
-  cli_setup_pkt(outbuf);
-
-  SSVAL(outbuf, smb_vwv0, 0);
-  put_dos_date3(outbuf, smb_vwv1, mtime);
-
-  p = smb_buf(outbuf);
-  *p++ = 4;
-  strcpy(p, fname);
-  p+= (strlen(fname)+1);
-
-  *p++ = 4;
-  *p++ = 0;
-
-  send_smb(Client, outbuf);
-  client_receive_smb(Client, inbuf, CLIENT_TIMEOUT);
-
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("%s setting attributes on file %s\n",
-           smb_errstr(inbuf), fname));
-      free(inbuf);free(outbuf);
-      return(False);
-    }
-
-  free(inbuf);free(outbuf);
-  return(True);
-
-}
-
-/****************************************************************************
-Set DOS file attributes
-***************************************************************************/
-static int do_setrattr(char *fname, int attr, int setit)
-{
-  /*
-   * First get the existing attribs from existing file
-   */
-  char *inbuf,*outbuf;
-  char *p;
-  pstring name;
-  int fattr;
-
-  strcpy(name,fname);
-  strcpy(fname,"\\");
-  strcat(fname,name);
-
-  inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-
-  if (!inbuf || !outbuf)
-    {
-      DEBUG(0,("out of memory\n"));
-      return False;
-    }
-
-  /* send an smb getatr message */
-
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,0,2 + strlen(fname),True);
-  CVAL(outbuf,smb_com) = SMBgetatr;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-
-  p = smb_buf(outbuf);
-  *p++ = 4;
-  strcpy(p,fname);
-  p += (strlen(fname)+1);
-  
-  *p++ = 4;
-  *p++ = 0;
-
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-  if (CVAL(inbuf,smb_rcls) != 0)
-    DEBUG(5,("getatr: %s\n",smb_errstr(inbuf)));
-  else
-    {
-      DEBUG(5,("\nattr 0x%X  time %d  size %d\n",
-              (int)CVAL(inbuf,smb_vwv0),
-              SVAL(inbuf,smb_vwv1),
-              SVAL(inbuf,smb_vwv3)));
-    }
-
-  fattr=CVAL(inbuf,smb_vwv0);
-
-  /* combine found attributes with bits to be set or reset */
-
-  attr=setit ? (fattr | attr) : (fattr & ~attr);
-
-  /* now try and set attributes by sending smb reset message */
-
-  /* clear out buffer and start again */
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,8,4 + strlen(fname),True);
-  CVAL(outbuf,smb_com) = SMBsetatr;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-
-  SSVAL(outbuf,smb_vwv0,attr);
-  
-  p = smb_buf(outbuf);
-  *p++ = 4;      
-  strcpy(p,fname);
-  p += (strlen(fname)+1);
-  
-  *p++ = 4;
-  *p++ = 0;
-
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-  
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("%s setting attributes on file %s\n",
-           smb_errstr(inbuf), fname));
-      free(inbuf);free(outbuf);
-      return(False);
-    }
-
-  free(inbuf);free(outbuf);
-  return(True);
-}
-
-/****************************************************************************
-Create a file on a share
-***************************************************************************/
-static BOOL smbcreat(file_info2 finfo, int *fnum, char *inbuf, char *outbuf)
-{
-  char *p;
-  /* *must* be called with buffer ready malloc'ed */
-  /* open remote file */
-
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,3,2 + strlen(finfo.name),True);
-  CVAL(outbuf,smb_com) = SMBcreate;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-  
-  SSVAL(outbuf,smb_vwv0,finfo.mode);
-  put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
-  
-  p = smb_buf(outbuf);
-  *p++ = 4;      
-  strcpy(p,finfo.name);
-  
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-  
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),
-              finfo.name));
-      return 0;
-    }
-  
-  *fnum = SVAL(inbuf,smb_vwv0);
-  return True;
-}
-
-/****************************************************************************
-Write a file to a share
-***************************************************************************/
-static BOOL smbwrite(int fnum, int n, int low, int high, int left,
-                    char *bufferp, char *inbuf, char *outbuf)
-{
-  /* *must* be called with buffer ready malloc'ed */
-
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,5,n + 3,True);
-  
-  memcpy(smb_buf(outbuf)+3, bufferp, n);
-  
-  set_message(outbuf,5,n + 3, False);
-  CVAL(outbuf,smb_com) = SMBwrite;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-  
-  SSVAL(outbuf,smb_vwv0,fnum);
-  SSVAL(outbuf,smb_vwv1,n);
-  SIVAL(outbuf,smb_vwv2,low);
-  SSVAL(outbuf,smb_vwv4,left);
-  CVAL(smb_buf(outbuf),0) = 1;
-  SSVAL(smb_buf(outbuf),1,n);
-
-  send_smb(Client,outbuf); 
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-  
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("%s writing remote file\n",smb_errstr(inbuf)));
-      return False;
-    }
-  
-  if (n != SVAL(inbuf,smb_vwv0))
-    {
-      DEBUG(0,("Error: only wrote %d bytes out of %d\n",
-              SVAL(inbuf,smb_vwv0), n));
-      return False;
-    }
-
-  return True;
-}
-
-/****************************************************************************
-Close a file on a share
-***************************************************************************/
-static BOOL smbshut(file_info2 finfo, int fnum, char *inbuf, char *outbuf)
-{
-  /* *must* be called with buffer ready malloc'ed */
-
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,3,0,True);
-  CVAL(outbuf,smb_com) = SMBclose;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-  
-  SSVAL(outbuf,smb_vwv0,fnum);
-  put_dos_date3(outbuf,smb_vwv1,finfo.mtime);
-  
-  DEBUG(3,("Setting date to %s (0x%X)",
-          asctime(LocalTime(&finfo.mtime)),
-          finfo.mtime));
-  
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-  
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("%s closing remote file %s\n",smb_errstr(inbuf),
-              finfo.name));
-      return False;
-    }
-
-  return True;
-}
-
-/****************************************************************************
-Verify existence of path on share
-***************************************************************************/
-static BOOL smbchkpath(char *fname, char *inbuf, char *outbuf)
-{
-  char *p;
-
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,0,4 + strlen(fname),True);
-  CVAL(outbuf,smb_com) = SMBchkpth;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-
-  p = smb_buf(outbuf);
-  *p++ = 4;
-  strcpy(p,fname);
-
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-  DEBUG(5,("smbchkpath: %s\n",smb_errstr(inbuf)));
-
-  return(CVAL(inbuf,smb_rcls) == 0);
-}
-
-/****************************************************************************
-Make a directory on share
-***************************************************************************/
-static BOOL smbmkdir(char *fname, char *inbuf, char *outbuf)
-{
-  /* *must* be called with buffer ready malloc'ed */
-  char *p;
-
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,0,2 + strlen(fname),True);
-  
-  CVAL(outbuf,smb_com) = SMBmkdir;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-  
-  p = smb_buf(outbuf);
-  *p++ = 4;      
-  strcpy(p,fname);
-  
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-  
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      DEBUG(0,("%s making remote directory %s\n",
-              smb_errstr(inbuf),fname));
-      return(False);
-    }
-
-  return(True);
-}
 
 /****************************************************************************
 Ensure a remote path exists (make if necessary)
 ***************************************************************************/
-static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
+static BOOL ensurepath(char *fname)
 {
   /* *must* be called with buffer ready malloc'ed */
   /* ensures path exists */
@@ -913,7 +549,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)
@@ -925,10 +561,10 @@ 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))
+      if (!cli_chkpath(cli, partpath)) {
+       if (!cli_mkdir(cli, partpath))
          {
            DEBUG(0, ("Error mkdirhiering\n"));
            return False;
@@ -938,32 +574,48 @@ static BOOL ensurepath(char *fname, char *inbuf, char *outbuf)
 
       }
 
-      strcat(partpath, "\\");
+      safe_strcat(partpath, "\\", strlen(fname) + 1);
       p = strtok(NULL,"/\\");
     }
 
     return True;
 }
 
-int padit(char *buf, int bufsize, int padsize)
+static int padit(char *buf, int bufsize, int padsize)
 {
-  int berr= 0;
-  int bytestowrite;
+       int berr= 0;
+       int bytestowrite;
   
-  DEBUG(5, ("Padding with %d zeros\n", padsize));
-  memset(buf, 0, bufsize);
-  while( !berr && padsize > 0 ) {
-    bytestowrite= MIN(bufsize, padsize);
-    berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
-    padsize -= bytestowrite;
-  }
+       DEBUG(5, ("Padding with %d zeros\n", padsize));
+       memset(buf, 0, bufsize);
+       while( !berr && padsize > 0 ) {
+               bytestowrite= MIN(bufsize, padsize);
+               berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
+               padsize -= bytestowrite;
+       }
   
-  return berr;
+       return berr;
 }
 
-/*
- * smbclient functions
- */
+
+static void do_setrattr(char *name, uint16 attr, int set)
+{
+       uint16 oldattr;
+
+       if (!cli_getatr(cli, name, &oldattr, NULL, NULL)) return;
+
+       if (set == ATTRSET) {
+               attr |= oldattr;
+       } else {
+               attr = oldattr & ~attr;
+       }
+
+       if (!cli_setatr(cli, name, attr, 0)) {
+               DEBUG(1,("setatr failed: %s\n", cli_errstr(cli)));
+       }
+}
+
+
 /****************************************************************************
 append one remote file to the tar file
 ***************************************************************************/
@@ -971,13 +623,12 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
 {
   int fnum;
   uint32 nread=0;
-  char *p, ftype;
-  char *inbuf,*outbuf;
-  file_info finfo;
+  char ftype;
+  file_info2 finfo;
   BOOL close_done = False;
   BOOL shallitime=True;
-  BOOL ignore_close_error = False;
-  char *dataptr=NULL;
+  char data[65520];
+  int read_size = 65520;
   int datalen=0;
 
   struct timeval tp_start;
@@ -985,86 +636,63 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
 
   ftype = '0'; /* An ordinary file ... */
 
-  if (finfo1) 
-    finfo = *finfo1;
-  else
-    finfo = def_finfo;
-
-  inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
+  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;
+  }
 
-  if (!inbuf || !outbuf)
+  if (dry_run)
     {
-      DEBUG(0,("out of memory\n"));
+      DEBUG(3,("skipping file %s of size %d bytes\n",
+              finfo.name,
+              finfo.size));
+      shallitime=0;
+      ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
+      ntarf++;
       return;
     }
 
-  memset(outbuf,0,smb_size);
-  set_message(outbuf,15,1 + strlen(rname),True);
-
-  CVAL(outbuf,smb_com) = SMBopenX;
-  SSVAL(outbuf,smb_tid,cnum);
-  cli_setup_pkt(outbuf);
-
-  SSVAL(outbuf,smb_vwv0,0xFF);
-  SSVAL(outbuf,smb_vwv2,1);
-  SSVAL(outbuf,smb_vwv3,(DENY_NONE<<4));
-  SSVAL(outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
-  SSVAL(outbuf,smb_vwv5,aSYSTEM | aHIDDEN);
-  SSVAL(outbuf,smb_vwv8,1);
-
-  p = smb_buf(outbuf);
-  strcpy(p,rname);
-  p = skip_string(p,1);
+  fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
 
   dos_clean_name(rname);
 
-  /* do a chained openX with a readX? */  
-  if (finfo.size > 0)
-    {
-      SSVAL(outbuf,smb_vwv0,SMBreadX);
-      SSVAL(outbuf,smb_vwv1,PTR_DIFF(p,outbuf) - 4);
-      memset(p,0,200);
-      p -= smb_wct;
-      SSVAL(p,smb_wct,10);
-      SSVAL(p,smb_vwv0,0xFF);
-      SSVAL(p,smb_vwv5,MIN(max_xmit-500,finfo.size));
-      SSVAL(p,smb_vwv9,MIN(0xFFFF,finfo.size));
-      smb_setlen(outbuf,smb_len(outbuf)+11*2+1);  
-    }
-  
-  send_smb(Client,outbuf);
-  client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-
-  if (CVAL(inbuf,smb_rcls) != 0)
-    {
-      if (CVAL(inbuf,smb_rcls) == ERRSRV &&
-         SVAL(inbuf,smb_err) == ERRnoresource &&
-         cli_reopen_connection(inbuf,outbuf))
-       {
-         do_atar(rname,lname,finfo1);
-         free(inbuf);free(outbuf);
+  if (fnum == -1) {
+         DEBUG(0,("%s opening remote file %s (%s)\n",
+                  cli_errstr(cli),rname, cur_dir));
          return;
-       }
+  }
 
-      DEBUG(0,("%s opening remote file %s\n",smb_errstr(inbuf),rname));
-      free(inbuf);free(outbuf);
-      return;
-    }
+  finfo.name = string_create_s(strlen(rname));
+  if (finfo.name == NULL) {
+         DEBUG(0, ("Unable to allocate space for finfo.name in do_atar\n"));
+         return;
+  }
 
-  strcpy(finfo.name,rname);
-  if (!finfo1)
-    {
-      finfo.mode = SVAL(inbuf,smb_vwv3);
-      finfo.size = IVAL(inbuf,smb_vwv4);
-      finfo.mtime = make_unix_date3(inbuf+smb_vwv6);
-      finfo.atime = finfo.ctime = finfo.mtime;
-    }
+  safe_strcpy(finfo.name,rname, strlen(rname));
+  if (!finfo1) {
+         if (!cli_getattrE(cli, fnum, &finfo.mode, &finfo.size, NULL, &finfo.atime, &finfo.mtime)) {
+                 DEBUG(0, ("getattrE: %s\n", cli_errstr(cli)));
+                 return;
+         }
+         finfo.ctime = finfo.mtime;
+  }
 
   DEBUG(3,("file %s attrib 0x%X\n",finfo.name,finfo.mode));
 
-  fnum = SVAL(inbuf,smb_vwv2);
-
   if (tar_inc && !(finfo.mode & aARCH))
     {
       DEBUG(4, ("skipping %s - archive bit not set\n", finfo.name));
@@ -1082,18 +710,6 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
     }
   else
     {
-      if (SVAL(inbuf,smb_vwv0) == SMBreadX)
-       {
-         p = (inbuf+4+SVAL(inbuf,smb_vwv1)) - smb_wct;
-         datalen = SVAL(p,smb_vwv5);
-         dataptr = inbuf + 4 + SVAL(p,smb_vwv6);
-       }
-      else
-       {
-         dataptr = NULL;
-         datalen = 0;
-       }
-
       DEBUG(3,("getting file %s of size %d bytes as a tar file %s",
               finfo.name,
               finfo.size,
@@ -1101,217 +717,40 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       
       /* write a tar header, don't bother with mode - just set to 100644 */
       writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0", ftype);
-      
-      while (nread < finfo.size && !close_done)
-       {
-         int method = -1;
-         static BOOL can_chain_close=True;
 
-         p=NULL;
-         
-         DEBUG(3,("nread=%d\n",nread));
-         
-         /* 3 possible read types. readbraw if a large block is required.
-            readX + close if not much left and read if neither is supported */
-
-         /* we might have already read some data from a chained readX */
-         if (dataptr && datalen>0)
-           method=3;
-         
-         /* if we can finish now then readX+close */
-         if (method<0 && can_chain_close && (Protocol >= PROTOCOL_LANMAN1) && 
-             ((finfo.size - nread) < 
-              (max_xmit - (2*smb_size + 13*SIZEOFWORD + 300))))
-           method = 0;
-         
-         /* if we support readraw then use that */
-         if (method<0 && readbraw_supported)
-           method = 1;
-         
-         /* if we can then use readX */
-         if (method<0 && (Protocol >= PROTOCOL_LANMAN1))
-           method = 2;
-         
-         
-         switch (method)
-           {
-             /* use readX */
-           case 0:
-           case 2:
-             if (method == 0)
-               close_done = True;
-             
-             /* use readX + close */
-             memset(outbuf,0,smb_size);
-             set_message(outbuf,10,0,True);
-             CVAL(outbuf,smb_com) = SMBreadX;
-             SSVAL(outbuf,smb_tid,cnum);
-             cli_setup_pkt(outbuf);
-             
-             if (close_done)
-               {
-                 CVAL(outbuf,smb_vwv0) = SMBclose;
-                 SSVAL(outbuf,smb_vwv1,PTR_DIFF(smb_buf(outbuf),outbuf) - 4);
-               }
-             else
-               CVAL(outbuf,smb_vwv0) = 0xFF;         
+      while (nread < finfo.size && !close_done)        {
              
+             DEBUG(3,("nread=%d\n",nread));
              
-             SSVAL(outbuf,smb_vwv2,fnum);
-             SIVAL(outbuf,smb_vwv3,nread);
-             SSVAL(outbuf,smb_vwv5,MIN(max_xmit-200,finfo.size - nread));
-             SSVAL(outbuf,smb_vwv6,0);
-             SIVAL(outbuf,smb_vwv7,0);
-             SSVAL(outbuf,smb_vwv9,MIN(0xFFFF,finfo.size-nread));
+             datalen = cli_read(cli, fnum, data, nread, read_size);
              
-             if (close_done)
-               {
-                 p = smb_buf(outbuf);
-                 memset(p,0,9);
-                 
-                 CVAL(p,0) = 3;
-                 SSVAL(p,1,fnum);
-                 SIVALS(p,3,-1);
-                 
-                 /* now set the total packet length */
-                 smb_setlen(outbuf,smb_len(outbuf)+9);
-               }
-             
-             send_smb(Client,outbuf);
-             client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-             
-             if (CVAL(inbuf,smb_rcls) != 0)
-               {
-                 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
-                 break;
-               }
-             
-             if (close_done &&
-                 SVAL(inbuf,smb_vwv0) != SMBclose)
-               {
-                 /* NOTE: WfWg sometimes just ignores the chained
-                    command! This seems to break the spec? */
-                 DEBUG(3,("Rejected chained close?\n"));
-                 close_done = False;
-                 can_chain_close = False;
-                 ignore_close_error = True;
-               }
-             
-             datalen = SVAL(inbuf,smb_vwv5);
-             dataptr = inbuf + 4 + SVAL(inbuf,smb_vwv6);
-             break;
+             if (datalen == -1) {
+                     DEBUG(0,("Error reading file %s : %s\n", rname, cli_errstr(cli)));
+                     break;
+             }
              
+             /* add received bits of file to buffer - dotarbuf will
+              * write out in 512 byte intervals */
+             if (dotarbuf(tarhandle,data,datalen) != datalen) {
+                     DEBUG(0,("Error writing to tar file - %s\n", strerror(errno)));
+                     break;
+             }
              
-             /* use readbraw */
-           case 1:
-             {
-               static int readbraw_size = 0xFFFF;
-               
-               extern int Client;
-               memset(outbuf,0,smb_size);
-               set_message(outbuf,8,0,True);
-               CVAL(outbuf,smb_com) = SMBreadbraw;
-               SSVAL(outbuf,smb_tid,cnum);
-               cli_setup_pkt(outbuf);
-               SSVAL(outbuf,smb_vwv0,fnum);
-               SIVAL(outbuf,smb_vwv1,nread);
-               SSVAL(outbuf,smb_vwv3,MIN(finfo.size-nread,readbraw_size));
-               SSVAL(outbuf,smb_vwv4,0);
-               SIVALS(outbuf,smb_vwv5,-1);
-               send_smb(Client,outbuf);
-               
-               /* Now read the raw data into the buffer and write it */          
-               if(read_smb_length(Client,inbuf,0) == -1) {
-                 DEBUG(0,("Failed to read length in readbraw\n"));         
-                 exit(1);
-               }
-               
-               /* Even though this is not an smb message, smb_len
-                  returns the generic length of an smb message */
-               datalen = smb_len(inbuf);
-               
-               if (datalen == 0)
-                 {
-                   /* we got a readbraw error */
-                   DEBUG(4,("readbraw error - reducing size\n"));
-                   readbraw_size = (readbraw_size * 9) / 10;
-                   
-                   if (readbraw_size < max_xmit)
-                     {
-                       DEBUG(0,("disabling readbraw\n"));
-                       readbraw_supported = False;
-                     }
-
-                   dataptr=NULL;
-                   continue;
-                 }
-
-               if(read_data(Client,inbuf,datalen) != datalen) {
-                 DEBUG(0,("Failed to read data in readbraw\n"));
-                 exit(1);
-               }
-               dataptr = inbuf;
+             nread += datalen;
+             if (datalen == 0) {
+                     DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
+                     break;
              }
-             break;
 
-           case 3:
-             /* we've already read some data with a chained readX */
-             break;
-             
-           default:
-             /* use plain read */
-             memset(outbuf,0,smb_size);
-             set_message(outbuf,5,0,True);
-             CVAL(outbuf,smb_com) = SMBread;
-             SSVAL(outbuf,smb_tid,cnum);
-             cli_setup_pkt(outbuf);
-             
-             SSVAL(outbuf,smb_vwv0,fnum);
-             SSVAL(outbuf,smb_vwv1,MIN(max_xmit-200,finfo.size - nread));
-             SIVAL(outbuf,smb_vwv2,nread);
-             SSVAL(outbuf,smb_vwv4,finfo.size - nread);
-             
-             send_smb(Client,outbuf);
-             client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-             
-             if (CVAL(inbuf,smb_rcls) != 0)
-               {
-                 DEBUG(0,("Error %s reading remote file\n",smb_errstr(inbuf)));
-                 break;
-               }
-             
-             datalen = SVAL(inbuf,smb_vwv0);
-             dataptr = smb_buf(inbuf) + 3;
-             break;
-           }
-         
-         
-         /* add received bits of file to buffer - dotarbuf will
-          * write out in 512 byte intervals */
-         if (dotarbuf(tarhandle,dataptr,datalen) != datalen)
-           {
-             DEBUG(0,("Error writing to tar file - %s\n", strerror(errno)));
-             break;
-           }
-         
-         nread += datalen;
-         if (datalen == 0) 
-           {
-             DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
-             break;
-           }
-
-         dataptr=NULL;
-         datalen=0;
-       }
+             datalen=0;
+      }
 
-       /* pad tar file with zero's if we couldn't get entire file */
-       if (nread < finfo.size)
-        {
-          DEBUG(0, ("Didn't get entire file. size=%d, nread=%d\n", finfo.size, nread));
-          if (padit(inbuf, BUFFER_SIZE, finfo.size - nread))
-              DEBUG(0,("Error writing tar file - %s\n", strerror(errno)));
-        }
+      /* pad tar file with zero's if we couldn't get entire file */
+      if (nread < finfo.size) {
+             DEBUG(0, ("Didn't get entire file. size=%d, nread=%d\n", finfo.size, nread));
+             if (padit(data, sizeof(data), finfo.size - nread))
+                     DEBUG(0,("Error writing tar file - %s\n", strerror(errno)));
+      }
 
       /* round tar file to nearest block */
       if (finfo.size % TBLOCK)
@@ -1321,27 +760,7 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       ntarf++;
     }
   
-  if (!close_done)
-    {
-      memset(outbuf,0,smb_size);
-      set_message(outbuf,3,0,True);
-      CVAL(outbuf,smb_com) = SMBclose;
-      SSVAL(outbuf,smb_tid,cnum);
-      cli_setup_pkt(outbuf);
-      
-      SSVAL(outbuf,smb_vwv0,fnum);
-      SIVALS(outbuf,smb_vwv1,-1);
-      
-      send_smb(Client,outbuf);
-      client_receive_smb(Client,inbuf,CLIENT_TIMEOUT);
-      
-      if (!ignore_close_error && CVAL(inbuf,smb_rcls) != 0)
-       {
-         DEBUG(0,("Error %s closing remote file\n",smb_errstr(inbuf)));
-         free(inbuf);free(outbuf);
-         return;
-       }
-    }
+  cli_close(cli, fnum);
 
   if (shallitime)
     {
@@ -1349,7 +768,8 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       int this_time;
 
       /* if shallitime is true then we didn't skip */
-      if (tar_reset) (void) do_setrattr(finfo.name, aARCH, ATTRRESET);
+      if (tar_reset && !dry_run)
+       (void) do_setrattr(finfo.name, aARCH, ATTRRESET);
       
       GetTimeOfDay(&tp_end);
       this_time = 
@@ -1358,20 +778,18 @@ 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);
 }
 
 /****************************************************************************
@@ -1381,18 +799,20 @@ static void do_tar(file_info *finfo)
 {
   pstring rname;
 
-  if (strequal(finfo->name,".."))
+  if (strequal(finfo->name,"..") || strequal(finfo->name,"."))
     return;
 
   /* Is it on the exclude list ? */
   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));
 
@@ -1411,38 +831,33 @@ static void do_tar(file_info *finfo)
     {
       pstring saved_curdir;
       pstring mtar_mask;
-      char *inbuf,*outbuf;
-
-      inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-      outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
 
-      if (!inbuf || !outbuf)
-       {
-         DEBUG(0,("out of memory\n"));
-         return;
-       }
+      safe_strcpy(saved_curdir, cur_dir, sizeof(saved_curdir));
 
-      strcpy(saved_curdir,cur_dir);
+      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,"*");
-      /*      do_dir((char *)inbuf,(char *)outbuf,mtar_mask,attribute,do_tar,recurse,True); */
-             strcpy(cur_dir,saved_curdir);
-      free(inbuf);free(outbuf);
+      safe_strcpy(mtar_mask,cur_dir, sizeof(pstring));
+      safe_strcat(mtar_mask,"*", sizeof(pstring));
+      DEBUG(5, ("Doing list with mtar_mask: %s\n", mtar_mask));
+      do_list(mtar_mask, attribute, do_tar, False, True);
+      safe_strcpy(cur_dir,saved_curdir, sizeof(pstring));
     }
   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);
     }
 }
@@ -1450,14 +865,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=%lX, secondb=%lX, len=%i\n", (long)tptr, (long)fp, l));
+
+  if (first) {
+    if (*fp == '.') {
+      fp++;
+      l--;
+    }
+    if (*fp == '\\' || *fp == '/') {
+      fp++;
+      l--;
+    }
+  }
 
   while (l > 0) {
     int skip;
@@ -1481,18 +906,25 @@ 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.
+blocks. FIXME, we should allow more than one block to be skipped.
 ****************************************************************************/
-static int next_block(char *ltarbuf, char *bufferp, int bufsiz)
+static int next_block(char *ltarbuf, char **bufferp, int bufsiz)
 {
   int bufread, total = 0;
 
-  if (bufferp >= (ltarbuf + bufsiz)) {
-    
+  DEBUG(5, ("Advancing to next block: %0lx\n", (unsigned long)*bufferp));
+  *bufferp += TBLOCK;
+  total = TBLOCK;
+
+  if (*bufferp >= (ltarbuf + bufsiz)) {
+
+    DEBUG(5, ("Reading more data into ltarbuf ...\n"));
+
+    total = 0;
+
     for (bufread = read(tarhandle, ltarbuf, bufsiz); total < bufsiz; total += bufread) {
 
       if (bufread <= 0) { /* An error, return false */
@@ -1501,477 +933,345 @@ static int next_block(char *ltarbuf, char *bufferp, int bufsiz)
 
     }
 
-    bufferp = ltarbuf;
-
-  }
-  else {
+    DEBUG(5, ("Total bytes read ... %i\n", total));
 
-    bufferp += TBLOCK;
+    *bufferp = ltarbuf;
 
   }
 
-  return(0);
+  return(total);
 
 }
 
-static int skip_file(int skip)
+/* Skip a file, even if it includes a long file name? */
+static int skip_file(int skipsize)
 {
+  int dsize = skipsize;
 
-  return(0);
-}
-
-static int get_file(file_info2 finfo)
-{
+  DEBUG(5, ("Skiping file. Size = %i\n", skipsize));
 
-  return(0);
+  /* FIXME, we should skip more than one block at a time */
 
-}
+  while (dsize > 0) {
 
-static int get_dir(file_info2 finfo)
-{
+    if (next_block(tarbuf, &buffer_p, tbufsiz) <= 0) {
 
-  return(0);
+       DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
+       return(False);
 
-}
+    }
 
-static char * get_longfilename(file_info2 finfo)
-{
+    dsize -= TBLOCK;
 
-  return(NULL);
+  }
 
+  return(True);
 }
 
-static char * bufferp;
-
-static void do_tarput2(void)
+/* We get a file from the tar file and store it */
+static int get_file(file_info2 finfo)
 {
-  file_info2 finfo, *finfo2;
-  struct timeval tp_start;
-  char *inbuf, *outbuf, *longfilename = NULL;
-  int skip = False;
+  int fsize = finfo.size;
+  int fnum = -1, pos = 0, dsize = 0, rsize = 0, bpos = 0;
 
-  GetTimeOfDay(&tp_start);
+  DEBUG(5, ("get_file: file: %s, size %i\n", finfo.name, fsize));
+
+  if (ensurepath(finfo.name) && 
+      (fnum=cli_open(cli, finfo.name, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE)) == -1)
+    {
+      DEBUG(0, ("abandoning restore\n"));
+      return(False);
+    }
 
-  bufferp = tarbuf + tbufsiz;  /* init this to force first read */
+  /* read the blocks from the tar file and write to the remote file */
 
-  if (push_dir(&dir_stack, &finfo)) {
+  rsize = fsize;  /* This is how much to write */
 
-    finfo2 = pop_dir(&dir_stack);
-    inbuf = top_dir_name(&dir_stack); /* FIXME */
-    if (sub_dir(inbuf, finfo2 -> name)){
+  while (rsize > 0) {
 
-      DEBUG(0, (""));
+    /* We can only write up to the end of the buffer */
 
+    dsize = MIN(tbufsiz - (buffer_p - tarbuf) - bpos, 65520); /* Calculate the size to write */
+    dsize = MIN(dsize, rsize);  /* Should be only what is left */
+    DEBUG(5, ("writing %i bytes, bpos = %i ...\n", dsize, bpos));
+
+    if (cli_write(cli, fnum, 0, buffer_p + bpos, pos, dsize) != dsize) {
+           DEBUG(0, ("Error writing remote file\n"));
+           return 0;
     }
-  }
 
-  inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
+    rsize -= dsize;
+    pos += dsize;
 
-  if (!inbuf || !outbuf) {
+    /* Now figure out how much to move in the buffer */
 
-    DEBUG(0, ("Out of memory during allocate of inbuf and outbuf!\n"));
-    return;
+    /* FIXME, we should skip more than one block at a time */
 
-  }
+    /* First, skip any initial part of the part written that is left over */
+    /* from the end of the first TBLOCK                                   */
 
-  if (next_block(tarbuf, bufferp, tbufsiz) <= 0) {
+    if ((bpos) && ((bpos + dsize) >= TBLOCK)) {
 
-    DEBUG(0, ("Empty file or short tar file: %s\n", strerror(errno)));
+      dsize -= (TBLOCK - bpos);  /* Get rid of the end of the first block */
+      bpos = 0;
 
-  }
+      if (next_block(tarbuf, &buffer_p, tbufsiz) <=0) {  /* and skip the block */
+       DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
+       return False;
 
-  /* Now read through those files ... */
+      }
 
-  while (True) {
+    }
 
-    switch (readtarheader((union hblock *) bufferp, &finfo, cur_dir)) {
+    while (dsize >= TBLOCK) {
 
-    case -2:    /* Hmm, not good, but not fatal */
-      DEBUG(0, ("Skipping %s...\n", finfo.name));
-      if ((next_block(tarbuf, bufferp, tbufsiz) <= 0) &&
-          !skip_file(finfo.size)) {
+      if (next_block(tarbuf, &buffer_p, tbufsiz) <=0) {
 
-       DEBUG(0, ("Short file, bailing out...\n"));
-       free(inbuf); free(outbuf);
-       continue;
+       DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
+       return False;
 
       }
 
-      break;
+      dsize -= TBLOCK;
 
-    case -1:
-      DEBUG(0, ("abandoning restore, -1 from read tar header\n"));
-      free(inbuf); free(outbuf);
-      return;
+    }
 
-    case 0: /* chksum is zero - looks like an EOF */
-      DEBUG(0, ("total of %d tar files restored to share\n", ntarf));
-      free(inbuf); free(outbuf);
-      return;        /* Hmmm, bad here ... */
+    bpos = dsize;
 
-    default:
-      break;
+  }
 
-    }
+  /* Now close the file ... */
 
-    /* Now, do we have a long file name? */
+  if (!cli_close(cli, fnum)) {
+         DEBUG(0, ("Error closing remote file\n"));
+         return(False);
+  }
 
-    if (longfilename != NULL) {
-      if (strlen(longfilename) < sizeof(finfo.name)) { /* if we have space */
+  /* Now we update the creation date ... */
 
-       strncpy(finfo.name, longfilename, sizeof(finfo.name) - 1);
-       free(longfilename);
-       longfilename = NULL;
+  DEBUG(5, ("Updating creation date on %s\n", finfo.name));
 
-      }
-      else {
+  if (!cli_setatr(cli, finfo.name, finfo.mode, finfo.mtime)) {
+         if (tar_real_noisy) {
+                 DEBUG(0, ("Could not set time on file: %s\n", finfo.name));
+                 /*return(False); */ /* Ignore, as Win95 does not allow changes */
+         }
+  }
 
-       DEBUG(0, ("filename: %s too long, skipping\n", strlen(longfilename)));
-       skip = True;
+  ntarf++;
 
-      }
-    }
+  DEBUG(0, ("restore tar file %s of size %d bytes\n", finfo.name, finfo.size));
+  
+  return(True);
+}
 
-    /* Well, now we have a header, process the file ... */
+/* Create a directory.  We just ensure that the path exists and return as there
+   is no file associated with a directory 
+*/
+static int get_dir(file_info2 finfo)
+{
 
-    /* Should we skip the file?                         */
+  DEBUG(5, ("Creating directory: %s\n", finfo.name));
 
-    if (skip) {
+  if (!ensurepath(finfo.name)) {
 
-      skip_file(finfo.size);
-      continue;
+    DEBUG(0, ("Problems creating directory\n"));
+    return(False);
 
-    }
+  }
+  return(True);
 
-    /* We only get this far if we should process the file */
+}
+/* Get a file with a long file name ... first file has file name, next file 
+   has the data. We only want the long file name, as the loop in do_tarput
+   will deal with the rest.
+*/
+static char * get_longfilename(file_info2 finfo)
+{
+  int namesize = finfo.size + strlen(cur_dir) + 2;
+  char *longname = malloc(namesize);
+  int offset = 0, left = finfo.size;
+  BOOL first = True;
 
-    switch (((union hblock *)bufferp) -> dbuf.linkflag) {
+  DEBUG(5, ("Restoring a long file name: %s\n", finfo.name));
+  DEBUG(5, ("Len = %i\n", finfo.size));
 
-    case '0':  /* Should use symbolic names--FIXME */
-      get_file(finfo);
-      break;
+  if (longname == NULL) {
 
-    case '5':
-      get_dir(finfo);
-      break;
+    DEBUG(0, ("could not allocate buffer of size %d for longname\n", 
+             finfo.size + strlen(cur_dir) + 2));
+    return(NULL);
+  }
 
-    case 'L':
-      longfilename = get_longfilename(finfo);
-      break;
+  /* First, add cur_dir to the long file name */
 
-    default:
-      skip_file(finfo.size);  /* Don't handle these yet */
-      break;
+  if (strlen(cur_dir) > 0) {
+    strncpy(longname, cur_dir, namesize);
+    offset = strlen(cur_dir);
+  }
+
+  /* Loop through the blocks picking up the name */
+
+  while (left > 0) {
+
+    if (next_block(tarbuf, &buffer_p, tbufsiz) <= 0) {
+
+      DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
+      return(NULL);
 
     }
 
+    unfixtarname(longname + offset, buffer_p, MIN(TBLOCK, finfo.size), first--);
+    DEBUG(5, ("UnfixedName: %s, buffer: %s\n", longname, buffer_p));
+
+    offset += TBLOCK;
+    left -= TBLOCK;
+
   }
 
+  return(longname);
 
 }
-#endif /* Removed to get around gcc 'defined but not used' error. */
 
-static void do_tarput()
+static void do_tarput(void)
 {
   file_info2 finfo;
-  int nread=0, bufread;
-  char *inbuf,*outbuf, *longname = NULL; 
-  int fsize=0;
-  int fnum;
   struct timeval tp_start;
-  BOOL tskip=False;       /* We'll take each file as it comes */
-
-  finfo.name = NULL;      /* No name in here ... */
+  char *longfilename = NULL, linkflag;
+  int skip = False;
 
   GetTimeOfDay(&tp_start);
-  
-  inbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  outbuf = (char *)malloc(BUFFER_SIZE + SAFETY_MARGIN);
-  
-  if (!inbuf || !outbuf)
-    {
-      DEBUG(0,("out of memory\n"));
-      return;
-    }
-  
-  /*
-   * Must read in tbufsiz dollops
-   */
 
-  /* These should be the only reads in clitar.c */
-  while ((bufread=read(tarhandle, tarbuf, tbufsiz))>0) {
-    char *buffer_p, *endofbuffer;
-    int chunk;
+  DEBUG(5, ("RJS do_tarput called ...\n"));
 
-    /* Code to handle a short read.
-     * We always need a TBLOCK full of stuff
-     */
-    if (bufread % TBLOCK) {
-      int lchunk=TBLOCK-(bufread % TBLOCK);
-      int lread;
-
-      /* It's a shorty - a short read that is */
-      DEBUG(3, ("Short read, read %d so far (need %d)\n", bufread, lchunk));
+  buffer_p = tarbuf + tbufsiz;  /* init this to force first read */
 
-      while ((lread=read(tarhandle, tarbuf+bufread, lchunk))>0) {
-       bufread+=lread;
-       if (!(lchunk-=lread)) break;
-      }
+  /* Now read through those files ... */
 
-      /* If we've reached EOF then that must be a short file */
-      if (lread<=0) break;
-    }
+  while (True) {
 
-    buffer_p=tarbuf; 
-    endofbuffer=tarbuf+bufread;
+    /* Get us to the next block, or the first block first time around */
 
-    if (tskip) {
-      if (fsize<bufread) {
-       tskip=False;
-       buffer_p+=fsize;
-       fsize=0;
-      } else {
-       if (fsize==bufread) tskip=False;
-       fsize-=bufread;
-       continue;
-      }
-    }
+    if (next_block(tarbuf, &buffer_p, tbufsiz) <= 0) {
 
-    do {
-      if (!fsize)
-       {
-          int next_header = 1;  /* Want at least one header */
-          while (next_header) 
-            {  
-            if (buffer_p >= endofbuffer) {
-
-              bufread = read(tarhandle, tarbuf, tbufsiz);
-              buffer_p = tarbuf;
-
-            }
-            next_header = 0;    /* Don't want the next one ... */
-
-           if (finfo.name != NULL) { /* Free the space */
-
-             free(finfo.name);
-             finfo.name = NULL;
-
-           }
-           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));
-               buffer_p+=TBLOCK;   /* header - like a link */
-               continue;
-             case -1:
-               DEBUG(0, ("abandoning restore, -1 from readtarheader\n"));
-               free(inbuf); free(outbuf);
-               return;
-             case 0: /* chksum is zero - we assume that one all zero
-                      *header block will do for eof */
-               DEBUG(0,
-                     ("total of %d tar files restored to share\n", ntarf));
-               free(inbuf); free(outbuf);
-               return;
-             default:
-               break;
-             }
+      DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
 
-            /* If we have a longname left from the last time through, 
-               copy it into finfo.name and free it.
+      return;
 
-               The size of a pstring is the limiting factor on filenames
-               and directory names now. The total pathname length must be
-               less than sizeof(pstring) - 1, which is currently 1023. */
+    }
 
-            if (longname != NULL) {
+    DEBUG(5, ("Reading the next header ...\n"));
 
-             free(finfo.name);  /* Free the name in the finfo */
-             finfo.name = string_create_s(strlen(longname) + 2);
-              strncpy(finfo.name, longname, strlen(longname) + 1);
-             DEBUG(5, ("Long name = \"%s\", filename=\"%s\"\n", longname, finfo.name));
-              free(longname);
-              longname = NULL;
+    switch (readtarheader((union hblock *) buffer_p, &finfo, cur_dir)) {
 
-            }
+    case -2:    /* Hmm, not good, but not fatal */
+      DEBUG(0, ("Skipping %s...\n", finfo.name));
+      if ((next_block(tarbuf, &buffer_p, tbufsiz) <= 0) &&
+          !skip_file(finfo.size)) {
 
-            /* Check if a long-link. We do this before the clip checking
-               because clip-checking should clip on real name - RJS */
+       DEBUG(0, ("Short file, bailing out...\n"));
+       return;
 
-            if (((union hblock *)buffer_p) -> dbuf.linkflag == 'L') {
+      }
 
-              /* Skip this header, but pick up length, get the name and 
-                 fix the name and skip the name. Hmmm, what about end of
-                 buffer??? */
+      break;
 
-             DEBUG(5, ("Buffer size = %i\n", finfo.size + strlen(cur_dir) +1));
-              longname = malloc(finfo.size + strlen(cur_dir) + 1);
-              if (longname == NULL) {
+    case -1:
+      DEBUG(0, ("abandoning restore, -1 from read tar header\n"));
+      return;
 
-                 DEBUG(0, ("could not allocate buffer of size %d for longname\n",
-                          finfo.size + strlen(cur_dir) + 1)
-                      );
-                 free(inbuf); free(outbuf);
-                 return;
-              }
+    case 0: /* chksum is zero - looks like an EOF */
+      DEBUG(0, ("total of %d tar files restored to share\n", ntarf));
+      return;        /* Hmmm, bad here ... */
 
-              buffer_p += TBLOCK;   /* Skip that longlink header */
+    default: 
+      /* No action */
 
-              /* This needs restructuring ... */
+      break;
 
-             if (buffer_p >= endofbuffer) {
+    }
 
-               bufread = read(tarhandle, tarbuf, tbufsiz);
+    /* Now, do we have a long file name? */
 
-               buffer_p = tarbuf;
+    if (longfilename != NULL) {
 
-              }
+      free(finfo.name);   /* Free the space already allocated */
+      finfo.name = longfilename;
+      longfilename = NULL;
 
-              strncpy(longname, cur_dir, strlen(cur_dir) + 1); 
-              unfixtarname(longname+strlen(cur_dir), buffer_p, finfo.size);
-             DEBUG(5, ("UnfixedName: %s, buffer: %s\n", longname, buffer_p));
+    }
 
-              /* Next rounds up to next TBLOCK and takes care of us being right
-                 on a TBLOCK boundary */
+    /* Well, now we have a header, process the file ...            */
 
-              buffer_p += (((finfo.size - 1)/TBLOCK)+1)*TBLOCK;
-              next_header = 1;  /* Force read of next header */
+    /* Should we skip the file? We have the long name as well here */
 
-            }
-          }
-         tskip=clipn
-           && ((!tar_re_search && clipfind(cliplist, clipn, finfo.name) ^ tar_excl)
+    skip = clipn &&
+      ((!tar_re_search && clipfind(cliplist, clipn, finfo.name) ^ tar_excl)
 #ifdef HAVE_REGEX_H
-               || (tar_re_search && !regexec(preg, finfo.name, 0, NULL, 0)));
+      || (tar_re_search && !regexec(preg, finfo.name, 0, NULL, 0)));
 #else
-               || (tar_re_search && mask_match(finfo.name, cliplist[0], True, False)));
+      || (tar_re_search && mask_match(finfo.name, cliplist[0], True, False)));
 #endif
-         if (tskip) {
-           buffer_p+=TBLOCK;
-           if (finfo.mode & aDIR)
-             continue;
-           else if ((fsize=finfo.size) % TBLOCK) {
-             fsize+=TBLOCK-(fsize%TBLOCK);
-           }
-           if (fsize<endofbuffer-buffer_p) {
-             buffer_p+=fsize;
-             fsize=0;
-             continue;
-           } else {
-             fsize-=endofbuffer-buffer_p;
-             break;
-           }
-         }
 
-         DEBUG(5, ("do_tarput: File is: %s\n", finfo.name));
+  DEBUG(5, ("Skip = %i, cliplist=%s, file=%s\n", skip, (cliplist?cliplist[0]:NULL), finfo.name));
 
-         if (finfo.mode & aDIR)
-           {
+  if (skip) {
 
-             DEBUG(5, ("Creating directory: %s\n", finfo.name));
+    skip_file(finfo.size);
+    continue;
 
-             if (!ensurepath(finfo.name, inbuf, outbuf))
-               {
-                 DEBUG(0, ("abandoning restore, problems ensuring path\n"));
-                 free(inbuf); free(outbuf);
-                 return;
-             }
-             else
-               {
-                 /* Now we update the creation date ... */
+  }
 
-                 DEBUG(0, ("Updating creation date on %s\n", finfo.name));
+    /* We only get this far if we should process the file */
+  linkflag = ((union hblock *)buffer_p) -> dbuf.linkflag;
 
-                 if (!do_setrtime(finfo.name, finfo.mtime)) {
+    switch (linkflag) {
 
-                    DEBUG(0, ("Could not set time on file: %s\n", finfo.name));
-                    return;
+    case '0':  /* Should use symbolic names--FIXME */
 
-                  }
+      /* Skip to the next block first, so we can get the file, FIXME, should
+         be in get_file ... */
+
+      if (next_block(tarbuf, &buffer_p, tbufsiz) <=0) {
+       DEBUG(0, ("Short file, bailing out...\n"));
+       return;
+      }
+      if (!get_file(finfo)) {
+       DEBUG(0, ("Abandoning restore\n"));
+       return;
 
-                 ntarf++;
-                 buffer_p+=TBLOCK;
-                 continue;
-               }
-           }
-         
-         fsize=finfo.size;
-
-         if (ensurepath(finfo.name, inbuf, outbuf)
-             && !smbcreat(finfo, &fnum, inbuf, outbuf))
-           {
-             DEBUG(0, ("abandoning restore\n"));
-             free(inbuf);free(outbuf);
-             return;
-           }
-
-         DEBUG(0 ,("restore tar file %s of size %d bytes\n",
-                  finfo.name,finfo.size));
-
-         /*          if (!finfo.size) {
-           if (!smbshut(finfo, fnum, inbuf, outbuf)){
-              DEBUG(0, ("Error closing remote file of length 0: %s\n", finfo.name));
-             free(inbuf);free(outbuf);
-              return;
-            }
-           } */
-
-         nread=0;
-         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 - buffer_p)
-       ? fsize - nread : endofbuffer - buffer_p;
-      
-      while (chunk > 0) {
-       int minichunk=MIN(chunk, max_xmit-200);
-       
-       if (!smbwrite(fnum, /* file descriptor */
-                     minichunk, /* n */
-                     nread, /* offset low */
-                     0, /* offset high - not implemented */
-                     fsize-nread, /* left - only hint to server */
-                     buffer_p,
-                     inbuf,
-                     outbuf))
-         {
-           DEBUG(0, ("Error writing remote file\n"));
-           free(inbuf); free(outbuf);
-           return;
-         }
-       DEBUG(5, ("chunk writing fname=%s fnum=%d nread=%d minichunk=%d chunk=%d size=%d\n", finfo.name, fnum, nread, minichunk, chunk, fsize));
-       
-       buffer_p+=minichunk; nread+=minichunk;
-       chunk-=minichunk;
       }
+      break;
 
-      if (nread>=fsize)
-       {
-         if (!smbshut(finfo, fnum, inbuf, outbuf))
-           {
-             DEBUG(0, ("Error closing remote file\n"));
-             free(inbuf);free(outbuf);
-             return;
-           }
-         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;
+    case '5':
+      if (!get_dir(finfo)) {
+       DEBUG(0, ("Abandoning restore \n"));
+       return;
+      }
+      break;
+
+    case 'L':
+      longfilename = get_longfilename(finfo);
+      if (!longfilename) {
+       DEBUG(0, ("abandoning restore\n"));
+       return;
+
+      }
+      DEBUG(5, ("Long file name: %s\n", longfilename));
+      break;
+
+    default:
+      skip_file(finfo.size);  /* Don't handle these yet */
+      break;
+
+    }
 
-       }
-    } while (buffer_p < endofbuffer);
   }
 
-  DEBUG(0, ("premature eof on tar file ?\n"));
-  DEBUG(0,("total of %d tar files restored to share\n", ntarf));
 
-  free(inbuf); free(outbuf);
 }
 
+
 /*
  * samba interactive commands
  */
@@ -1979,12 +1279,12 @@ static void do_tarput()
 /****************************************************************************
 Blocksize command
 ***************************************************************************/
-void cmd_block(char *dum_in, char *dum_out)
+void cmd_block(void)
 {
   fstring buf;
   int block;
 
-  if (!next_token(NULL,buf,NULL))
+  if (!next_token(NULL,buf,NULL,sizeof(buf)))
     {
       DEBUG(0, ("blocksize <n>\n"));
       return;
@@ -2004,11 +1304,11 @@ void cmd_block(char *dum_in, char *dum_out)
 /****************************************************************************
 command to set incremental / reset mode
 ***************************************************************************/
-void cmd_tarmode(char *dum_in, char *dum_out)
+void cmd_tarmode(void)
 {
   fstring buf;
 
-  while (next_token(NULL,buf,NULL)) {
+  while (next_token(NULL,buf,NULL,sizeof(buf))) {
     if (strequal(buf, "full"))
       tar_inc=False;
     else if (strequal(buf, "inc"))
@@ -2044,26 +1344,26 @@ void cmd_tarmode(char *dum_in, char *dum_out)
 /****************************************************************************
 Feeble attrib command
 ***************************************************************************/
-void cmd_setmode(char *dum_in, char *dum_out)
+void cmd_setmode(void)
 {
   char *q;
   fstring buf;
   pstring fname;
-  int attra[2];
+  uint16 attra[2];
   int direct=1;
 
   attra[0] = attra[1] = 0;
 
-  if (!next_token(NULL,buf,NULL))
+  if (!next_token(NULL,buf,NULL,sizeof(buf)))
     {
-      DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
+      DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
       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)) {
+  while (next_token(NULL,buf,NULL,sizeof(buf))) {
     q=buf;
 
     while(*q)
@@ -2087,27 +1387,27 @@ void cmd_setmode(char *dum_in, char *dum_out)
 
   if (attra[ATTRSET]==0 && attra[ATTRRESET]==0)
     {
-      DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
+      DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
       return;
     }
 
   DEBUG(2, ("\nperm set %d %d\n", attra[ATTRSET], attra[ATTRRESET]));
-  (void) do_setrattr(fname, attra[ATTRSET], ATTRSET);
-  (void) do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
+  do_setrattr(fname, attra[ATTRSET], ATTRSET);
+  do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
 }
 
 /****************************************************************************
 Principal command for creating / extracting
 ***************************************************************************/
-void cmd_tar(char *inbuf, char *outbuf)
+void cmd_tar(void)
 {
   fstring buf;
   char **argl;
   int argcl;
 
-  if (!next_token(NULL,buf,NULL))
+  if (!next_token(NULL,buf,NULL,sizeof(buf)))
     {
-      DEBUG(0,("tar <c|x>[IXbga] <filename>\n"));
+      DEBUG(0,("tar <c|x>[IXbgan] <filename>\n"));
       return;
     }
 
@@ -2115,7 +1415,7 @@ void cmd_tar(char *inbuf, char *outbuf)
   if (!tar_parseargs(argcl, argl, buf, 0))
     return;
 
-  process_tar(inbuf, outbuf);
+  process_tar();
 
   free(argl);
 }
@@ -2123,13 +1423,13 @@ void cmd_tar(char *inbuf, char *outbuf)
 /****************************************************************************
 Command line (option) version
 ***************************************************************************/
-int process_tar(char *inbuf, char *outbuf)
+int process_tar(void)
 {
   initarbuf();
   switch(tar_type) {
   case 'x':
 
-#ifdef 0
+#if 0
     do_tarput2();
 #else
     do_tarput();
@@ -2153,30 +1453,33 @@ 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);
+         DEBUG(5, ("process_tar, do_list with tarmac: %s\n", tarmac));
+         do_list(tarmac,attribute,do_tar, False, True);
+         safe_strcpy(cur_dir,saved_dir, sizeof(pstring));
        } else {
-         strcpy(tarmac, cur_dir);
-         strcat(tarmac, cliplist[i]);
-         do_dir((char *)inbuf,(char *)outbuf,tarmac,attribute,do_tar,recurse, True);
+         safe_strcpy(tarmac, cur_dir, sizeof(pstring));
+         safe_strcat(tarmac, cliplist[i], sizeof(pstring));
+         DEBUG(5, ("process_tar, do_list with tarmac: %s\n", tarmac));
+         do_list(tarmac,attribute,do_tar, False, True);
        }
       }
     } else {
       pstring mask;
-      strcpy(mask,cur_dir);
-      strcat(mask,"\\*");
-      do_dir((char *)inbuf,(char *)outbuf,mask,attribute,do_tar,recurse, True);
+      safe_strcpy(mask,cur_dir, sizeof(pstring));
+      DEBUG(5, ("process_tar, do_list with mask: %s\n", mask));
+      safe_strcat(mask,"\\*", sizeof(pstring));
+      do_list(mask,attribute,do_tar,False, True);
     }
     
     if (ntarf) dotareof(tarhandle);
@@ -2184,17 +1487,28 @@ int process_tar(char *inbuf, char *outbuf)
     free(tarbuf);
     
     DEBUG(0, ("tar: dumped %d tar files\n", ntarf));
-    DEBUG(0, ("Total bytes written: %d\n", ttarf));
+    DEBUG(0, ("Total bytes written: %.0f\n", (double)ttarf));
     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);
 }
 
 /****************************************************************************
 Find a token (filename) in a clip list
 ***************************************************************************/
-int clipfind(char **aret, int ret, char *tok)
+static int clipfind(char **aret, int ret, char *tok)
 {
   if (aret==NULL) return 0;
 
@@ -2213,6 +1527,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 = sys_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.
 ***************************************************************************/
@@ -2225,6 +1647,7 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
    */
   tar_type='\0';
   tar_excl=True;
+  dry_run=False;
 
   while (*Optarg) 
     switch(*Optarg++) {
@@ -2254,10 +1677,10 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
        DEBUG(0,("Option N must be followed by valid file name\n"));
        return 0;
       } else {
-       struct stat stbuf;
+       SMB_STRUCT_STAT stbuf;
        extern time_t newer_than;
        
-       if (sys_stat(argv[Optind], &stbuf) == 0) {
+       if (dos_stat(argv[Optind], &stbuf) == 0) {
          newer_than = stbuf.st_mtime;
          DEBUG(1,("Getting files newer than %s",
                   asctime(LocalTime(&newer_than))));
@@ -2271,24 +1694,43 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
     case 'a':
       tar_reset=True;
       break;
+    case 'q':
+      tar_noisy=False;
+      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;
       break;
+    case 'n':
+      if (tar_type == 'c') {
+       DEBUG(0, ("dry_run set\n"));
+       dry_run = True;
+      } else {
+       DEBUG(0, ("n is only meaningful when creating a tar-file\n"));
+       return 0;
+      }
+      break;
     default:
       DEBUG(0,("Unknown tar option\n"));
       return 0;
@@ -2299,9 +1741,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;
@@ -2327,13 +1779,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 */
@@ -2367,9 +1820,23 @@ int tar_parseargs(int argc, char *argv[], char *Optarg, int Optind)
   if (Optind>=argc || !strcmp(argv[Optind], "-")) {
     /* Sets tar handle to either 0 or 1, as appropriate */
     tarhandle=(tar_type=='c');
+    /*
+     * Make sure that dbf points to stderr if we are using stdout for 
+     * tar output
+    */
+    if (tarhandle == 1) 
+      dbf = stderr;
   } else {
-    if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY)) == -1)
-       || (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
+    if (tar_type=='c' && (dry_run || strcmp(argv[Optind], "/dev/null")==0))
+      {
+       if (!dry_run) {
+         DEBUG(0,("Output is /dev/null, assuming dry_run"));
+         dry_run = True;
+       }
+       tarhandle=-1;
+      } else
+    if ((tar_type=='x' && (tarhandle = sys_open(argv[Optind], O_RDONLY, 0)) == -1)
+       || (tar_type=='c' && (tarhandle=sys_creat(argv[Optind], 0644)) < 0))
       {
        DEBUG(0,("Error opening local file %s - %s\n",
                 argv[Optind], strerror(errno)));