I told Jeremy at the CIFS conference that I would sort the command list
[sfrench/samba-autobuild/.git] / source3 / client / client.c
index 479c4f764f99b0862bb54f7e710e6cd13c3a2fd8..448f4314c814040aa74242bf6c4ee68f7aee473e 100644 (file)
@@ -29,7 +29,7 @@
 
 struct cli_state *cli;
 extern BOOL in_client;
-static int port = SMB_PORT;
+static int port = 0;
 pstring cur_dir = "\\";
 pstring cd_path = "";
 static pstring service;
@@ -135,17 +135,17 @@ static int writefile(int f, char *b, int n)
   read from a file with LF->CR/LF translation if appropriate. return the 
   number read. read approx n bytes.
 ****************************************************************************/
-static int readfile(char *b, int size, int n, FILE *f)
+static int readfile(char *b, int n, XFILE *f)
 {
        int i;
        int c;
 
-       if (!translation || (size != 1))
-               return(fread(b,size,n,f));
+       if (!translation)
+               return x_fread(b,1,n,f);
   
        i = 0;
        while (i < (n - 1) && (i < BUFFER_SIZE)) {
-               if ((c = getc(f)) == EOF) {
+               if ((c = x_getc(f)) == EOF) {
                        break;
                }
       
@@ -169,12 +169,12 @@ static void send_message(void)
        int grp_id;
 
        if (!cli_message_start(cli, desthost, username, &grp_id)) {
-               DEBUG(0,("message start: %s\n", cli_errstr(cli)));
+               d_printf("message start: %s\n", cli_errstr(cli));
                return;
        }
 
 
-       printf("Connected. Type your message, ending it with a Control-D\n");
+       d_printf("Connected. Type your message, ending it with a Control-D\n");
 
        while (!feof(stdin) && total_len < 1600) {
                int maxlen = MIN(1600 - total_len,127);
@@ -191,7 +191,7 @@ static void send_message(void)
                }
 
                if (!cli_message_text(cli, msg, l, grp_id)) {
-                       printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
+                       d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
                        return;
                }      
                
@@ -199,12 +199,12 @@ static void send_message(void)
        }
 
        if (total_len >= 1600)
-               printf("the message was truncated to 1600 bytes\n");
+               d_printf("the message was truncated to 1600 bytes\n");
        else
-               printf("sent %d bytes\n",total_len);
+               d_printf("sent %d bytes\n",total_len);
 
        if (!cli_message_end(cli, grp_id)) {
-               printf("SMBsendend failed (%s)\n",cli_errstr(cli));
+               d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
                return;
        }      
 }
@@ -219,12 +219,12 @@ static void do_dskattr(void)
        int total, bsize, avail;
 
        if (!cli_dskattr(cli, &bsize, &total, &avail)) {
-               DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli))); 
+               d_printf("Error in dskattr: %s\n",cli_errstr(cli)); 
                return;
        }
 
-       DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
-                total, bsize, avail));
+       d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
+                total, bsize, avail);
 }
 
 /****************************************************************************
@@ -232,8 +232,8 @@ show cd/pwd
 ****************************************************************************/
 static void cmd_pwd(void)
 {
-       DEBUG(0,("Current directory is %s",service));
-       DEBUG(0,("%s\n",cur_dir));
+       d_printf("Current directory is %s",service);
+       d_printf("%s\n",cur_dir);
 }
 
 
@@ -265,7 +265,7 @@ static void do_cd(char *newdir)
        
        if (!strequal(cur_dir,"\\")) {
                if (!cli_chkpath(cli, dname)) {
-                       DEBUG(0,("cd %s: %s\n", dname, cli_errstr(cli)));
+                       d_printf("cd %s: %s\n", dname, cli_errstr(cli));
                        pstrcpy(cur_dir,saved_dir);
                }
        }
@@ -283,7 +283,7 @@ static void cmd_cd(void)
        if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
                do_cd(buf);
        else
-               DEBUG(0,("Current directory is %s\n",cur_dir));
+               d_printf("Current directory is %s\n",cur_dir);
 }
 
 
@@ -320,11 +320,11 @@ 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 */
-               DEBUG(0,("  %-30s%7.7s %8.0f  %s",
+               d_printf("  %-30s%7.7s %8.0f  %s",
                         finfo->name,
                         attrib_string(finfo->mode),
                         (double)finfo->size,
-                        asctime(LocalTime(&t))));
+                        asctime(LocalTime(&t)));
                dir_total += finfo->size;
        }
 }
@@ -365,11 +365,7 @@ functions for do_list_queue
  */
 static void reset_do_list_queue(void)
 {
-       if (do_list_queue)
-       {
-               free(do_list_queue);
-       }
-       do_list_queue = 0;
+       SAFE_FREE(do_list_queue);
        do_list_queue_size = 0;
        do_list_queue_start = 0;
        do_list_queue_end = 0;
@@ -381,8 +377,8 @@ static void init_do_list_queue(void)
        do_list_queue_size = 1024;
        do_list_queue = malloc(do_list_queue_size);
        if (do_list_queue == 0) { 
-               DEBUG(0,("malloc fail for size %d\n",
-                        (int)do_list_queue_size));
+               d_printf("malloc fail for size %d\n",
+                        (int)do_list_queue_size);
                reset_do_list_queue();
        } else {
                memset(do_list_queue, 0, do_list_queue_size);
@@ -415,20 +411,22 @@ static void adjust_do_list_queue(void)
 
 static void add_to_do_list_queue(const char* entry)
 {
+       char *dlq;
        long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
        while (new_end > do_list_queue_size)
        {
                do_list_queue_size *= 2;
                DEBUG(4,("enlarging do_list_queue to %d\n",
                         (int)do_list_queue_size));
-               do_list_queue = Realloc(do_list_queue, do_list_queue_size);
-               if (! do_list_queue) {
-                       DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
-                                (int)do_list_queue_size));
+               dlq = Realloc(do_list_queue, do_list_queue_size);
+               if (! dlq) {
+                       d_printf("failure enlarging do_list_queue to %d bytes\n",
+                                (int)do_list_queue_size);
                        reset_do_list_queue();
                }
                else
                {
+                       do_list_queue = dlq;
                        memset(do_list_queue + do_list_queue_size / 2,
                               0, do_list_queue_size / 2);
                }
@@ -544,7 +542,7 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
                                                strlen(next_file) - 2;
                                        *save_ch = '\0';
                                }
-                               DEBUG(0,("\n%s\n",next_file));
+                               d_printf("\n%s\n",next_file);
                                if (save_ch)
                                {
                                        *save_ch = '\\';
@@ -556,7 +554,7 @@ void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec,
        {
                if (cli_list(cli, mask, attribute, do_list_helper, NULL) == -1)
                {
-                       DEBUG(0, ("%s listing %s\n", cli_errstr(cli), mask));
+                       d_printf("%s listing %s\n", cli_errstr(cli), mask);
                }
        }
 
@@ -627,7 +625,7 @@ static void cmd_du(void)
 
        do_dskattr();
 
-       DEBUG(0, ("Total number of bytes: %.0f\n", dir_total));
+       d_printf("Total number of bytes: %.0f\n", dir_total);
 }
 
 
@@ -654,7 +652,7 @@ static void do_get(char *rname,char *lname)
        fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
 
        if (fnum == -1) {
-               DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
+               d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
                return;
        }
 
@@ -665,7 +663,7 @@ static void do_get(char *rname,char *lname)
                newhandle = True;
        }
        if (handle < 0) {
-               DEBUG(0,("Error opening local file %s\n",lname));
+               d_printf("Error opening local file %s\n",lname);
                return;
        }
 
@@ -674,7 +672,7 @@ static void do_get(char *rname,char *lname)
                           &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
            !cli_getattrE(cli, fnum, 
                          &attr, &size, NULL, NULL, NULL)) {
-               DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
+               d_printf("getattrib: %s\n",cli_errstr(cli));
                return;
        }
 
@@ -682,7 +680,7 @@ static void do_get(char *rname,char *lname)
                 lname, (double)size, lname));
 
        if(!(data = (char *)malloc(read_size))) { 
-               DEBUG(0,("malloc fail for size %d\n", read_size));
+               d_printf("malloc fail for size %d\n", read_size);
                cli_close(cli, fnum);
                return;
        }
@@ -693,7 +691,7 @@ static void do_get(char *rname,char *lname)
                if (n <= 0) break;
  
                if (writefile(handle,data, n) != n) {
-                       DEBUG(0,("Error writing local file\n"));
+                       d_printf("Error writing local file\n");
                        break;
                }
       
@@ -705,10 +703,10 @@ static void do_get(char *rname,char *lname)
                rname, (long)nread));
        }
 
-       free(data);
+       SAFE_FREE(data);
        
        if (!cli_close(cli, fnum)) {
-               DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
+               d_printf("Error %s closing remote file\n",cli_errstr(cli));
        }
 
        if (newhandle) {
@@ -752,7 +750,7 @@ static void cmd_get(void)
        p = rname + strlen(rname);
        
        if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
-               DEBUG(0,("get <filename>\n"));
+               d_printf("get <filename>\n");
                return;
        }
        pstrcpy(lname,p);
@@ -778,7 +776,7 @@ static void do_mget(file_info *finfo)
                return;
 
        if (abort_mget) {
-               DEBUG(0,("mget aborted\n"));
+               d_printf("mget aborted\n");
                return;
        }
 
@@ -810,13 +808,13 @@ static void do_mget(file_info *finfo)
        
        if (!directory_exist(finfo->name,NULL) && 
            mkdir(finfo->name,0777) != 0) {
-               DEBUG(0,("failed to create directory %s\n",finfo->name));
+               d_printf("failed to create directory %s\n",finfo->name);
                pstrcpy(cur_dir,saved_curdir);
                return;
        }
        
        if (chdir(finfo->name) != 0) {
-               DEBUG(0,("failed to chdir to directory %s\n",finfo->name));
+               d_printf("failed to chdir to directory %s\n",finfo->name);
                pstrcpy(cur_dir,saved_curdir);
                return;
        }
@@ -845,13 +843,13 @@ static void cmd_more(void)
        slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
        fd = smb_mkstemp(lname);
        if (fd == -1) {
-               DEBUG(0,("failed to create temporary file for more\n"));
+               d_printf("failed to create temporary file for more\n");
                return;
        }
        close(fd);
 
        if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
-               DEBUG(0,("more <filename>\n"));
+               d_printf("more <filename>\n");
                unlink(lname);
                return;
        }
@@ -914,8 +912,8 @@ make a directory of name "name"
 static BOOL do_mkdir(char *name)
 {
        if (!cli_mkdir(cli, name)) {
-               DEBUG(0,("%s making remote directory %s\n",
-                        cli_errstr(cli),name));
+               d_printf("%s making remote directory %s\n",
+                        cli_errstr(cli),name);
                return(False);
        }
 
@@ -946,7 +944,7 @@ static void cmd_mkdir(void)
 
        if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
                if (!recurse)
-                       DEBUG(0,("mkdir <dirname>\n"));
+                       d_printf("mkdir <dirname>\n");
                return;
        }
        pstrcat(mask,p);
@@ -979,7 +977,7 @@ static void cmd_mkdir(void)
 static void do_put(char *rname,char *lname)
 {
        int fnum;
-       FILE *f;
+       XFILE *f;
        int nread=0;
        char *buf=NULL;
        int maxwrite=io_bufsize;
@@ -990,21 +988,21 @@ static void do_put(char *rname,char *lname)
        fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
   
        if (fnum == -1) {
-               DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
+               d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
                return;
        }
 
        /* allow files to be piped into smbclient
           jdblair 24.jun.98 */
        if (!strcmp(lname, "-")) {
-               f = stdin;
+               f = x_stdin;
                /* size of file is not known */
        } else {
-               f = sys_fopen(lname,"r");
+               f = x_fopen(lname,O_RDONLY, 0);
        }
 
        if (!f) {
-               DEBUG(0,("Error opening local file %s\n",lname));
+               d_printf("Error opening local file %s\n",lname);
                return;
        }
 
@@ -1013,22 +1011,26 @@ static void do_put(char *rname,char *lname)
                 rname));
   
        buf = (char *)malloc(maxwrite);
-       while (!feof(f)) {
+       if (!buf) {
+               d_printf("ERROR: Not enough memory!\n");
+               return;
+       }
+       while (!x_feof(f)) {
                int n = maxwrite;
                int ret;
 
-               if ((n = readfile(buf,1,n,f)) < 1) {
-                       if((n == 0) && feof(f))
+               if ((n = readfile(buf,n,f)) < 1) {
+                       if((n == 0) && x_feof(f))
                                break; /* Empty local file. */
 
-                       DEBUG(0,("Error reading local file: %s\n", strerror(errno) ));
+                       d_printf("Error reading local file: %s\n", strerror(errno));
                        break;
                }
 
                ret = cli_write(cli, fnum, 0, buf, nread, n);
 
                if (n != ret) {
-                       DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
+                       d_printf("Error writing file: %s\n", cli_errstr(cli));
                        break;
                } 
 
@@ -1036,15 +1038,15 @@ static void do_put(char *rname,char *lname)
        }
 
        if (!cli_close(cli, fnum)) {
-               DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),rname));
-               fclose(f);
-               if (buf) free(buf);
+               d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
+               x_fclose(f);
+               SAFE_FREE(buf);
                return;
        }
 
        
-       fclose(f);
-       if (buf) free(buf);
+       x_fclose(f);
+       SAFE_FREE(buf);
 
        {
                struct timeval tp_end;
@@ -1062,7 +1064,7 @@ static void do_put(char *rname,char *lname)
                         put_total_size / (1.024*put_total_time_ms)));
        }
 
-       if (f == stdin) {
+       if (f == x_stdin) {
                cli_shutdown(cli);
                exit(0);
        }
@@ -1084,7 +1086,7 @@ static void cmd_put(void)
        pstrcat(rname,"\\");
   
        if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
-               DEBUG(0,("put <filename>\n"));
+               d_printf("put <filename>\n");
                return;
        }
        pstrcpy(lname,p);
@@ -1102,7 +1104,7 @@ static void cmd_put(void)
                   jdblair, 24.jun.98 */
                if (!file_exist(lname,&st) &&
                    (strcmp(lname,"-"))) {
-                       DEBUG(0,("%s does not exist\n",lname));
+                       d_printf("%s does not exist\n",lname);
                        return;
                }
        }
@@ -1132,8 +1134,8 @@ static void free_file_list (struct file_list * list)
        {
                tmp = list;
                DLIST_REMOVE(list, list);
-               if (tmp->file_path) free(tmp->file_path);
-               free(tmp);
+               SAFE_FREE(tmp->file_path);
+               SAFE_FREE(tmp);
        }
 }
 
@@ -1190,7 +1192,7 @@ static int file_find(struct file_list **list, const char *directory,
                }
 
                isdir = False;
-               if (!match || !ms_fnmatch(expression, dname)) {
+               if (!match || !gen_fnmatch(expression, dname)) {
                        if (recurse) {
                                ret = stat(path, &statbuf);
                                if (ret == 0) {
@@ -1199,18 +1201,18 @@ static int file_find(struct file_list **list, const char *directory,
                                                ret = file_find(list, path, expression, False);
                                        }
                                } else {
-                                       DEBUG(0,("file_find: cannot stat file %s\n", path));
+                                       d_printf("file_find: cannot stat file %s\n", path);
                                }
                                
                                if (ret == -1) {
-                                       free(path);
+                                       SAFE_FREE(path);
                                        closedir(dir);
                                        return -1;
                                }
                        }
                        entry = (struct file_list *) malloc(sizeof (struct file_list));
                        if (!entry) {
-                               DEBUG(0,("Out of memory in file_find\n"));
+                               d_printf("Out of memory in file_find\n");
                                closedir(dir);
                                return -1;
                        }
@@ -1218,7 +1220,7 @@ static int file_find(struct file_list **list, const char *directory,
                        entry->isdir = isdir;
                         DLIST_ADD(*list, entry);
                } else {
-                       free(path);
+                       SAFE_FREE(path);
                }
         }
 
@@ -1254,7 +1256,7 @@ static void cmd_mput(void)
                for (temp_list = file_list; temp_list; 
                     temp_list = temp_list->next) {
 
-                       if (lname) free(lname);
+                       SAFE_FREE(lname);
                        if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
                                continue;
                        trim_string(lname, "./", "/");
@@ -1263,7 +1265,7 @@ static void cmd_mput(void)
                        if (temp_list->isdir) {
                                /* if (!recurse) continue; */
                                
-                               if (quest) free(quest);
+                               SAFE_FREE(quest);
                                asprintf(&quest, "Put directory %s? ", lname);
                                if (prompt && !yesno(quest)) { /* No */
                                        /* Skip the directory */
@@ -1271,7 +1273,7 @@ static void cmd_mput(void)
                                        if (!seek_list(temp_list, lname))
                                                break;              
                                } else { /* Yes */
-                                       if (rname) free(rname);
+                                       SAFE_FREE(rname);
                                        asprintf(&rname, "%s%s", cur_dir, lname);
                                        dos_format(rname);
                                        if (!cli_chkpath(cli, rname) && 
@@ -1285,13 +1287,13 @@ static void cmd_mput(void)
                                }
                                continue;
                        } else {
-                               if (quest) free(quest);
+                               SAFE_FREE(quest);
                                asprintf(&quest,"Put file %s? ", lname);
                                if (prompt && !yesno(quest)) /* No */
                                        continue;
                                
                                /* Yes */
-                               if (rname) free(rname);
+                               SAFE_FREE(rname);
                                asprintf(&rname, "%s%s", cur_dir, lname);
                        }
 
@@ -1300,9 +1302,9 @@ static void cmd_mput(void)
                        do_put(rname, lname);
                }
                free_file_list(file_list);
-               if (quest) free(quest);
-               if (lname) free(lname);
-               if (rname) free(rname);
+               SAFE_FREE(quest);
+               SAFE_FREE(lname);
+               SAFE_FREE(rname);
        }
 }
 
@@ -1313,9 +1315,9 @@ static void cmd_mput(void)
 static void do_cancel(int job)
 {
        if (cli_printjob_del(cli, job)) {
-               printf("Job %d cancelled\n",job);
+               d_printf("Job %d cancelled\n",job);
        } else {
-               printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
+               d_printf("Error cancelling job %d : %s\n",job,cli_errstr(cli));
        }
 }
 
@@ -1329,7 +1331,7 @@ static void cmd_cancel(void)
        int job; 
 
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
-               printf("cancel <jobid> ...\n");
+               d_printf("cancel <jobid> ...\n");
                return;
        }
        do {
@@ -1349,7 +1351,7 @@ static void cmd_print(void)
        char *p;
 
        if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
-               DEBUG(0,("print <filename>\n"));
+               d_printf("print <filename>\n");
                return;
        }
 
@@ -1372,7 +1374,7 @@ static void cmd_print(void)
 ****************************************************************************/
 static void queue_fn(struct print_job_info *p)
 {
-       DEBUG(0,("%-6d   %-9d    %s\n", (int)p->id, (int)p->size, p->name));
+       d_printf("%-6d   %-9d    %s\n", (int)p->id, (int)p->size, p->name);
 }
 
 /****************************************************************************
@@ -1397,7 +1399,7 @@ static void do_del(file_info *finfo)
                return;
 
        if (!cli_unlink(cli, mask)) {
-               DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),mask));
+               d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
        }
 }
 
@@ -1416,7 +1418,7 @@ static void cmd_del(void)
        pstrcpy(mask,cur_dir);
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
-               DEBUG(0,("del <filename>\n"));
+               d_printf("del <filename>\n");
                return;
        }
        pstrcat(mask,buf);
@@ -1434,7 +1436,7 @@ static void cmd_open(void)
        pstrcpy(mask,cur_dir);
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
-               DEBUG(0,("del <filename>\n"));
+               d_printf("del <filename>\n");
                return;
        }
        pstrcat(mask,buf);
@@ -1454,14 +1456,14 @@ static void cmd_rmdir(void)
        pstrcpy(mask,cur_dir);
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
-               DEBUG(0,("rmdir <dirname>\n"));
+               d_printf("rmdir <dirname>\n");
                return;
        }
        pstrcat(mask,buf);
 
        if (!cli_rmdir(cli, mask)) {
-               DEBUG(0,("%s removing remote directory file %s\n",
-                        cli_errstr(cli),mask));
+               d_printf("%s removing remote directory file %s\n",
+                        cli_errstr(cli),mask);
        }  
 }
 
@@ -1478,7 +1480,7 @@ static void cmd_rename(void)
        
        if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
            !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
-               DEBUG(0,("rename <src> <dest>\n"));
+               d_printf("rename <src> <dest>\n");
                return;
        }
 
@@ -1486,7 +1488,7 @@ static void cmd_rename(void)
        pstrcat(dest,buf2);
 
        if (!cli_rename(cli, src, dest)) {
-               DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
+               d_printf("%s renaming files\n",cli_errstr(cli));
                return;
        }  
 }
@@ -1521,7 +1523,7 @@ static void cmd_newer(void)
        }
 
        if (ok && newer_than == 0)
-               DEBUG(0,("Error setting newer-than time\n"));
+               d_printf("Error setting newer-than time\n");
 }
 
 /****************************************************************************
@@ -1534,7 +1536,7 @@ static void cmd_archive(void)
        if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
                archive_level = atoi(buf);
        } else
-               DEBUG(0,("Archive level is %d\n",archive_level));
+               d_printf("Archive level is %d\n",archive_level);
 }
 
 /****************************************************************************
@@ -1638,7 +1640,7 @@ static void browse_fn(const char *name, uint32 m,
           case STYPE_IPC:
             fstrcpy(typestr,"IPC"); break;
         }
-       printf("\t%-15.15s%-10.10s%s\n",
+       d_printf("\t%-15.15s%-10.10s%s\n",
                name,typestr,comment);
 }
 
@@ -1650,11 +1652,11 @@ static BOOL browse_host(BOOL sort)
 {
        int ret;
 
-        printf("\n\tSharename      Type      Comment\n");
-        printf("\t---------      ----      -------\n");
+        d_printf("\n\tSharename      Type      Comment\n");
+        d_printf("\t---------      ----      -------\n");
 
        if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1)
-               printf("Error returning browse list: %s\n", cli_errstr(cli));
+               d_printf("Error returning browse list: %s\n", cli_errstr(cli));
 
        return (ret != -1);
 }
@@ -1665,7 +1667,7 @@ list a server name
 static void server_fn(const char *name, uint32 m, 
                       const char *comment, void *state)
 {
-        printf("\t%-16.16s     %s\n", name, comment);
+        d_printf("\t%-16.16s     %s\n", name, comment);
 }
 
 /****************************************************************************
@@ -1675,13 +1677,13 @@ static BOOL list_servers(char *wk_grp)
 {
        if (!cli->server_domain) return False;
        
-        printf("\n\tServer               Comment\n");
-        printf("\t---------            -------\n");
+        d_printf("\n\tServer               Comment\n");
+        d_printf("\t---------            -------\n");
 
        cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn, NULL);
 
-        printf("\n\tWorkgroup            Master\n");
-        printf("\t---------            -------\n");
+        d_printf("\n\tWorkgroup            Master\n");
+        d_printf("\t---------            -------\n");
 
        cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn, NULL);
        return True;
@@ -1693,7 +1695,11 @@ static BOOL list_servers(char *wk_grp)
 #define COMPL_REMOTE      1          /* Complete remote filename */
 #define COMPL_LOCAL       2          /* Complete local filename */
 
-/* This defines the commands supported by this client */
+/* This defines the commands supported by this client.
+ * NOTE: The "!" must be the last one in the list because it's fn pointer
+ *       field is NULL, and NULL in that field is used in process_tok()
+ *       (below) to indicate the end of the list.  crh
+ */
 struct
 {
   char *name;
@@ -1702,47 +1708,46 @@ struct
   char compl_args[2];      /* Completion argument info */
 } commands[] = 
 {
-  {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
+  {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
+  {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
+  {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
-  {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
-  {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
-  {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
+  {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
+  {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
+  {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
+  {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
+  {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
+  {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
+  {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
-  {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
   {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
-  {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
-  {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
-  {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
-  {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
-  {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
-  {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
-  {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
-  {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
-  {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},  
-  {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
   {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
+  {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
+  {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
+  {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
+  {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
-  {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-  {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-  {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-  {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
-  {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
-  {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
-  {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
-  {"tarmode",cmd_tarmode,
-     "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
+  {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
+  {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
+  {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
   {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
-  {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
+  {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
+  {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
+  {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
-  {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
   {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
   {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
 };
@@ -1788,16 +1793,17 @@ static void cmd_help(void)
        
        if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
                if ((i = process_tok(buf)) >= 0)
-                       DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));                   
+                       d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
        } else {
                while (commands[i].description) {
                        for (j=0; commands[i].description && (j<5); j++) {
-                               DEBUG(0,("%-15s",commands[i].name));
+                               d_printf("%-15s",commands[i].name);
                                i++;
                        }
-                       DEBUG(0,("\n"));
+                       d_printf("\n");
                }
        }
+       return;
 }
 
 /****************************************************************************
@@ -1831,9 +1837,9 @@ static void process_command_string(char *cmd)
                if ((i = process_tok(tok)) >= 0) {
                        commands[i].fn();
                } else if (i == -2) {
-                       DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
+                       d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
-                       DEBUG(0,("%s: command not found\n",tok));
+                       d_printf("%s: command not found\n",tok);
                }
        }
 }      
@@ -1865,7 +1871,7 @@ static char **completion_fn(char *text, int start, int end)
        }
 
        if (count == 2) {
-               free(matches[0]);
+               SAFE_FREE(matches[0]);
                matches[0] = strdup(matches[1]);
        }
        matches[count] = NULL;
@@ -1945,9 +1951,9 @@ static void process_stdin(void)
                if ((i = process_tok(tok)) >= 0) {
                        commands[i].fn();
                } else if (i == -2) {
-                       DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
+                       d_printf("%s: command abbreviation ambiguous\n",tok);
                } else {
-                       DEBUG(0,("%s: command not found\n",tok));
+                       d_printf("%s: command not found\n",tok);
                }
        }
 }
@@ -1989,9 +1995,9 @@ struct cli_state *do_connect(const char *server, const char *share)
        if (have_ip) ip = dest_ip;
 
        /* have to open a new connection */
-       if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
+       if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) != port) ||
            !cli_connect(c, server_n, &ip)) {
-               DEBUG(0,("Connection to %s failed\n", server_n));
+               d_printf("Connection to %s failed\n", server_n);
                return NULL;
        }
 
@@ -1999,10 +2005,10 @@ struct cli_state *do_connect(const char *server, const char *share)
 
        if (!cli_session_request(c, &calling, &called)) {
                char *p;
-               DEBUG(0,("session request to %s failed (%s)\n", 
-                        called.name, cli_errstr(c)));
+               d_printf("session request to %s failed (%s)\n", 
+                        called.name, cli_errstr(c));
                cli_shutdown(c);
-               free(c);
+               SAFE_FREE(c);
                if ((p=strchr_m(called.name, '.'))) {
                        *p = 0;
                        goto again;
@@ -2017,9 +2023,9 @@ struct cli_state *do_connect(const char *server, const char *share)
        DEBUG(4,(" session request ok\n"));
 
        if (!cli_negprot(c)) {
-               DEBUG(0,("protocol negotiation failed\n"));
+               d_printf("protocol negotiation failed\n");
                cli_shutdown(c);
-               free(c);
+               SAFE_FREE(c);
                return NULL;
        }
 
@@ -2037,12 +2043,12 @@ struct cli_state *do_connect(const char *server, const char *share)
                /* if a password was not supplied then try again with a null username */
                if (password[0] || !username[0] || 
                    !cli_session_setup(c, "", "", 0, "", 0, workgroup)) { 
-                       DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
+                       d_printf("session setup failed: %s\n", cli_errstr(c));
                        cli_shutdown(c);
-                       free(c);
+                       SAFE_FREE(c);
                        return NULL;
                }
-               DEBUG(0,("Anonymous login successful\n"));
+               d_printf("Anonymous login successful\n");
        }
 
        /*
@@ -2062,9 +2068,9 @@ struct cli_state *do_connect(const char *server, const char *share)
 
        if (!cli_send_tconX(c, sharename, "?????",
                            password, strlen(password)+1)) {
-               DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
+               d_printf("tree connect failed: %s\n", cli_errstr(c));
                cli_shutdown(c);
-               free(c);
+               SAFE_FREE(c);
                return NULL;
        }
 
@@ -2101,34 +2107,34 @@ usage on the program
 ****************************************************************************/
 static void usage(char *pname)
 {
-  DEBUG(0,("Usage: %s service <password> [options]", pname));
-
-  DEBUG(0,("\nVersion %s\n",VERSION));
-  DEBUG(0,("\t-s smb.conf           pathname to smb.conf file\n"));
-  DEBUG(0,("\t-O socket_options     socket options to use\n"));
-  DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
-  DEBUG(0,("\t-M host               send a winpopup message to the host\n"));
-  DEBUG(0,("\t-i scope              use this NetBIOS scope\n"));
-  DEBUG(0,("\t-N                    don't ask for a password\n"));
-  DEBUG(0,("\t-n netbios name.      Use this name as my netbios name\n"));
-  DEBUG(0,("\t-d debuglevel         set the debuglevel\n"));
-  DEBUG(0,("\t-P                    connect to service as a printer\n"));
-  DEBUG(0,("\t-p port               connect to the specified port\n"));
-  DEBUG(0,("\t-l log basename.      Basename for log/debug files\n"));
-  DEBUG(0,("\t-h                    Print this help message.\n"));
-  DEBUG(0,("\t-I dest IP            use this IP to connect to\n"));
-  DEBUG(0,("\t-E                    write messages to stderr instead of stdout\n"));
-  DEBUG(0,("\t-U username           set the network username\n"));
-  DEBUG(0,("\t-L host               get a list of shares available on a host\n"));
-  DEBUG(0,("\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
-  DEBUG(0,("\t-m max protocol       set the max protocol level\n"));
-  DEBUG(0,("\t-A filename           get the credentials from a file\n"));
-  DEBUG(0,("\t-W workgroup          set the workgroup name\n"));
-  DEBUG(0,("\t-T<c|x>IXFqgbNan      command line tar\n"));
-  DEBUG(0,("\t-D directory          start from directory\n"));
-  DEBUG(0,("\t-c command string     execute semicolon separated commands\n"));
-  DEBUG(0,("\t-b xmit/send buffer   changes the transmit/send buffer (default: 65520)\n"));
-  DEBUG(0,("\n"));
+  d_printf("Usage: %s service <password> [options]", pname);
+
+  d_printf("\nVersion %s\n",VERSION);
+  d_printf("\t-s smb.conf           pathname to smb.conf file\n");
+  d_printf("\t-O socket_options     socket options to use\n");
+  d_printf("\t-R name resolve order use these name resolution services only\n");
+  d_printf("\t-M host               send a winpopup message to the host\n");
+  d_printf("\t-i scope              use this NetBIOS scope\n");
+  d_printf("\t-N                    don't ask for a password\n");
+  d_printf("\t-n netbios name.      Use this name as my netbios name\n");
+  d_printf("\t-d debuglevel         set the debuglevel\n");
+  d_printf("\t-P                    connect to service as a printer\n");
+  d_printf("\t-p port               connect to the specified port\n");
+  d_printf("\t-l log basename.      Basename for log/debug files\n");
+  d_printf("\t-h                    Print this help message.\n");
+  d_printf("\t-I dest IP            use this IP to connect to\n");
+  d_printf("\t-E                    write messages to stderr instead of stdout\n");
+  d_printf("\t-U username           set the network username\n");
+  d_printf("\t-L host               get a list of shares available on a host\n");
+  d_printf("\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
+  d_printf("\t-m max protocol       set the max protocol level\n");
+  d_printf("\t-A filename           get the credentials from a file\n");
+  d_printf("\t-W workgroup          set the workgroup name\n");
+  d_printf("\t-T<c|x>IXFqgbNan      command line tar\n");
+  d_printf("\t-D directory          start from directory\n");
+  d_printf("\t-c command string     execute semicolon separated commands\n");
+  d_printf("\t-b xmit/send buffer   changes the transmit/send buffer (default: 65520)\n");
+  d_printf("\n");
 }
 
 
@@ -2247,13 +2253,13 @@ static int do_message_op(void)
        ip = ipzero;
        if (have_ip) ip = dest_ip;
 
-       if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) == 0) || !cli_connect(cli, desthost, &ip)) {
-               DEBUG(0,("Connection to %s failed\n", desthost));
+       if (!(cli=cli_initialise(NULL)) || (cli_set_port(cli, port) != port) || !cli_connect(cli, desthost, &ip)) {
+               d_printf("Connection to %s failed\n", desthost);
                return 1;
        }
 
        if (!cli_session_request(cli, &calling, &called)) {
-               DEBUG(0,("session request failed\n"));
+               d_printf("session request failed\n");
                cli_shutdown(cli);
                return 1;
        }
@@ -2273,7 +2279,6 @@ static int do_message_op(void)
        fstring base_directory;
        char *pname = argv[0];
        int opt;
-       extern FILE *dbf;
        extern char *optarg;
        extern int optind;
        int old_debug;
@@ -2308,7 +2313,7 @@ static int do_message_op(void)
 
        for (opt = 1; opt < argc; opt++) {
                if (strcmp(argv[opt], "-E") == 0)
-                       dbf = stderr;
+                       dbf = x_stderr;
                else if(strncmp(argv[opt], "-s", 2) == 0) {
                        if(argv[opt][2] != '\0')
                                pstrcpy(servicesf, &argv[opt][2]);
@@ -2394,7 +2399,7 @@ static int do_message_op(void)
                
                if (count_chars(service,'\\') < 3) {
                        usage(pname);
-                       printf("\n%s: Not enough '\\' characters in service\n",service);
+                       d_printf("\n%s: Not enough '\\' characters in service\n",service);
                        exit(1);
                }
 
@@ -2465,7 +2470,8 @@ static int do_message_op(void)
                        }
                        break;
                case 'E':
-                       dbf = stderr;
+                       display_set_stderr();
+                       dbf = x_stderr;
                        break;
                case 'U':
                        {
@@ -2482,22 +2488,22 @@ static int do_message_op(void)
 
                case 'A':
                        {
-                               FILE *auth;
+                               XFILE *auth;
                                fstring buf;
                                uint16 len = 0;
                                char *ptr, *val, *param;
                                
-                               if ((auth=sys_fopen(optarg, "r")) == NULL)
+                               if ((auth=x_fopen(optarg, O_RDONLY, 0)) == NULL)
                                {
                                        /* fail if we can't open the credentials file */
-                                       DEBUG(0,("ERROR: Unable to open credentials file!\n"));
+                                       d_printf("ERROR: Unable to open credentials file!\n");
                                        exit (-1);
                                }
                                 
-                               while (!feof(auth))
+                               while (!x_feof(auth))
                                {  
                                        /* get a line from the file */
-                                       if (!fgets (buf, sizeof(buf), auth))
+                                       if (!x_fgets(buf, sizeof(buf), auth))
                                                continue;
                                        len = strlen(buf);
                                        
@@ -2531,7 +2537,7 @@ static int do_message_op(void)
                                                
                                        memset(buf, 0, sizeof(buf));
                                }
-                               fclose(auth);
+                               x_fclose(auth);
                        }
                        break;