r3323: more warning reductions
[bbaumbach/samba-autobuild/.git] / source4 / client / client.c
index dc2aed7f55f6f68ae50b2297afc23ab06c516947..dce99a7833997ce9a74480c81d717663fc6e7c1e 100644 (file)
@@ -3,7 +3,7 @@
    SMB client
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Simo Sorce 2001-2002
-   Copyright (C) Jelmer Vernooij 2003
+   Copyright (C) Jelmer Vernooij 2003-2004
    Copyright (C) James J Myers   2003 <myersjj@samba.org>
    
    This program is free software; you can redistribute it and/or modify
@@ -26,7 +26,7 @@
 #define REGISTER 0
 #endif
 
-struct cli_state *cli;
+struct smbcli_state *cli;
 extern BOOL in_client;
 static int port = 0;
 pstring cur_dir = "\\";
@@ -34,9 +34,8 @@ static pstring cd_path = "";
 static pstring service;
 static pstring desthost;
 static pstring username;
+static pstring domain;
 static pstring password;
-static BOOL use_kerberos;
-static BOOL got_pass;
 static char *cmdstr = NULL;
 
 static int io_bufsize = 64512;
@@ -44,7 +43,7 @@ static int io_bufsize = 64512;
 static int name_type = 0x20;
 
 static int process_tok(fstring tok);
-static int cmd_help(void);
+static int cmd_help(const char **cmd_ptr);
 
 /* 30 second timeout on most commands */
 #define CLIENT_TIMEOUT (30*1000)
@@ -58,8 +57,6 @@ static int archive_level = 0;
 
 static BOOL translation = False;
 
-static BOOL have_ip;
-
 /* clitar bits insert */
 extern int blocksize;
 extern BOOL tar_inc;
@@ -74,7 +71,7 @@ static int printmode = 1;
 static BOOL recurse = False;
 BOOL lowercase = False;
 
-static struct in_addr dest_ip;
+static const char *dest_ip;
 
 #define SEPARATORS " \t\n\r"
 
@@ -83,10 +80,10 @@ static BOOL abort_mget = True;
 static pstring fileselection = "";
 
 /* timing globals */
-SMB_BIG_UINT get_total_size = 0;
-unsigned int get_total_time_ms = 0;
-static SMB_BIG_UINT put_total_size = 0;
-static unsigned int put_total_time_ms = 0;
+uint64_t get_total_size = 0;
+uint_t get_total_time_ms = 0;
+static uint64_t put_total_size = 0;
+static uint_t put_total_time_ms = 0;
 
 /* totals globals */
 static double dir_total;
@@ -94,7 +91,7 @@ static double dir_total;
 #define USENMB
 
 /* some forward declarations */
-static struct cli_state *do_connect(const char *server, const char *share);
+static struct smbcli_state *do_connect(const char *server, const char *share);
 
 
 /*******************************************************************
@@ -191,8 +188,8 @@ static void send_message(void)
        int total_len = 0;
        int grp_id;
 
-       if (!cli_message_start(cli->tree, desthost, username, &grp_id)) {
-               d_printf("message start: %s\n", cli_errstr(cli->tree));
+       if (!smbcli_message_start(cli->tree, desthost, username, &grp_id)) {
+               d_printf("message start: %s\n", smbcli_errstr(cli->tree));
                return;
        }
 
@@ -213,8 +210,8 @@ static void send_message(void)
                        msg[l] = c;   
                }
 
-               if (!cli_message_text(cli->tree, msg, l, grp_id)) {
-                       d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli->tree));
+               if (!smbcli_message_text(cli->tree, msg, l, grp_id)) {
+                       d_printf("SMBsendtxt failed (%s)\n",smbcli_errstr(cli->tree));
                        return;
                }      
                
@@ -226,8 +223,8 @@ static void send_message(void)
        else
                d_printf("sent %d bytes\n",total_len);
 
-       if (!cli_message_end(cli->tree, grp_id)) {
-               d_printf("SMBsendend failed (%s)\n",cli_errstr(cli->tree));
+       if (!smbcli_message_end(cli->tree, grp_id)) {
+               d_printf("SMBsendend failed (%s)\n",smbcli_errstr(cli->tree));
                return;
        }      
 }
@@ -241,8 +238,8 @@ static int do_dskattr(void)
 {
        int total, bsize, avail;
 
-       if (NT_STATUS_IS_ERR(cli_dskattr(cli->tree, &bsize, &total, &avail))) {
-               d_printf("Error in dskattr: %s\n",cli_errstr(cli->tree)); 
+       if (NT_STATUS_IS_ERR(smbcli_dskattr(cli->tree, &bsize, &total, &avail))) {
+               d_printf("Error in dskattr: %s\n",smbcli_errstr(cli->tree)); 
                return 1;
        }
 
@@ -255,13 +252,20 @@ static int do_dskattr(void)
 /****************************************************************************
 show cd/pwd
 ****************************************************************************/
-static int cmd_pwd(void)
+static int cmd_pwd(const char **cmd_ptr)
 {
        d_printf("Current directory is %s",service);
        d_printf("%s\n",cur_dir);
        return 0;
 }
 
+/*
+  convert a string to dos format
+*/
+static void dos_format(char *s)
+{
+       string_replace(s, '/', '\\');
+}
 
 /****************************************************************************
 change directory - inner section
@@ -290,8 +294,8 @@ static int do_cd(char *newdir)
        dos_clean_name(cur_dir);
        
        if (!strequal(cur_dir,"\\")) {
-               if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, dname))) {
-                       d_printf("cd %s: %s\n", dname, cli_errstr(cli->tree));
+               if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, dname))) {
+                       d_printf("cd %s: %s\n", dname, smbcli_errstr(cli->tree));
                        pstrcpy(cur_dir,saved_dir);
                }
        }
@@ -304,12 +308,12 @@ static int do_cd(char *newdir)
 /****************************************************************************
 change directory
 ****************************************************************************/
-static int cmd_cd(void)
+static int cmd_cd(const char **cmd_ptr)
 {
        fstring buf;
        int rc = 0;
 
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
                rc = do_cd(buf);
        else
                d_printf("Current directory is %s\n",cur_dir);
@@ -344,44 +348,6 @@ static BOOL do_this_one(file_info *finfo)
        return(True);
 }
 
-/*******************************************************************
- Return a string representing an attribute for a file.
-********************************************************************/
-static const char *attrib_string(uint16 mode)
-{
-       static fstring attrstr;
-       int i, len;
-       const struct {
-               char c;
-               uint16 attr;
-       } attr_strs[] = {
-               {'V', FILE_ATTRIBUTE_VOLUME},
-               {'D', FILE_ATTRIBUTE_DIRECTORY},
-               {'A', FILE_ATTRIBUTE_ARCHIVE},
-               {'H', FILE_ATTRIBUTE_HIDDEN},
-               {'S', FILE_ATTRIBUTE_SYSTEM},
-               {'R', FILE_ATTRIBUTE_READONLY},
-               {'d', FILE_ATTRIBUTE_DEVICE},
-               {'t', FILE_ATTRIBUTE_TEMPORARY},
-               {'s', FILE_ATTRIBUTE_SPARSE},
-               {'r', FILE_ATTRIBUTE_REPARSE_POINT},
-               {'c', FILE_ATTRIBUTE_COMPRESSED},
-               {'o', FILE_ATTRIBUTE_OFFLINE},
-               {'n', FILE_ATTRIBUTE_NONINDEXED},
-               {'e', FILE_ATTRIBUTE_ENCRYPTED}
-       };
-
-       for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
-               if (mode & attr_strs[i].attr) {
-                       attrstr[len++] = attr_strs[i].c;
-               }
-       }
-
-       attrstr[len] = 0;
-
-       return(attrstr);
-}
-
 /****************************************************************************
   display info about a file
   ****************************************************************************/
@@ -389,12 +355,14 @@ static void display_finfo(file_info *finfo)
 {
        if (do_this_one(finfo)) {
                time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
+               char *astr = attrib_string(NULL, finfo->mode);
                d_printf("  %-30s%7.7s %8.0f  %s",
                         finfo->name,
-                        attrib_string(finfo->mode),
+                        astr,
                         (double)finfo->size,
                         asctime(localtime(&t)));
                dir_total += finfo->size;
+               talloc_free(astr);
        }
 }
 
@@ -564,9 +532,9 @@ static void do_list_helper(file_info *f, const char *mask, void *state)
 
 
 /****************************************************************************
-a wrapper around cli_list that adds recursion
+a wrapper around smbcli_list that adds recursion
   ****************************************************************************/
-void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
+void do_list(const char *mask,uint16_t attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
 {
        static int in_do_list = 0;
 
@@ -591,14 +559,14 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
                {
                        /*
                         * Need to copy head so that it doesn't become
-                        * invalid inside the call to cli_list.  This
+                        * invalid inside the call to smbcli_list.  This
                         * would happen if the list were expanded
                         * during the call.
                         * Fix from E. Jay Berkenbilt (ejb@ql.org)
                         */
                        pstring head;
                        pstrcpy(head, do_list_queue_head());
-                       cli_list(cli->tree, head, attribute, do_list_helper, NULL);
+                       smbcli_list(cli->tree, head, attribute, do_list_helper, NULL);
                        remove_do_list_queue_head();
                        if ((! do_list_queue_empty()) && (fn == display_finfo))
                        {
@@ -622,9 +590,9 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
        }
        else
        {
-               if (cli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
+               if (smbcli_list(cli->tree, mask, attribute, do_list_helper, NULL) == -1)
                {
-                       d_printf("%s listing %s\n", cli_errstr(cli->tree), mask);
+                       d_printf("%s listing %s\n", smbcli_errstr(cli->tree), mask);
                }
        }
 
@@ -635,9 +603,9 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static int cmd_dir(void)
+static int cmd_dir(const char **cmd_ptr)
 {
-       uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mask;
        fstring buf;
        char *p=buf;
@@ -648,7 +616,7 @@ static int cmd_dir(void)
        if(mask[strlen(mask)-1]!='\\')
                pstrcat(mask,"\\");
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                dos_format(p);
                if (*p == '\\')
                        pstrcpy(mask,p);
@@ -656,7 +624,12 @@ static int cmd_dir(void)
                        pstrcat(mask,p);
        }
        else {
-               pstrcat(mask,"*");
+               if (cli->tree->session->transport->negotiate.protocol <= 
+                   PROTOCOL_LANMAN1) { 
+                       pstrcat(mask,"*.*");
+               } else {
+                       pstrcat(mask,"*");
+               }
        }
 
        do_list(mask, attribute, display_finfo, recurse, True);
@@ -672,9 +645,9 @@ static int cmd_dir(void)
 /****************************************************************************
   get a directory listing
   ****************************************************************************/
-static int cmd_du(void)
+static int cmd_du(const char **cmd_ptr)
 {
-       uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mask;
        fstring buf;
        char *p=buf;
@@ -685,14 +658,14 @@ static int cmd_du(void)
        if(mask[strlen(mask)-1]!='\\')
                pstrcat(mask,"\\");
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                dos_format(p);
                if (*p == '\\')
                        pstrcpy(mask,p);
                else
                        pstrcat(mask,p);
        } else {
-               pstrcat(mask,"*");
+               pstrcat(mask,"\\*");
        }
 
        do_list(mask, attribute, do_du, recurse, True);
@@ -715,7 +688,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        char *data;
        struct timeval tp_start;
        int read_size = io_bufsize;
-       uint16 attr;
+       uint16_t attr;
        size_t size;
        off_t start = 0;
        off_t nread = 0;
@@ -724,13 +697,13 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        GetTimeOfDay(&tp_start);
 
        if (lowercase) {
-               strlower(lname);
+               strlower(discard_const_p(char, lname));
        }
 
-       fnum = cli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
+       fnum = smbcli_open(cli->tree, rname, O_RDONLY, DENY_NONE);
 
        if (fnum == -1) {
-               d_printf("%s opening remote file %s\n",cli_errstr(cli->tree),rname);
+               d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
                return 1;
        }
 
@@ -757,11 +730,11 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        }
 
 
-       if (NT_STATUS_IS_ERR(cli_qfileinfo(cli->tree, fnum, 
+       if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, 
                           &attr, &size, NULL, NULL, NULL, NULL, NULL)) &&
-           NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum, 
+           NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, 
                          &attr, &size, NULL, NULL, NULL))) {
-               d_printf("getattrib: %s\n",cli_errstr(cli->tree));
+               d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
                return 1;
        }
 
@@ -770,12 +743,12 @@ static int do_get(char *rname, const char *lname, BOOL reget)
 
        if(!(data = (char *)malloc(read_size))) { 
                d_printf("malloc fail for size %d\n", read_size);
-               cli_close(cli->tree, fnum);
+               smbcli_close(cli->tree, fnum);
                return 1;
        }
 
        while (1) {
-               int n = cli_read(cli->tree, fnum, data, nread + start, read_size);
+               int n = smbcli_read(cli->tree, fnum, data, nread + start, read_size);
 
                if (n <= 0) break;
  
@@ -797,8 +770,8 @@ static int do_get(char *rname, const char *lname, BOOL reget)
 
        SAFE_FREE(data);
        
-       if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
-               d_printf("Error %s closing remote file\n",cli_errstr(cli->tree));
+       if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
+               d_printf("Error %s closing remote file\n",smbcli_errstr(cli->tree));
                rc = 1;
        }
 
@@ -807,7 +780,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
        }
 
        if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
-               cli_setatr(cli->tree, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
+               smbcli_setatr(cli->tree, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
        }
 
        {
@@ -833,7 +806,7 @@ static int do_get(char *rname, const char *lname, BOOL reget)
 /****************************************************************************
   get a file
   ****************************************************************************/
-static int cmd_get(void)
+static int cmd_get(const char **cmd_ptr)
 {
        pstring lname;
        pstring rname;
@@ -844,14 +817,14 @@ static int cmd_get(void)
        
        p = rname + strlen(rname);
        
-       if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(rname)-strlen(rname))) {
                d_printf("get <filename>\n");
                return 1;
        }
        pstrcpy(lname,p);
        dos_clean_name(rname);
        
-       next_token_nr(NULL,lname,NULL,sizeof(lname));
+       next_token(cmd_ptr,lname,NULL,sizeof(lname));
        
        return do_get(rname, lname, False);
 }
@@ -897,9 +870,10 @@ static void do_mget(file_info *finfo)
        pstrcat(cur_dir,finfo->name);
        pstrcat(cur_dir,"\\");
 
-       unix_format(finfo->name);
-       if (lowercase)
-               strlower(finfo->name);
+       string_replace(discard_const_p(char, finfo->name), '\\', '/');
+       if (lowercase) {
+               strlower(discard_const_p(char, finfo->name));
+       }
        
        if (!directory_exist(finfo->name,NULL) && 
            mkdir(finfo->name,0777) != 0) {
@@ -926,7 +900,7 @@ static void do_mget(file_info *finfo)
 /****************************************************************************
 view the file using the pager
 ****************************************************************************/
-static int cmd_more(void)
+static int cmd_more(const char **cmd_ptr)
 {
        fstring rname,lname,pager_cmd;
        char *pager;
@@ -944,7 +918,7 @@ static int cmd_more(void)
        }
        close(fd);
 
-       if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
+       if (!next_token(cmd_ptr,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
                d_printf("more <filename>\n");
                unlink(lname);
                return 1;
@@ -968,9 +942,9 @@ static int cmd_more(void)
 /****************************************************************************
 do a mget command
 ****************************************************************************/
-static int cmd_mget(void)
+static int cmd_mget(const char **cmd_ptr)
 {
-       uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        pstring mget_mask;
        fstring buf;
        char *p=buf;
@@ -982,7 +956,7 @@ static int cmd_mget(void)
        
        abort_mget = False;
 
-       while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                pstrcpy(mget_mask,cur_dir);
                if(mget_mask[strlen(mget_mask)-1]!='\\')
                        pstrcat(mget_mask,"\\");
@@ -1013,9 +987,9 @@ static NTSTATUS do_mkdir(char *name)
 {
        NTSTATUS status;
 
-       if (NT_STATUS_IS_ERR(status = cli_mkdir(cli->tree, name))) {
+       if (NT_STATUS_IS_ERR(status = smbcli_mkdir(cli->tree, name))) {
                d_printf("%s making remote directory %s\n",
-                        cli_errstr(cli->tree),name);
+                        smbcli_errstr(cli->tree),name);
                return status;
        }
 
@@ -1028,9 +1002,9 @@ show 8.3 name of a file
 static BOOL do_altname(char *name)
 {
        const char *altname;
-       if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli->tree, name, &altname))) {
+       if (!NT_STATUS_IS_OK(smbcli_qpathinfo_alt_name(cli->tree, name, &altname))) {
                d_printf("%s getting alt name for %s\n",
-                        cli_errstr(cli->tree),name);
+                        smbcli_errstr(cli->tree),name);
                return(False);
        }
        d_printf("%s\n", altname);
@@ -1042,9 +1016,9 @@ static BOOL do_altname(char *name)
 /****************************************************************************
  Exit client.
 ****************************************************************************/
-static int cmd_quit(void)
+static int cmd_quit(const char **cmd_ptr)
 {
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
        exit(0);
        /* NOTREACHED */
        return 0;
@@ -1054,7 +1028,7 @@ static int cmd_quit(void)
 /****************************************************************************
   make a directory
   ****************************************************************************/
-static int cmd_mkdir(void)
+static int cmd_mkdir(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
@@ -1062,7 +1036,7 @@ static int cmd_mkdir(void)
   
        pstrcpy(mask,cur_dir);
 
-       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                if (!recurse)
                        d_printf("mkdir <dirname>\n");
                return 1;
@@ -1079,7 +1053,7 @@ static int cmd_mkdir(void)
                p = strtok(ddir,"/\\");
                while (p) {
                        pstrcat(ddir2,p);
-                       if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, ddir2))) { 
+                       if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, ddir2))) { 
                                do_mkdir(ddir2);
                        }
                        pstrcat(ddir2,"\\");
@@ -1096,7 +1070,7 @@ static int cmd_mkdir(void)
 /****************************************************************************
   show alt name
   ****************************************************************************/
-static int cmd_altname(void)
+static int cmd_altname(const char **cmd_ptr)
 {
        pstring name;
        fstring buf;
@@ -1104,7 +1078,7 @@ static int cmd_altname(void)
   
        pstrcpy(name,cur_dir);
 
-       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                d_printf("altname <file>\n");
                return 1;
        }
@@ -1133,21 +1107,21 @@ static int do_put(char *rname, char *lname, BOOL reput)
        GetTimeOfDay(&tp_start);
 
        if (reput) {
-               fnum = cli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
+               fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT, DENY_NONE);
                if (fnum >= 0) {
-                       if (NT_STATUS_IS_ERR(cli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
-                           NT_STATUS_IS_ERR(cli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
-                               d_printf("getattrib: %s\n",cli_errstr(cli->tree));
+                       if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL)) &&
+                           NT_STATUS_IS_ERR(smbcli_getattrE(cli->tree, fnum, NULL, &start, NULL, NULL, NULL))) {
+                               d_printf("getattrib: %s\n",smbcli_errstr(cli->tree));
                                return 1;
                        }
                }
        } else {
-               fnum = cli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC, 
+               fnum = smbcli_open(cli->tree, rname, O_RDWR|O_CREAT|O_TRUNC, 
                                DENY_NONE);
        }
   
        if (fnum == -1) {
-               d_printf("%s opening remote file %s\n",cli_errstr(cli->tree),rname);
+               d_printf("%s opening remote file %s\n",smbcli_errstr(cli->tree),rname);
                return 1;
        }
 
@@ -1196,10 +1170,10 @@ static int do_put(char *rname, char *lname, BOOL reput)
                        break;
                }
 
-               ret = cli_write(cli->tree, fnum, 0, buf, nread + start, n);
+               ret = smbcli_write(cli->tree, fnum, 0, buf, nread + start, n);
 
                if (n != ret) {
-                       d_printf("Error writing file: %s\n", cli_errstr(cli->tree));
+                       d_printf("Error writing file: %s\n", smbcli_errstr(cli->tree));
                        rc = 1;
                        break;
                } 
@@ -1207,8 +1181,8 @@ static int do_put(char *rname, char *lname, BOOL reput)
                nread += n;
        }
 
-       if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
-               d_printf("%s closing remote file %s\n",cli_errstr(cli->tree),rname);
+       if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
+               d_printf("%s closing remote file %s\n",smbcli_errstr(cli->tree),rname);
                x_fclose(f);
                SAFE_FREE(buf);
                return 1;
@@ -1238,7 +1212,7 @@ static int do_put(char *rname, char *lname, BOOL reput)
        }
 
        if (f == x_stdin) {
-               cli_shutdown(cli);
+               smbcli_shutdown(cli);
                exit(0);
        }
        
@@ -1250,7 +1224,7 @@ static int do_put(char *rname, char *lname, BOOL reput)
 /****************************************************************************
   put a file
   ****************************************************************************/
-static int cmd_put(void)
+static int cmd_put(const char **cmd_ptr)
 {
        pstring lname;
        pstring rname;
@@ -1260,13 +1234,13 @@ static int cmd_put(void)
        pstrcpy(rname,cur_dir);
        pstrcat(rname,"\\");
   
-       if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                d_printf("put <filename>\n");
                return 1;
        }
        pstrcpy(lname,p);
   
-       if (next_token_nr(NULL,p,NULL,sizeof(buf)))
+       if (next_token(cmd_ptr,p,NULL,sizeof(buf)))
                pstrcat(rname,p);      
        else
                pstrcat(rname,lname);
@@ -1334,10 +1308,10 @@ static BOOL seek_list(struct file_list *list, char *name)
 /****************************************************************************
   set the file selection mask
   ****************************************************************************/
-static int cmd_select(void)
+static int cmd_select(const char **cmd_ptr)
 {
        pstrcpy(fileselection,"");
-       next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
+       next_token(cmd_ptr,fileselection,NULL,sizeof(fileselection));
 
        return 0;
 }
@@ -1408,12 +1382,12 @@ static int file_find(struct file_list **list, const char *directory,
 /****************************************************************************
   mput some files
   ****************************************************************************/
-static int cmd_mput(void)
+static int cmd_mput(const char **cmd_ptr)
 {
        fstring buf;
        char *p=buf;
        
-       while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
+       while (next_token(cmd_ptr,p,NULL,sizeof(buf))) {
                int ret;
                struct file_list *temp_list;
                char *quest, *lname, *rname;
@@ -1453,7 +1427,7 @@ static int cmd_mput(void)
                                        SAFE_FREE(rname);
                                        if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
                                        dos_format(rname);
-                                       if (NT_STATUS_IS_ERR(cli_chkpath(cli->tree, rname)) && 
+                                       if (NT_STATUS_IS_ERR(smbcli_chkpath(cli->tree, rname)) && 
                                            NT_STATUS_IS_ERR(do_mkdir(rname))) {
                                                DEBUG (0, ("Unable to make dir, skipping..."));
                                                /* Skip the directory */
@@ -1501,19 +1475,19 @@ static int do_cancel(int job)
 /****************************************************************************
   cancel a print job
   ****************************************************************************/
-static int cmd_cancel(void)
+static int cmd_cancel(const char **cmd_ptr)
 {
        fstring buf;
        int job; 
 
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("cancel <jobid> ...\n");
                return 1;
        }
        do {
                job = atoi(buf);
                do_cancel(job);
-       } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
+       } while (next_token(cmd_ptr,buf,NULL,sizeof(buf)));
        
        return 0;
 }
@@ -1522,13 +1496,13 @@ static int cmd_cancel(void)
 /****************************************************************************
   print a file
   ****************************************************************************/
-static int cmd_print(void)
+static int cmd_print(const char **cmd_ptr)
 {
        pstring lname;
        pstring rname;
        char *p;
 
-       if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
+       if (!next_token(cmd_ptr,lname,NULL, sizeof(lname))) {
                d_printf("print <filename>\n");
                return 1;
        }
@@ -1550,7 +1524,7 @@ static int cmd_print(void)
 /****************************************************************************
  show a print queue
 ****************************************************************************/
-static int cmd_queue(void)
+static int cmd_queue(const char **cmd_ptr)
 {
        d_printf("REWRITE: print job queue not implemented\n");
        
@@ -1560,42 +1534,26 @@ static int cmd_queue(void)
 /****************************************************************************
 delete some files
 ****************************************************************************/
-static void do_del(file_info *finfo)
-{
-       pstring mask;
-
-       pstrcpy(mask,cur_dir);
-       pstrcat(mask,finfo->name);
-
-       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) 
-               return;
-
-       if (NT_STATUS_IS_ERR(cli_unlink(cli->tree, mask))) {
-               d_printf("%s deleting remote file %s\n",cli_errstr(cli->tree),mask);
-       }
-}
-
-/****************************************************************************
-delete some files
-****************************************************************************/
-static int cmd_del(void)
+static int cmd_del(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
-       uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
 
        if (recurse)
                attribute |= FILE_ATTRIBUTE_DIRECTORY;
        
        pstrcpy(mask,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("del <filename>\n");
                return 1;
        }
        pstrcat(mask,buf);
 
-       do_list(mask, attribute,do_del,False,False);
+       if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, mask))) {
+               d_printf("%s deleting remote file %s\n",smbcli_errstr(cli->tree),mask);
+       }
        
        return 0;
 }
@@ -1604,7 +1562,7 @@ static int cmd_del(void)
 /****************************************************************************
 delete a whole directory tree
 ****************************************************************************/
-static int cmd_deltree(void)
+static int cmd_deltree(const char **cmd_ptr)
 {
        pstring dname;
        fstring buf;
@@ -1612,16 +1570,16 @@ static int cmd_deltree(void)
 
        pstrcpy(dname,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("deltree <dirname>\n");
                return 1;
        }
        pstrcat(dname,buf);
 
-       ret = cli_deltree(cli->tree, dname);
+       ret = smbcli_deltree(cli->tree, dname);
 
        if (ret == -1) {
-               printf("Failed to delete tree %s - %s\n", dname, cli_errstr(cli->tree));
+               printf("Failed to delete tree %s - %s\n", dname, smbcli_errstr(cli->tree));
                return -1;
        }
 
@@ -1634,7 +1592,7 @@ static int cmd_deltree(void)
 /****************************************************************************
 show as much information as possible about a file
 ****************************************************************************/
-static int cmd_allinfo(void)
+static int cmd_allinfo(const char **cmd_ptr)
 {
        pstring fname;
        fstring buf;
@@ -1645,13 +1603,13 @@ static int cmd_allinfo(void)
 
        pstrcpy(fname,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("allinfo <filename>\n");
                return 1;
        }
        pstrcat(fname,buf);
 
-       mem_ctx = talloc_init(fname);
+       mem_ctx = talloc_init("%s", fname);
 
        /* first a ALL_INFO QPATHINFO */
        finfo.generic.level = RAW_FILEINFO_ALL_INFO;
@@ -1663,10 +1621,10 @@ static int cmd_allinfo(void)
                goto done;
        }
 
-       d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.create_time));
-       d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.access_time));
-       d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.write_time));
-       d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.change_time));
+       d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.create_time));
+       d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.access_time));
+       d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, finfo.all_info.out.write_time));
+       d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, finfo.all_info.out.change_time));
        d_printf("\tattrib:         0x%x\n", finfo.all_info.out.attrib);
        d_printf("\talloc_size:     %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
        d_printf("\tsize:           %lu\n", (unsigned long)finfo.all_info.out.size);
@@ -1742,7 +1700,7 @@ done:
 /****************************************************************************
 show any ACL on a file
 ****************************************************************************/
-static int cmd_acl(void)
+static int cmd_acl(const char **cmd_ptr)
 {
        pstring fname;
        fstring buf;
@@ -1754,19 +1712,19 @@ static int cmd_acl(void)
 
        pstrcpy(fname,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("acl <filename>\n");
                return 1;
        }
        pstrcat(fname,buf);
 
-       fnum = cli_open(cli->tree, fname, O_RDONLY, DENY_NONE);
+       fnum = smbcli_open(cli->tree, fname, O_RDONLY, DENY_NONE);
        if (fnum == -1) {
-               d_printf("%s - %s\n", fname, cli_errstr(cli->tree));
+               d_printf("%s - %s\n", fname, smbcli_errstr(cli->tree));
                return -1;
        }
 
-       mem_ctx = talloc_init(fname);
+       mem_ctx = talloc_init("%s", fname);
 
        query.in.fnum = fnum;
        query.in.secinfo_flags = 0x7;
@@ -1778,6 +1736,8 @@ static int cmd_acl(void)
                goto done;
        }
 
+       NDR_PRINT_DEBUG(security_descriptor, query.out.sd);
+
        talloc_destroy(mem_ctx);
 
 done:
@@ -1787,20 +1747,20 @@ done:
 
 /****************************************************************************
 ****************************************************************************/
-static int cmd_open(void)
+static int cmd_open(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
        
        pstrcpy(mask,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("open <filename>\n");
                return 1;
        }
        pstrcat(mask,buf);
 
-       cli_open(cli->tree, mask, O_RDWR, DENY_ALL);
+       smbcli_open(cli->tree, mask, O_RDWR, DENY_ALL);
 
        return 0;
 }
@@ -1809,22 +1769,22 @@ static int cmd_open(void)
 /****************************************************************************
 remove a directory
 ****************************************************************************/
-static int cmd_rmdir(void)
+static int cmd_rmdir(const char **cmd_ptr)
 {
        pstring mask;
        fstring buf;
   
        pstrcpy(mask,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                d_printf("rmdir <dirname>\n");
                return 1;
        }
        pstrcat(mask,buf);
 
-       if (NT_STATUS_IS_ERR(cli_rmdir(cli->tree, mask))) {
+       if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, mask))) {
                d_printf("%s removing remote directory file %s\n",
-                        cli_errstr(cli->tree),mask);
+                        smbcli_errstr(cli->tree),mask);
        }
        
        return 0;
@@ -1833,7 +1793,7 @@ static int cmd_rmdir(void)
 /****************************************************************************
  UNIX hardlink.
 ****************************************************************************/
-static int cmd_link(void)
+static int cmd_link(const char **cmd_ptr)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1846,8 +1806,8 @@ static int cmd_link(void)
        pstrcpy(src,cur_dir);
        pstrcpy(dest,cur_dir);
   
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("link <src> <dest>\n");
                return 1;
        }
@@ -1855,8 +1815,8 @@ static int cmd_link(void)
        pstrcat(src,buf);
        pstrcat(dest,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_unix_hardlink(cli->tree, src, dest))) {
-               d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli->tree), src, dest);
+       if (NT_STATUS_IS_ERR(smbcli_unix_hardlink(cli->tree, src, dest))) {
+               d_printf("%s linking files (%s -> %s)\n", smbcli_errstr(cli->tree), src, dest);
                return 1;
        }  
 
@@ -1867,7 +1827,7 @@ static int cmd_link(void)
  UNIX symlink.
 ****************************************************************************/
 
-static int cmd_symlink(void)
+static int cmd_symlink(const char **cmd_ptr)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1880,8 +1840,8 @@ static int cmd_symlink(void)
        pstrcpy(src,cur_dir);
        pstrcpy(dest,cur_dir);
        
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("symlink <src> <dest>\n");
                return 1;
        }
@@ -1889,9 +1849,9 @@ static int cmd_symlink(void)
        pstrcat(src,buf);
        pstrcat(dest,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_unix_symlink(cli->tree, src, dest))) {
+       if (NT_STATUS_IS_ERR(smbcli_unix_symlink(cli->tree, src, dest))) {
                d_printf("%s symlinking files (%s -> %s)\n",
-                       cli_errstr(cli->tree), src, dest);
+                       smbcli_errstr(cli->tree), src, dest);
                return 1;
        } 
 
@@ -1902,7 +1862,7 @@ static int cmd_symlink(void)
  UNIX chmod.
 ****************************************************************************/
 
-static int cmd_chmod(void)
+static int cmd_chmod(const char **cmd_ptr)
 {
        pstring src;
        mode_t mode;
@@ -1915,8 +1875,8 @@ static int cmd_chmod(void)
 
        pstrcpy(src,cur_dir);
        
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("chmod mode file\n");
                return 1;
        }
@@ -1924,9 +1884,9 @@ static int cmd_chmod(void)
        mode = (mode_t)strtol(buf, NULL, 8);
        pstrcat(src,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_unix_chmod(cli->tree, src, mode))) {
+       if (NT_STATUS_IS_ERR(smbcli_unix_chmod(cli->tree, src, mode))) {
                d_printf("%s chmod file %s 0%o\n",
-                       cli_errstr(cli->tree), src, (unsigned int)mode);
+                       smbcli_errstr(cli->tree), src, (uint_t)mode);
                return 1;
        } 
 
@@ -1937,7 +1897,7 @@ static int cmd_chmod(void)
  UNIX chown.
 ****************************************************************************/
 
-static int cmd_chown(void)
+static int cmd_chown(const char **cmd_ptr)
 {
        pstring src;
        uid_t uid;
@@ -1951,9 +1911,9 @@ static int cmd_chown(void)
 
        pstrcpy(src,cur_dir);
        
-       if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token(NULL,buf2,NULL, sizeof(buf2)) ||
-           !next_token(NULL,buf3,NULL, sizeof(buf3))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2)) ||
+           !next_token(cmd_ptr,buf3,NULL, sizeof(buf3))) {
                d_printf("chown uid gid file\n");
                return 1;
        }
@@ -1962,9 +1922,9 @@ static int cmd_chown(void)
        gid = (gid_t)atoi(buf2);
        pstrcat(src,buf3);
 
-       if (NT_STATUS_IS_ERR(cli_unix_chown(cli->tree, src, uid, gid))) {
+       if (NT_STATUS_IS_ERR(smbcli_unix_chown(cli->tree, src, uid, gid))) {
                d_printf("%s chown file %s uid=%d, gid=%d\n",
-                       cli_errstr(cli->tree), src, (int)uid, (int)gid);
+                       smbcli_errstr(cli->tree), src, (int)uid, (int)gid);
                return 1;
        } 
 
@@ -1974,7 +1934,7 @@ static int cmd_chown(void)
 /****************************************************************************
 rename some files
 ****************************************************************************/
-static int cmd_rename(void)
+static int cmd_rename(const char **cmd_ptr)
 {
        pstring src,dest;
        fstring buf,buf2;
@@ -1982,8 +1942,8 @@ static int cmd_rename(void)
        pstrcpy(src,cur_dir);
        pstrcpy(dest,cur_dir);
        
-       if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
-           !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
+       if (!next_token(cmd_ptr,buf,NULL,sizeof(buf)) || 
+           !next_token(cmd_ptr,buf2,NULL, sizeof(buf2))) {
                d_printf("rename <src> <dest>\n");
                return 1;
        }
@@ -1991,8 +1951,8 @@ static int cmd_rename(void)
        pstrcat(src,buf);
        pstrcat(dest,buf2);
 
-       if (NT_STATUS_IS_ERR(cli_rename(cli->tree, src, dest))) {
-               d_printf("%s renaming files\n",cli_errstr(cli->tree));
+       if (NT_STATUS_IS_ERR(smbcli_rename(cli->tree, src, dest))) {
+               d_printf("%s renaming files\n",smbcli_errstr(cli->tree));
                return 1;
        }
        
@@ -2003,7 +1963,7 @@ static int cmd_rename(void)
 /****************************************************************************
 toggle the prompt flag
 ****************************************************************************/
-static int cmd_prompt(void)
+static int cmd_prompt(const char **cmd_ptr)
 {
        prompt = !prompt;
        DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
@@ -2015,13 +1975,13 @@ static int cmd_prompt(void)
 /****************************************************************************
 set the newer than time
 ****************************************************************************/
-static int cmd_newer(void)
+static int cmd_newer(const char **cmd_ptr)
 {
        fstring buf;
        BOOL ok;
        SMB_STRUCT_STAT sbuf;
 
-       ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
+       ok = next_token(cmd_ptr,buf,NULL,sizeof(buf));
        if (ok && (sys_stat(buf,&sbuf) == 0)) {
                newer_than = sbuf.st_mtime;
                DEBUG(1,("Getting files newer than %s",
@@ -2041,11 +2001,11 @@ static int cmd_newer(void)
 /****************************************************************************
 set the archive level
 ****************************************************************************/
-static int cmd_archive(void)
+static int cmd_archive(const char **cmd_ptr)
 {
        fstring buf;
 
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                archive_level = atoi(buf);
        } else
                d_printf("Archive level is %d\n",archive_level);
@@ -2056,7 +2016,7 @@ static int cmd_archive(void)
 /****************************************************************************
 toggle the lowercaseflag
 ****************************************************************************/
-static int cmd_lowercase(void)
+static int cmd_lowercase(const char **cmd_ptr)
 {
        lowercase = !lowercase;
        DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
@@ -2070,7 +2030,7 @@ static int cmd_lowercase(void)
 /****************************************************************************
 toggle the recurse flag
 ****************************************************************************/
-static int cmd_recurse(void)
+static int cmd_recurse(const char **cmd_ptr)
 {
        recurse = !recurse;
        DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
@@ -2081,7 +2041,7 @@ static int cmd_recurse(void)
 /****************************************************************************
 toggle the translate flag
 ****************************************************************************/
-static int cmd_translate(void)
+static int cmd_translate(const char **cmd_ptr)
 {
        translation = !translation;
        DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
@@ -2094,12 +2054,12 @@ static int cmd_translate(void)
 /****************************************************************************
 do a printmode command
 ****************************************************************************/
-static int cmd_printmode(void)
+static int cmd_printmode(const char **cmd_ptr)
 {
        fstring buf;
        fstring mode;
 
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                if (strequal(buf,"text")) {
                        printmode = 0;      
                } else {
@@ -2131,12 +2091,12 @@ static int cmd_printmode(void)
 /****************************************************************************
  do the lcd command
  ****************************************************************************/
-static int cmd_lcd(void)
+static int cmd_lcd(const char **cmd_ptr)
 {
        fstring buf;
        pstring d;
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf)))
                chdir(buf);
        DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
 
@@ -2146,7 +2106,7 @@ static int cmd_lcd(void)
 /****************************************************************************
  get a file restarting at end of local file
  ****************************************************************************/
-static int cmd_reget(void)
+static int cmd_reget(const char **cmd_ptr)
 {
        pstring local_name;
        pstring remote_name;
@@ -2157,14 +2117,14 @@ static int cmd_reget(void)
        
        p = remote_name + strlen(remote_name);
        
-       if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
+       if (!next_token(cmd_ptr, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
                d_printf("reget <filename>\n");
                return 1;
        }
        pstrcpy(local_name, p);
        dos_clean_name(remote_name);
        
-       next_token_nr(NULL, local_name, NULL, sizeof(local_name));
+       next_token(cmd_ptr, local_name, NULL, sizeof(local_name));
        
        return do_get(remote_name, local_name, True);
 }
@@ -2172,7 +2132,7 @@ static int cmd_reget(void)
 /****************************************************************************
  put a file restarting at end of local file
  ****************************************************************************/
-static int cmd_reput(void)
+static int cmd_reput(const char **cmd_ptr)
 {
        pstring local_name;
        pstring remote_name;
@@ -2183,7 +2143,7 @@ static int cmd_reput(void)
        pstrcpy(remote_name, cur_dir);
        pstrcat(remote_name, "\\");
   
-       if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
+       if (!next_token(cmd_ptr, p, NULL, sizeof(buf))) {
                d_printf("reput <filename>\n");
                return 1;
        }
@@ -2194,7 +2154,7 @@ static int cmd_reput(void)
                return 1;
        }
 
-       if (next_token_nr(NULL, p, NULL, sizeof(buf)))
+       if (next_token(cmd_ptr, p, NULL, sizeof(buf)))
                pstrcat(remote_name, p);
        else
                pstrcat(remote_name, local_name);
@@ -2208,14 +2168,16 @@ static int cmd_reput(void)
 /*
   return a string representing a share type
 */
-static const char *share_type_str(uint32 type)
+static const char *share_type_str(uint32_t type)
 {
-       switch (type & 0x3) {
-       case 0
+       switch (type & 0xF) {
+       case STYPE_DISKTREE
                return "Disk";
-       case 1
+       case STYPE_PRINTQ
                return "Printer";
-       case 3: 
+       case STYPE_DEVICE: 
+               return "Device";
+       case STYPE_IPC: 
                return "IPC";
        default:
                return "Unknown";
@@ -2251,7 +2213,7 @@ static BOOL browse_host(const char *query_host)
        char *binding;
        NTSTATUS status;
        struct srvsvc_NetShareEnumAll r;
-       uint32 resume_handle = 0;
+       uint32_t resume_handle = 0;
        TALLOC_CTX *mem_ctx = talloc_init("browse_host");
        struct srvsvc_NetShareCtr1 ctr1;
 
@@ -2260,7 +2222,7 @@ static BOOL browse_host(const char *query_host)
        status = dcerpc_pipe_connect(&p, binding, 
                                     DCERPC_SRVSVC_UUID, 
                                     DCERPC_SRVSVC_VERSION,
-                                    lp_workgroup()
+                                    domain
                                     username, password);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Failed to connect to %s - %s\n", 
@@ -2268,17 +2230,16 @@ static BOOL browse_host(const char *query_host)
                talloc_destroy(mem_ctx);
                return False;
        }
+       talloc_steal(mem_ctx, p);
 
        r.in.server_unc = talloc_asprintf(mem_ctx,"\\\\%s",dcerpc_server_name(p));
        r.in.level = 1;
        r.in.ctr.ctr1 = &ctr1;
        r.in.max_buffer = ~0;
        r.in.resume_handle = &resume_handle;
-       r.out.resume_handle = &resume_handle;
-       r.out.ctr.ctr1 = &ctr1;
 
-       d_printf("\n\tSharename       Type      Comment\n");
-       d_printf("\t---------       ----      -------\n");
+       d_printf("\n\tSharename       Type       Comment\n");
+       d_printf("\t---------       ----       -------\n");
 
        do {
                ZERO_STRUCT(ctr1);
@@ -2293,10 +2254,11 @@ static BOOL browse_host(const char *query_host)
                }
        } while (NT_STATUS_IS_OK(status) && W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
 
+       talloc_destroy(mem_ctx);
+
        if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(r.out.result)) {
                d_printf("Failed NetShareEnumAll %s - %s/%s\n", 
                         binding, nt_errstr(status), win_errstr(r.out.result));
-               talloc_destroy(mem_ctx);
                return False;
        }
 
@@ -2306,7 +2268,7 @@ static BOOL browse_host(const char *query_host)
 /****************************************************************************
 try and browse available connections on a host
 ****************************************************************************/
-static BOOL list_servers(char *wk_grp)
+static BOOL list_servers(const char *wk_grp)
 {
        d_printf("REWRITE: list servers not implemented\n");
        return False;
@@ -2326,7 +2288,7 @@ static BOOL list_servers(char *wk_grp)
 static struct
 {
   const char *name;
-  int (*fn)(void);
+  int (*fn)(const char **cmd_ptr);
   const char *description;
   char compl_args[2];      /* Completion argument info */
 } commands[] = 
@@ -2403,7 +2365,7 @@ static int process_tok(fstring tok)
                        matches = 1;
                        cmd = i;
                        break;
-               } else if (strnequal(commands[i].name, tok, tok_len)) {
+               } else if (strncasecmp(commands[i].name, tok, tok_len) == 0) {
                        matches++;
                        cmd = i;
                }
@@ -2421,12 +2383,12 @@ static int process_tok(fstring tok)
 /****************************************************************************
 help
 ****************************************************************************/
-static int cmd_help(void)
+static int cmd_help(const char **cmd_ptr)
 {
        int i=0,j;
        fstring buf;
        
-       if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
+       if (next_token(cmd_ptr,buf,NULL,sizeof(buf))) {
                if ((i = process_tok(buf)) >= 0)
                        d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
        } else {
@@ -2476,10 +2438,10 @@ static int process_command_string(char *cmd)
                
                /* and get the first part of the command */
                ptr = line;
-               if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
+               if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
                
                if ((i = process_tok(tok)) >= 0) {
-                       rc = commands[i].fn();
+                       rc = commands[i].fn(&ptr);
                } else if (i == -2) {
                        d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
@@ -2563,7 +2525,7 @@ static char **remote_completion(const char *text, int len)
        } else
                snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
 
-       if (cli_list(cli->tree, dirmask, 
+       if (smbcli_list(cli->tree, dirmask, 
                     FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN, 
                     completion_remote_filter, &info) < 0)
                goto cleanup;
@@ -2677,8 +2639,6 @@ make sure we swallow keepalives during idle time
 ****************************************************************************/
 static void readline_callback(void)
 {
-       fd_set fds;
-       struct timeval timeout;
        static time_t last_t;
        time_t t;
 
@@ -2688,31 +2648,10 @@ static void readline_callback(void)
 
        last_t = t;
 
- again:
-       if (cli->transport->socket->fd == -1)
-               return;
-
-       FD_ZERO(&fds);
-       FD_SET(cli->transport->socket->fd, &fds);
-
-       timeout.tv_sec = 0;
-       timeout.tv_usec = 0;
-       sys_select_intr(cli->transport->socket->fd+1,&fds,NULL,NULL,&timeout);
-               
-       /* We deliberately use cli_swallow_keepalives instead of
-          client_receive_smb as we want to receive
-          session keepalives and then drop them here.
-       */
-       if (FD_ISSET(cli->transport->socket->fd, &fds)) {
-               if (!cli_request_receive_next(cli->transport)) {
-                       d_printf("Lost connection to server\n");
-                       exit(1);
-               }
-               goto again;
-       }
+       smbcli_transport_process(cli->transport);
 
        if (cli->tree) {
-               cli_chkpath(cli->tree, "\\");
+               smbcli_chkpath(cli->tree, "\\");
        }
 }
 
@@ -2722,13 +2661,12 @@ process commands on stdin
 ****************************************************************************/
 static void process_stdin(void)
 {
-       const char *ptr;
-
        while (1) {
                fstring tok;
                fstring the_prompt;
                char *cline;
                pstring line;
+               const char *ptr;
                int i;
                
                /* display a prompt */
@@ -2747,10 +2685,10 @@ static void process_stdin(void)
       
                /* and get the first part of the command */
                ptr = line;
-               if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
+               if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
 
                if ((i = process_tok(tok)) >= 0) {
-                       commands[i].fn();
+                       commands[i].fn(&ptr);
                } else if (i == -2) {
                        d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
@@ -2763,12 +2701,11 @@ static void process_stdin(void)
 /***************************************************** 
 return a connection to a server
 *******************************************************/
-static struct cli_state *do_connect(const char *server, const char *share)
+static struct smbcli_state *do_connect(const char *server, const char *share)
 {
-       struct cli_state *c;
+       struct smbcli_state *c;
        struct nmb_name called, calling;
        const char *server_n;
-       struct in_addr ip;
        fstring servicename;
        char *sharename;
        NTSTATUS status;
@@ -2786,28 +2723,23 @@ static struct cli_state *do_connect(const char *server, const char *share)
 
        asprintf(&sharename, "\\\\%s\\%s", server, sharename);
 
-       server_n = server;
+       server_n = dest_ip?dest_ip:server;
        
-       zero_ip(&ip);
-
        make_nmb_name(&calling, lp_netbios_name(), 0x0);
-       make_nmb_name(&called , server, name_type);
+       choose_called_name(&called, server, name_type);
 
  again:
-       zero_ip(&ip);
-       if (have_ip) ip = dest_ip;
-
        /* have to open a new connection */
-       if (!(c=cli_state_init()) || !cli_socket_connect(c, server_n, &ip)) {
+       if (!(c=smbcli_state_init(NULL)) || !smbcli_socket_connect(c, server_n)) {
                d_printf("Connection to %s failed\n", server_n);
                return NULL;
        }
 
-       if (!cli_transport_establish(c, &calling, &called)) {
+       if (!smbcli_transport_establish(c, &calling, &called)) {
                char *p;
-               d_printf("session request to %s failed (%s)\n", 
-                        called.name, cli_errstr(c->tree));
-               cli_shutdown(c);
+               d_printf("session request to %s failed\n", 
+                        called.name);
+               smbcli_shutdown(c);
                if ((p=strchr_m(called.name, '.'))) {
                        *p = 0;
                        goto again;
@@ -2821,28 +2753,22 @@ static struct cli_state *do_connect(const char *server, const char *share)
 
        DEBUG(4,(" session request ok\n"));
 
-       if (NT_STATUS_IS_ERR(cli_negprot(c))) {
+       if (NT_STATUS_IS_ERR(smbcli_negprot(c))) {
                d_printf("protocol negotiation failed\n");
-               cli_shutdown(c);
+               smbcli_shutdown(c);
                return NULL;
        }
 
-       if (!got_pass) {
-               const char *pass = getpass("Password: ");
-               if (pass) {
-                       pstrcpy(password, pass);
-               }
-       }
-
-       status = cli_session_setup(c, username, password, lp_workgroup());
+       status = smbcli_session_setup(c, username, password, domain);
        if (NT_STATUS_IS_ERR(status)) {
+               d_printf("authenticated session setup failed: %s\n", nt_errstr(status));
                /* if a password was not supplied then try again with a null username */
-               if (password[0] || !username[0] || use_kerberos) {
-                       status = cli_session_setup(c, "", "", lp_workgroup());
+               if (password[0] || !username[0]) {
+                       status = smbcli_session_setup(c, "", "", lp_workgroup());
                }
                if (NT_STATUS_IS_ERR(status)) {
                        d_printf("session setup failed: %s\n", nt_errstr(status));
-                       cli_shutdown(c);
+                       smbcli_shutdown(c);
                        return NULL;
                }
                d_printf("Anonymous login successful\n");
@@ -2850,9 +2776,9 @@ static struct cli_state *do_connect(const char *server, const char *share)
 
        DEBUG(4,(" session setup ok\n"));
 
-       if (NT_STATUS_IS_ERR(cli_send_tconX(c, sharename, "?????", password))) {
-               d_printf("tree connect failed: %s\n", cli_errstr(c->tree));
-               cli_shutdown(c);
+       if (NT_STATUS_IS_ERR(smbcli_send_tconX(c, sharename, "?????", password))) {
+               d_printf("tree connect failed: %s\n", smbcli_errstr(c->tree));
+               smbcli_shutdown(c);
                return NULL;
        }
 
@@ -2882,7 +2808,7 @@ static int process(char *base_directory)
                process_stdin();
        }
   
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
        return rc;
 }
 
@@ -2891,15 +2817,8 @@ handle a -L query
 ****************************************************************************/
 static int do_host_query(char *query_host)
 {
-       cli = do_connect(query_host, "IPC$");
-       if (!cli)
-               return 1;
-
        browse_host(query_host);
        list_servers(lp_workgroup());
-
-       cli_shutdown(cli);
-       
        return(0);
 }
 
@@ -2924,7 +2843,7 @@ static int do_tar_op(char *base_directory)
        
        ret=process_tar();
 
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
 
        return(ret);
 }
@@ -2934,34 +2853,27 @@ handle a message operation
 ****************************************************************************/
 static int do_message_op(void)
 {
-       struct in_addr ip;
        struct nmb_name called, calling;
-       fstring server_name;
-       char name_type_hex[10];
+       const char *server_name;
 
        make_nmb_name(&calling, lp_netbios_name(), 0x0);
-       make_nmb_name(&called , desthost, name_type);
-
-       fstrcpy(server_name, desthost);
-       snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
-       fstrcat(server_name, name_type_hex);
+       choose_called_name(&called, desthost, name_type);
 
-       zero_ip(&ip);
-       if (have_ip) ip = dest_ip;
+       server_name = dest_ip ? dest_ip : desthost;
 
-       if (!(cli=cli_state_init()) || !cli_socket_connect(cli, server_name, &ip)) {
-               d_printf("Connection to %s failed\n", desthost);
+       if (!(cli=smbcli_state_init(NULL)) || !smbcli_socket_connect(cli, server_name)) {
+               d_printf("Connection to %s failed\n", server_name);
                return 1;
        }
 
-       if (!cli_transport_establish(cli, &calling, &called)) {
+       if (!smbcli_transport_establish(cli, &calling, &called)) {
                d_printf("session request failed\n");
-               cli_shutdown(cli);
+               smbcli_shutdown(cli);
                return 1;
        }
 
        send_message();
-       cli_shutdown(cli);
+       smbcli_shutdown(cli);
 
        return 0;
 }
@@ -3006,13 +2918,11 @@ static void remember_query_host(const char *arg,
        struct poptOption long_options[] = {
                POPT_AUTOHELP
 
-               { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
                { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
                { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
                { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
                { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
                { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
-               { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
                { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
                { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
                { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" }, 
@@ -3021,6 +2931,7 @@ static void remember_query_host(const char *arg,
                POPT_COMMON_SAMBA
                POPT_COMMON_CONNECTION
                POPT_COMMON_CREDENTIALS
+               POPT_COMMON_VERSION
                POPT_TABLEEND
        };
        
@@ -3046,9 +2957,8 @@ static void remember_query_host(const char *arg,
                        argv[0], dyn_CONFIGFILE);
        }
        
-       pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
-                               POPT_CONTEXT_KEEP_FIRST);
-       poptSetOtherOptionHelp(pc, "service <password>");
+       pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
+       poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
 
        in_client = True;   /* Make sure that we tell lp_load we are */
 
@@ -3065,12 +2975,7 @@ static void remember_query_host(const char *arg,
                        message = True;
                        break;
                case 'I':
-                       {
-                               dest_ip = *interpret_addr2(mem_ctx, poptGetOptArg(pc));
-                               if (is_zero_ip(dest_ip))
-                                       exit(1);
-                               have_ip = True;
-                       }
+                       dest_ip = poptGetOptArg(pc);
                        break;
                case 'E':
                        setup_logging("client", DEBUG_STDERR);
@@ -3082,12 +2987,6 @@ static void remember_query_host(const char *arg,
                case 't':
                        pstrcpy(term_code, poptGetOptArg(pc));
                        break;
-               case 'm':
-                       lp_set_cmdline("max protocol", poptGetOptArg(pc));
-                       break;
-               case 'R':
-                       lp_set_cmdline("name resolve order", poptGetOptArg(pc));
-                       break;
                case 'T':
                        if (!tar_parseargs(argc, argv, poptGetOptArg(pc), optind)) {
                                poptPrintUsage(pc, stderr, 0);
@@ -3103,8 +3002,6 @@ static void remember_query_host(const char *arg,
                }
        }
 
-       poptGetArg(pc);
-       
        load_interfaces();
 
        if(poptPeekArg(pc)) {
@@ -3120,8 +3017,7 @@ static void remember_query_host(const char *arg,
        }
 
        if (poptPeekArg(pc)) { 
-               cmdline_auth_info.got_pass = True;
-               pstrcpy(cmdline_auth_info.password,poptGetArg(pc));  
+               cmdline_set_userpassword(poptGetArg(pc));
        }
 
        /*init_names(); */
@@ -3133,10 +3029,9 @@ static void remember_query_host(const char *arg,
 
        poptFreeContext(pc);
 
-       pstrcpy(username, cmdline_auth_info.username);
-       pstrcpy(password, cmdline_auth_info.password);
-       use_kerberos = cmdline_auth_info.use_kerberos;
-       got_pass = cmdline_auth_info.got_pass;
+       pstrcpy(username, cmdline_get_username());
+       pstrcpy(domain, cmdline_get_userdomain());
+       pstrcpy(password, cmdline_get_userpassword());
 
        DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION_STRING ) );