r3959: fix compiler warnings
[samba.git] / source4 / client / clitar.c
index 8ab54a40c2e6abe9b33e1cefa315ede415d77b88..476e29d4ba693574febfb7f2f584eb07c03c1ad9 100644 (file)
 
 
 #include "includes.h"
+#include "client.h"
 #include "clitar.h"
+#include "system/time.h"
+#include "system/iconv.h"
+#include "system/filesys.h"
+
+/**
+ Convert list of tokens to array; dependent on above routine.
+**/
+static char **toktocliplist(char *ptr, int *ctok, const char *sep)
+{
+       char *s = ptr;
+       int ictok=0;
+       char **ret, **iret;
+
+       if (!sep)
+               sep = " \t\n\r";
+
+       while(*s && strchr_m(sep,*s))
+               s++;
+
+       /* nothing left? */
+       if (!*s)
+               return(NULL);
+
+       do {
+               ictok++;
+               while(*s && (!strchr_m(sep,*s)))
+                       s++;
+               while(*s && strchr_m(sep,*s))
+                       *s++=0;
+       } while(*s);
+       
+       *ctok=ictok;
+       s = ptr;
+       
+       if (!(ret=iret=malloc(ictok*sizeof(char *))))
+               return NULL;
+       
+       while(ictok--) {    
+               *iret++=s;
+               while(*s++)
+                       ;
+               while(!*s)
+                       s++;
+       }
+
+       return ret;
+}
 
 static int clipfind(char **aret, int ret, char *tok);
 void dos_clean_name(char *s);
@@ -45,8 +93,8 @@ typedef struct file_info_struct file_info2;
 
 struct file_info_struct
 {
-  SMB_BIG_UINT size;
-  uint16 mode;
+  uint64_t size;
+  uint16_t mode;
   uid_t uid;
   gid_t gid;
   /* These times are normally kept in GMT */
@@ -67,14 +115,14 @@ typedef struct
 } stack;
 
 #define SEPARATORS " \t\n\r"
-extern struct cli_state *cli;
+extern struct smbcli_state *cli;
 
 /* 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 uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+static uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
 
 #ifndef CLIENT_TIMEOUT
 #define CLIENT_TIMEOUT (30*1000)
@@ -109,9 +157,8 @@ static char **cliplist=NULL;
 static int clipn=0;
 static BOOL must_free_cliplist = False;
 
-extern file_info def_finfo;
 extern BOOL lowercase;
-extern uint16 cnum;
+extern uint16_t cnum;
 extern BOOL readbraw_supported;
 extern int max_xmit;
 extern pstring cur_dir;
@@ -121,11 +168,11 @@ extern int get_total_size;
 static int blocksize=20;
 static int tarhandle;
 
-static void writetarheader(int f,  const char *aname, SMB_BIG_UINT size, time_t mtime,
-                          const char *amode, unsigned char ftype);
-static void do_atar(char *rname,char *lname,file_info *finfo1);
-static void do_tar(file_info *finfo);
-static void oct_it(SMB_BIG_UINT value, int ndgs, char *p);
+static void writetarheader(int f,  const char *aname, uint64_t size, time_t mtime,
+                          const char *amode, uint8_t ftype);
+static void do_atar(char *rname,const char *lname,struct file_info *finfo1);
+static void do_tar(struct file_info *finfo);
+static void oct_it(uint64_t value, int ndgs, char *p);
 static void fixtarname(char *tptr, const char *fp, int l);
 static int dotarbuf(int f, char *b, int n);
 static void dozerobuf(int f, int n);
@@ -164,8 +211,8 @@ static char *string_create_s(int size)
 /****************************************************************************
 Write a tar header to buffer
 ****************************************************************************/
-static void writetarheader(int f, const char *aname, SMB_BIG_UINT size, time_t mtime,
-                          const char *amode, unsigned char ftype)
+static void writetarheader(int f, const char *aname, uint64_t size, time_t mtime,
+                          const char *amode, uint8_t ftype)
 {
   union hblock hb;
   int i, chk, l;
@@ -203,17 +250,17 @@ static void writetarheader(int f, const char *aname, SMB_BIG_UINT size, time_t m
 
   hb.dbuf.name[NAMSIZ-1]='\0';
   safe_strcpy(hb.dbuf.mode, amode, strlen(amode));
-  oct_it((SMB_BIG_UINT)0, 8, hb.dbuf.uid);
-  oct_it((SMB_BIG_UINT)0, 8, hb.dbuf.gid);
-  oct_it((SMB_BIG_UINT) size, 13, hb.dbuf.size);
-  oct_it((SMB_BIG_UINT) mtime, 13, hb.dbuf.mtime);
+  oct_it((uint64_t)0, 8, hb.dbuf.uid);
+  oct_it((uint64_t)0, 8, hb.dbuf.gid);
+  oct_it((uint64_t) size, 13, hb.dbuf.size);
+  oct_it((uint64_t) mtime, 13, hb.dbuf.mtime);
   memcpy(hb.dbuf.chksum, "        ", sizeof(hb.dbuf.chksum));
   memset(hb.dbuf.linkname, 0, NAMSIZ);
   hb.dbuf.linkflag=ftype;
   
   for (chk=0, i=sizeof(hb.dummy), jp=hb.dummy; --i>=0;) chk+=(0xFF & *jp++);
 
-  oct_it((SMB_BIG_UINT) chk, 8, hb.dbuf.chksum);
+  oct_it((uint64_t) chk, 8, hb.dbuf.chksum);
   hb.dbuf.chksum[6] = '\0';
 
   (void) dotarbuf(f, hb.dummy, sizeof(hb.dummy));
@@ -253,7 +300,7 @@ static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix)
   if (fchk != chk)
     {
       DEBUG(0, ("checksums don't match %ld %ld\n", fchk, chk));
-      dump_data(5, (char *)hb - TBLOCK, TBLOCK *3);
+      dump_data(5, (uint8_t *)hb - TBLOCK, TBLOCK *3);
       return -1;
     }
 
@@ -391,7 +438,7 @@ Write two zero blocks at end of file
 ****************************************************************************/
 static void dotareof(int f)
 {
-  SMB_STRUCT_STAT stbuf;
+  struct stat stbuf;
   /* Two zero blocks at end of file, write out full buffer */
 
   if (dry_run)
@@ -400,7 +447,7 @@ static void dotareof(int f)
   (void) dozerobuf(f, TBLOCK);
   (void) dozerobuf(f, TBLOCK);
 
-  if (sys_fstat(f, &stbuf) == -1)
+  if (fstat(f, &stbuf) == -1)
     {
       DEBUG(0, ("Couldn't stat file handle\n"));
       return;
@@ -427,7 +474,7 @@ static void fixtarname(char *tptr, const char *fp, int l)
 /****************************************************************************
 Convert from decimal to octal string
 ****************************************************************************/
-static void oct_it (SMB_BIG_UINT value, int ndgs, char *p)
+static void oct_it (uint64_t value, int ndgs, char *p)
 {
   /* Converts long to octal string, pads with leading zeros */
 
@@ -539,8 +586,8 @@ static BOOL ensurepath(char *fname)
     {
       safe_strcat(partpath, p, strlen(fname) + 1);
 
-      if (!cli_chkpath(cli, partpath)) {
-       if (!cli_mkdir(cli, partpath))
+      if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, partpath))) {
+       if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, partpath)))
          {
            DEBUG(0, ("Error mkdirhiering\n"));
            return False;
@@ -574,11 +621,12 @@ static int padit(char *buf, int bufsize, int padsize)
 }
 
 
-static void do_setrattr(char *name, uint16 attr, int set)
+static void do_setrattr(char *name, uint16_t attr, int set)
 {
-       uint16 oldattr;
+       uint16_t oldattr;
 
-       if (!cli_getatr(cli, name, &oldattr, NULL, NULL)) return;
+       if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, name, &oldattr, NULL, NULL)))
+               return;
 
        if (set == ATTRSET) {
                attr |= oldattr;
@@ -586,8 +634,8 @@ static void do_setrattr(char *name, uint16 attr, int set)
                attr = oldattr & ~attr;
        }
 
-       if (!cli_setatr(cli, name, attr, 0)) {
-               DEBUG(1,("setatr failed: %s\n", cli_errstr(cli)));
+       if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, name, attr, 0))) {
+               DEBUG(1,("setatr failed: %s\n", smbcli_errstr(cli->tree)));
        }
 }
 
@@ -595,10 +643,10 @@ static void do_setrattr(char *name, uint16 attr, int set)
 /****************************************************************************
 append one remote file to the tar file
 ***************************************************************************/
-static void do_atar(char *rname,char *lname,file_info *finfo1)
+static void do_atar(char *rname,const char *lname,struct file_info *finfo1)
 {
   int fnum;
-  SMB_BIG_UINT nread=0;
+  uint64_t nread=0;
   char ftype;
   file_info2 finfo;
   BOOL close_done = False;
@@ -620,17 +668,10 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
     finfo.mtime = finfo1 -> mtime;
     finfo.atime = finfo1 -> atime;
     finfo.ctime = finfo1 -> ctime;
-    finfo.name  = finfo1 -> name;
+    finfo.name  = discard_const_p(char, finfo1 -> name);
   }
   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;
-    finfo.name  = def_finfo.name;
+         ZERO_STRUCT(finfo);
   }
 
   if (dry_run)
@@ -644,13 +685,13 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       return;
     }
 
-  fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
+  fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
 
   dos_clean_name(rname);
 
   if (fnum == -1) {
          DEBUG(0,("%s opening remote file %s (%s)\n",
-                  cli_errstr(cli),rname, cur_dir));
+                  smbcli_errstr(cli->tree),rname, cur_dir));
          return;
   }
 
@@ -663,8 +704,8 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
   safe_strcpy(finfo.name,rname, strlen(rname));
   if (!finfo1) {
          size_t size;
-         if (!cli_getattrE(cli, fnum, &finfo.mode, &size, NULL, &finfo.atime, &finfo.mtime)) {
-                 DEBUG(0, ("getattrE: %s\n", cli_errstr(cli)));
+         if (NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, &finfo.mode, &size, NULL, &finfo.atime, &finfo.mtime))) {
+                 DEBUG(0, ("getattrE: %s\n", smbcli_errstr(cli->tree)));
                  return;
          }
          finfo.size = size;
@@ -702,10 +743,10 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
              
              DEBUG(3,("nread=%.0f\n",(double)nread));
              
-             datalen = cli_read(cli, fnum, data, nread, read_size);
+             datalen = smbcli_read(cli->tree, fnum, data, nread, read_size);
              
              if (datalen == -1) {
-                     DEBUG(0,("Error reading file %s : %s\n", rname, cli_errstr(cli)));
+                     DEBUG(0,("Error reading file %s : %s\n", rname, smbcli_errstr(cli->tree)));
                      break;
              }
              
@@ -750,7 +791,7 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       ntarf++;
     }
   
-  cli_close(cli, fnum);
+  smbcli_close(cli->tree, fnum);
 
   if (shallitime)
     {
@@ -785,7 +826,7 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
 /****************************************************************************
 Append single file to tar file (or not)
 ***************************************************************************/
-static void do_tar(file_info *finfo)
+static void do_tar(struct file_info *finfo)
 {
   pstring rname;
 
@@ -966,7 +1007,7 @@ static int get_file(file_info2 finfo)
   DEBUG(5, ("get_file: file: %s, size %i\n", finfo.name, (int)finfo.size));
 
   if (ensurepath(finfo.name) && 
-      (fnum=cli_open(cli, finfo.name, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) == -1) {
+      (fnum=smbcli_open(cli->tree, finfo.name, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) == -1) {
       DEBUG(0, ("abandoning restore\n"));
       return(False);
   }
@@ -983,7 +1024,7 @@ static int get_file(file_info2 finfo)
     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) {
+    if (smbcli_write(cli->tree, fnum, 0, buffer_p + bpos, pos, dsize) != dsize) {
            DEBUG(0, ("Error writing remote file\n"));
            return 0;
     }
@@ -1036,7 +1077,7 @@ static int get_file(file_info2 finfo)
 
   /* Now close the file ... */
 
-  if (!cli_close(cli, fnum)) {
+  if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
          DEBUG(0, ("Error closing remote file\n"));
          return(False);
   }
@@ -1045,7 +1086,7 @@ static int get_file(file_info2 finfo)
 
   DEBUG(5, ("Updating creation date on %s\n", finfo.name));
 
-  if (!cli_setatr(cli, finfo.name, finfo.mode, finfo.mtime)) {
+  if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, 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 */
@@ -1279,12 +1320,12 @@ static void do_tarput(void)
 /****************************************************************************
 Blocksize command
 ***************************************************************************/
-int cmd_block(void)
+int cmd_block(const char **cmd_ptr)
 {
   fstring buf;
   int block;
 
-  if (!next_token_nr(NULL,buf,NULL,sizeof(buf)))
+  if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)))
     {
       DEBUG(0, ("blocksize <n>\n"));
       return 1;
@@ -1306,11 +1347,11 @@ int cmd_block(void)
 /****************************************************************************
 command to set incremental / reset mode
 ***************************************************************************/
-int cmd_tarmode(void)
+int cmd_tarmode(const char **cmd_ptr)
 {
   fstring buf;
 
-  while (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+  while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
     if (strequal(buf, "full"))
       tar_inc=False;
     else if (strequal(buf, "inc"))
@@ -1347,17 +1388,17 @@ int cmd_tarmode(void)
 /****************************************************************************
 Feeble attrib command
 ***************************************************************************/
-int cmd_setmode(void)
+int cmd_setmode(const char **cmd_ptr)
 {
   char *q;
   fstring buf;
   pstring fname;
-  uint16 attra[2];
+  uint16_t attra[2];
   int direct=1;
 
   attra[0] = attra[1] = 0;
 
-  if (!next_token_nr(NULL,buf,NULL,sizeof(buf)))
+  if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)))
     {
       DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
       return 1;
@@ -1366,7 +1407,7 @@ int cmd_setmode(void)
   safe_strcpy(fname, cur_dir, sizeof(pstring));
   safe_strcat(fname, buf, sizeof(pstring));
 
-  while (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+  while (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
     q=buf;
 
     while(*q)
@@ -1404,19 +1445,19 @@ int cmd_setmode(void)
 /****************************************************************************
 Principal command for creating / extracting
 ***************************************************************************/
-int cmd_tar(void)
+int cmd_tar(const char **cmd_ptr)
 {
   fstring buf;
   char **argl;
   int argcl;
 
-  if (!next_token_nr(NULL,buf,NULL,sizeof(buf)))
+  if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)))
     {
       DEBUG(0,("tar <c|x>[IXbgan] <filename>\n"));
       return 1;
     }
 
-  argl=toktocliplist(&argcl, NULL);
+  argl=toktocliplist(discard_const_p(char, *cmd_ptr), &argcl, NULL);
   if (!tar_parseargs(argcl, argl, buf, 0))
     return 1;
 
@@ -1684,13 +1725,13 @@ int tar_parseargs(int argc, char *argv[], const char *Optarg, int Optind)
        DEBUG(0,("Option N must be followed by valid file name\n"));
        return 0;
       } else {
-       SMB_STRUCT_STAT stbuf;
+       struct stat stbuf;
        extern time_t newer_than;
        
-       if (sys_stat(argv[Optind], &stbuf) == 0) {
+       if (stat(argv[Optind], &stbuf) == 0) {
          newer_than = stbuf.st_mtime;
          DEBUG(1,("Getting files newer than %s",
-                  asctime(LocalTime(&newer_than))));
+                  asctime(localtime(&newer_than))));
          Optind++;
        } else {
          DEBUG(0,("Error setting newer-than time\n"));
@@ -1842,8 +1883,8 @@ int tar_parseargs(int argc, char *argv[], const char *Optarg, int Optind)
        }
        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))
+    if ((tar_type=='x' && (tarhandle = open(argv[Optind], O_RDONLY, 0)) == -1)
+       || (tar_type=='c' && (tarhandle=creat(argv[Optind], 0644)) < 0))
       {
        DEBUG(0,("Error opening local file %s - %s\n",
                 argv[Optind], strerror(errno)));