r3447: more include/system/XXX.h include files
[samba.git] / source4 / client / clitar.c
index dce914e9a6e87c15c5ab2808494d32f9af508906..3746f51248f4fe2833291b8130741308b1b4c4b6 100644 (file)
 
 #include "includes.h"
 #include "clitar.h"
+#include "system/time.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);
@@ -67,7 +112,7 @@ 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 */
@@ -121,8 +166,8 @@ static int blocksize=20;
 static int tarhandle;
 
 static void writetarheader(int f,  const char *aname, uint64_t size, time_t mtime,
-                          const char *amode, unsigned char ftype);
-static void do_atar(char *rname,char *lname,file_info *finfo1);
+                          const char *amode, uint8_t ftype);
+static void do_atar(char *rname,const char *lname,file_info *finfo1);
 static void do_tar(file_info *finfo);
 static void oct_it(uint64_t value, int ndgs, char *p);
 static void fixtarname(char *tptr, const char *fp, int l);
@@ -164,7 +209,7 @@ static char *string_create_s(int size)
 Write a tar header to buffer
 ****************************************************************************/
 static void writetarheader(int f, const char *aname, uint64_t size, time_t mtime,
-                          const char *amode, unsigned char ftype)
+                          const char *amode, uint8_t ftype)
 {
   union hblock hb;
   int i, chk, l;
@@ -390,7 +435,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)
@@ -399,7 +444,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;
@@ -538,8 +583,8 @@ static BOOL ensurepath(char *fname)
     {
       safe_strcat(partpath, p, strlen(fname) + 1);
 
-      if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, partpath))) {
-       if (NT_STATUS_IS_ERR(cli_mkdir(cli->tree, 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;
@@ -577,7 +622,7 @@ static void do_setrattr(char *name, uint16_t attr, int set)
 {
        uint16_t oldattr;
 
-       if (NT_STATUS_IS_ERR(cli_getatr(cli->tree, name, &oldattr, NULL, NULL)))
+       if (NT_STATUS_IS_ERR(smbcli_getatr(cli->tree, name, &oldattr, NULL, NULL)))
                return;
 
        if (set == ATTRSET) {
@@ -586,8 +631,8 @@ static void do_setrattr(char *name, uint16_t attr, int set)
                attr = oldattr & ~attr;
        }
 
-       if (NT_STATUS_IS_ERR(cli_setatr(cli->tree, name, attr, 0))) {
-               DEBUG(1,("setatr failed: %s\n", cli_errstr(cli->tree)));
+       if (NT_STATUS_IS_ERR(smbcli_setatr(cli->tree, name, attr, 0))) {
+               DEBUG(1,("setatr failed: %s\n", smbcli_errstr(cli->tree)));
        }
 }
 
@@ -595,7 +640,7 @@ static void do_setrattr(char *name, uint16_t 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,file_info *finfo1)
 {
   int fnum;
   uint64_t nread=0;
@@ -620,7 +665,7 @@ 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 {
          ZERO_STRUCT(finfo);
@@ -637,13 +682,13 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       return;
     }
 
-  fnum = cli_open(cli->tree, 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->tree),rname, cur_dir));
+                  smbcli_errstr(cli->tree),rname, cur_dir));
          return;
   }
 
@@ -656,8 +701,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 (NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum, &finfo.mode, &size, NULL, &finfo.atime, &finfo.mtime))) {
-                 DEBUG(0, ("getattrE: %s\n", cli_errstr(cli->tree)));
+         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;
@@ -695,10 +740,10 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
              
              DEBUG(3,("nread=%.0f\n",(double)nread));
              
-             datalen = cli_read(cli->tree, 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->tree)));
+                     DEBUG(0,("Error reading file %s : %s\n", rname, smbcli_errstr(cli->tree)));
                      break;
              }
              
@@ -743,7 +788,7 @@ static void do_atar(char *rname,char *lname,file_info *finfo1)
       ntarf++;
     }
   
-  cli_close(cli->tree, fnum);
+  smbcli_close(cli->tree, fnum);
 
   if (shallitime)
     {
@@ -959,7 +1004,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->tree, 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);
   }
@@ -976,7 +1021,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->tree, 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;
     }
@@ -1029,7 +1074,7 @@ static int get_file(file_info2 finfo)
 
   /* Now close the file ... */
 
-  if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
+  if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
          DEBUG(0, ("Error closing remote file\n"));
          return(False);
   }
@@ -1038,7 +1083,7 @@ static int get_file(file_info2 finfo)
 
   DEBUG(5, ("Updating creation date on %s\n", finfo.name));
 
-  if (NT_STATUS_IS_ERR(cli_setatr(cli->tree, 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 */
@@ -1272,12 +1317,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;
@@ -1299,11 +1344,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"))
@@ -1340,7 +1385,7 @@ int cmd_tarmode(void)
 /****************************************************************************
 Feeble attrib command
 ***************************************************************************/
-int cmd_setmode(void)
+int cmd_setmode(const char **cmd_ptr)
 {
   char *q;
   fstring buf;
@@ -1350,7 +1395,7 @@ int cmd_setmode(void)
 
   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;
@@ -1359,7 +1404,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)
@@ -1397,19 +1442,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;
 
@@ -1677,10 +1722,10 @@ 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))));
@@ -1835,8 +1880,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)));