by using a single proto.h we gain another factor of 4 in the speed of
[sfrench/samba-autobuild/.git] / source4 / client / client.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB client
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Simo Sorce 2001-2002
6    Copyright (C) Jelmer Vernooij 2003
7    Copyright (C) James J Myers   2003 <myersjj@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #ifndef REGISTER
26 #define REGISTER 0
27 #endif
28
29 struct cli_state *cli;
30 extern BOOL in_client;
31 static int port = 0;
32 pstring cur_dir = "\\";
33 static pstring cd_path = "";
34 static pstring service;
35 static pstring desthost;
36 static pstring username;
37 static pstring password;
38 static BOOL use_kerberos;
39 static BOOL got_pass;
40 static char *cmdstr = NULL;
41
42 static int io_bufsize = 64512;
43
44 static int name_type = 0x20;
45
46 static int process_tok(fstring tok);
47 static int cmd_help(void);
48
49 /* 30 second timeout on most commands */
50 #define CLIENT_TIMEOUT (30*1000)
51 #define SHORT_TIMEOUT (5*1000)
52
53 /* value for unused fid field in trans2 secondary request */
54 #define FID_UNUSED (0xFFFF)
55
56 time_t newer_than = 0;
57 static int archive_level = 0;
58
59 static BOOL translation = False;
60
61 static BOOL have_ip;
62
63 /* clitar bits insert */
64 extern int blocksize;
65 extern BOOL tar_inc;
66 extern BOOL tar_reset;
67 /* clitar bits end */
68  
69
70 static BOOL prompt = True;
71
72 static int printmode = 1;
73
74 static BOOL recurse = False;
75 BOOL lowercase = False;
76
77 static struct in_addr dest_ip;
78
79 #define SEPARATORS " \t\n\r"
80
81 static BOOL abort_mget = True;
82
83 static pstring fileselection = "";
84
85 extern file_info def_finfo;
86
87 /* timing globals */
88 SMB_BIG_UINT get_total_size = 0;
89 unsigned int get_total_time_ms = 0;
90 static SMB_BIG_UINT put_total_size = 0;
91 static unsigned int put_total_time_ms = 0;
92
93 /* totals globals */
94 static double dir_total;
95
96 #define USENMB
97
98 /* some forward declarations */
99 static struct cli_state *do_connect(const char *server, const char *share);
100
101
102 /*******************************************************************
103  Reduce a file name, removing .. elements.
104 ********************************************************************/
105 void dos_clean_name(char *s)
106 {
107         char *p=NULL;
108
109         DEBUG(3,("dos_clean_name [%s]\n",s));
110
111         /* remove any double slashes */
112         all_string_sub(s, "\\\\", "\\", 0);
113
114         while ((p = strstr(s,"\\..\\")) != NULL) {
115                 pstring s1;
116
117                 *p = 0;
118                 pstrcpy(s1,p+3);
119
120                 if ((p=strrchr_m(s,'\\')) != NULL)
121                         *p = 0;
122                 else
123                         *s = 0;
124                 pstrcat(s,s1);
125         }  
126
127         trim_string(s,NULL,"\\..");
128
129         all_string_sub(s, "\\.\\", "\\", 0);
130 }
131
132 /****************************************************************************
133 write to a local file with CR/LF->LF translation if appropriate. return the 
134 number taken from the buffer. This may not equal the number written.
135 ****************************************************************************/
136 static int writefile(int f, char *b, int n)
137 {
138         int i;
139
140         if (!translation) {
141                 return write(f,b,n);
142         }
143
144         i = 0;
145         while (i < n) {
146                 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
147                         b++;i++;
148                 }
149                 if (write(f, b, 1) != 1) {
150                         break;
151                 }
152                 b++;
153                 i++;
154         }
155   
156         return(i);
157 }
158
159 /****************************************************************************
160   read from a file with LF->CR/LF translation if appropriate. return the 
161   number read. read approx n bytes.
162 ****************************************************************************/
163 static int readfile(char *b, int n, XFILE *f)
164 {
165         int i;
166         int c;
167
168         if (!translation)
169                 return x_fread(b,1,n,f);
170   
171         i = 0;
172         while (i < (n - 1) && (i < CLI_BUFFER_SIZE)) {
173                 if ((c = x_getc(f)) == EOF) {
174                         break;
175                 }
176       
177                 if (c == '\n') { /* change all LFs to CR/LF */
178                         b[i++] = '\r';
179                 }
180       
181                 b[i++] = c;
182         }
183   
184         return(i);
185 }
186  
187
188 /****************************************************************************
189 send a message
190 ****************************************************************************/
191 static void send_message(void)
192 {
193         int total_len = 0;
194         int grp_id;
195
196         if (!cli_message_start(cli, desthost, username, &grp_id)) {
197                 d_printf("message start: %s\n", cli_errstr(cli));
198                 return;
199         }
200
201
202         d_printf("Connected. Type your message, ending it with a Control-D\n");
203
204         while (!feof(stdin) && total_len < 1600) {
205                 int maxlen = MIN(1600 - total_len,127);
206                 pstring msg;
207                 int l=0;
208                 int c;
209
210                 ZERO_ARRAY(msg);
211
212                 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
213                         if (c == '\n')
214                                 msg[l++] = '\r';
215                         msg[l] = c;   
216                 }
217
218                 if (!cli_message_text(cli, msg, l, grp_id)) {
219                         d_printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
220                         return;
221                 }      
222                 
223                 total_len += l;
224         }
225
226         if (total_len >= 1600)
227                 d_printf("the message was truncated to 1600 bytes\n");
228         else
229                 d_printf("sent %d bytes\n",total_len);
230
231         if (!cli_message_end(cli, grp_id)) {
232                 d_printf("SMBsendend failed (%s)\n",cli_errstr(cli));
233                 return;
234         }      
235 }
236
237
238
239 /****************************************************************************
240 check the space on a device
241 ****************************************************************************/
242 static int do_dskattr(void)
243 {
244         int total, bsize, avail;
245
246         if (!cli_dskattr(cli, &bsize, &total, &avail)) {
247                 d_printf("Error in dskattr: %s\n",cli_errstr(cli)); 
248                 return 1;
249         }
250
251         d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
252                  total, bsize, avail);
253
254         return 0;
255 }
256
257 /****************************************************************************
258 show cd/pwd
259 ****************************************************************************/
260 static int cmd_pwd(void)
261 {
262         d_printf("Current directory is %s",service);
263         d_printf("%s\n",cur_dir);
264         return 0;
265 }
266
267
268 /****************************************************************************
269 change directory - inner section
270 ****************************************************************************/
271 static int do_cd(char *newdir)
272 {
273         char *p = newdir;
274         pstring saved_dir;
275         pstring dname;
276       
277         dos_format(newdir);
278
279         /* Save the current directory in case the
280            new directory is invalid */
281         pstrcpy(saved_dir, cur_dir);
282         if (*p == '\\')
283                 pstrcpy(cur_dir,p);
284         else
285                 pstrcat(cur_dir,p);
286         if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
287                 pstrcat(cur_dir, "\\");
288         }
289         dos_clean_name(cur_dir);
290         pstrcpy(dname,cur_dir);
291         pstrcat(cur_dir,"\\");
292         dos_clean_name(cur_dir);
293         
294         if (!strequal(cur_dir,"\\")) {
295                 if (!cli_chkpath(cli, dname)) {
296                         d_printf("cd %s: %s\n", dname, cli_errstr(cli));
297                         pstrcpy(cur_dir,saved_dir);
298                 }
299         }
300         
301         pstrcpy(cd_path,cur_dir);
302
303         return 0;
304 }
305
306 /****************************************************************************
307 change directory
308 ****************************************************************************/
309 static int cmd_cd(void)
310 {
311         fstring buf;
312         int rc = 0;
313
314         if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
315                 rc = do_cd(buf);
316         else
317                 d_printf("Current directory is %s\n",cur_dir);
318
319         return rc;
320 }
321
322
323 /*******************************************************************
324   decide if a file should be operated on
325   ********************************************************************/
326 static BOOL do_this_one(file_info *finfo)
327 {
328         if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) return(True);
329
330         if (*fileselection && 
331             !mask_match(cli, finfo->name,fileselection,False)) {
332                 DEBUG(3,("mask_match %s failed\n", finfo->name));
333                 return False;
334         }
335
336         if (newer_than && finfo->mtime < newer_than) {
337                 DEBUG(3,("newer_than %s failed\n", finfo->name));
338                 return(False);
339         }
340
341         if ((archive_level==1 || archive_level==2) && !(finfo->mode & FILE_ATTRIBUTE_ARCHIVE)) {
342                 DEBUG(3,("archive %s failed\n", finfo->name));
343                 return(False);
344         }
345         
346         return(True);
347 }
348
349 /*******************************************************************
350  Return a string representing an attribute for a file.
351 ********************************************************************/
352 static const char *attrib_string(uint16 mode)
353 {
354         static fstring attrstr;
355         int i, len;
356         const struct {
357                 char c;
358                 uint16 attr;
359         } attr_strs[] = {
360                 {'V', FILE_ATTRIBUTE_VOLUME},
361                 {'D', FILE_ATTRIBUTE_DIRECTORY},
362                 {'A', FILE_ATTRIBUTE_ARCHIVE},
363                 {'H', FILE_ATTRIBUTE_HIDDEN},
364                 {'S', FILE_ATTRIBUTE_SYSTEM},
365                 {'R', FILE_ATTRIBUTE_READONLY},
366                 {'d', FILE_ATTRIBUTE_DEVICE},
367                 {'t', FILE_ATTRIBUTE_TEMPORARY},
368                 {'s', FILE_ATTRIBUTE_SPARSE},
369                 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
370                 {'c', FILE_ATTRIBUTE_COMPRESSED},
371                 {'o', FILE_ATTRIBUTE_OFFLINE},
372                 {'n', FILE_ATTRIBUTE_NONINDEXED},
373                 {'e', FILE_ATTRIBUTE_ENCRYPTED}
374         };
375
376         for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
377                 if (mode & attr_strs[i].attr) {
378                         attrstr[len++] = attr_strs[i].c;
379                 }
380         }
381
382         attrstr[len] = 0;
383
384         return(attrstr);
385 }
386
387 /****************************************************************************
388   display info about a file
389   ****************************************************************************/
390 static void display_finfo(file_info *finfo)
391 {
392         if (do_this_one(finfo)) {
393                 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
394                 d_printf("  %-30s%7.7s %8.0f  %s",
395                          finfo->name,
396                          attrib_string(finfo->mode),
397                          (double)finfo->size,
398                          asctime(LocalTime(&t)));
399                 dir_total += finfo->size;
400         }
401 }
402
403
404 /****************************************************************************
405    accumulate size of a file
406   ****************************************************************************/
407 static void do_du(file_info *finfo)
408 {
409         if (do_this_one(finfo)) {
410                 dir_total += finfo->size;
411         }
412 }
413
414 static BOOL do_list_recurse;
415 static BOOL do_list_dirs;
416 static char *do_list_queue = 0;
417 static long do_list_queue_size = 0;
418 static long do_list_queue_start = 0;
419 static long do_list_queue_end = 0;
420 static void (*do_list_fn)(file_info *);
421
422 /****************************************************************************
423 functions for do_list_queue
424   ****************************************************************************/
425
426 /*
427  * The do_list_queue is a NUL-separated list of strings stored in a
428  * char*.  Since this is a FIFO, we keep track of the beginning and
429  * ending locations of the data in the queue.  When we overflow, we
430  * double the size of the char*.  When the start of the data passes
431  * the midpoint, we move everything back.  This is logically more
432  * complex than a linked list, but easier from a memory management
433  * angle.  In any memory error condition, do_list_queue is reset.
434  * Functions check to ensure that do_list_queue is non-NULL before
435  * accessing it.
436  */
437 static void reset_do_list_queue(void)
438 {
439         SAFE_FREE(do_list_queue);
440         do_list_queue_size = 0;
441         do_list_queue_start = 0;
442         do_list_queue_end = 0;
443 }
444
445 static void init_do_list_queue(void)
446 {
447         reset_do_list_queue();
448         do_list_queue_size = 1024;
449         do_list_queue = malloc(do_list_queue_size);
450         if (do_list_queue == 0) { 
451                 d_printf("malloc fail for size %d\n",
452                          (int)do_list_queue_size);
453                 reset_do_list_queue();
454         } else {
455                 memset(do_list_queue, 0, do_list_queue_size);
456         }
457 }
458
459 static void adjust_do_list_queue(void)
460 {
461         /*
462          * If the starting point of the queue is more than half way through,
463          * move everything toward the beginning.
464          */
465         if (do_list_queue && (do_list_queue_start == do_list_queue_end))
466         {
467                 DEBUG(4,("do_list_queue is empty\n"));
468                 do_list_queue_start = do_list_queue_end = 0;
469                 *do_list_queue = '\0';
470         }
471         else if (do_list_queue_start > (do_list_queue_size / 2))
472         {
473                 DEBUG(4,("sliding do_list_queue backward\n"));
474                 memmove(do_list_queue,
475                         do_list_queue + do_list_queue_start,
476                         do_list_queue_end - do_list_queue_start);
477                 do_list_queue_end -= do_list_queue_start;
478                 do_list_queue_start = 0;
479         }
480            
481 }
482
483 static void add_to_do_list_queue(const char* entry)
484 {
485         char *dlq;
486         long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
487         while (new_end > do_list_queue_size)
488         {
489                 do_list_queue_size *= 2;
490                 DEBUG(4,("enlarging do_list_queue to %d\n",
491                          (int)do_list_queue_size));
492                 dlq = Realloc(do_list_queue, do_list_queue_size);
493                 if (! dlq) {
494                         d_printf("failure enlarging do_list_queue to %d bytes\n",
495                                  (int)do_list_queue_size);
496                         reset_do_list_queue();
497                 }
498                 else
499                 {
500                         do_list_queue = dlq;
501                         memset(do_list_queue + do_list_queue_size / 2,
502                                0, do_list_queue_size / 2);
503                 }
504         }
505         if (do_list_queue)
506         {
507                 safe_strcpy(do_list_queue + do_list_queue_end, entry, 
508                             do_list_queue_size - do_list_queue_end - 1);
509                 do_list_queue_end = new_end;
510                 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
511                          entry, (int)do_list_queue_start, (int)do_list_queue_end));
512         }
513 }
514
515 static char *do_list_queue_head(void)
516 {
517         return do_list_queue + do_list_queue_start;
518 }
519
520 static void remove_do_list_queue_head(void)
521 {
522         if (do_list_queue_end > do_list_queue_start)
523         {
524                 do_list_queue_start += strlen(do_list_queue_head()) + 1;
525                 adjust_do_list_queue();
526                 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
527                          (int)do_list_queue_start, (int)do_list_queue_end));
528         }
529 }
530
531 static int do_list_queue_empty(void)
532 {
533         return (! (do_list_queue && *do_list_queue));
534 }
535
536 /****************************************************************************
537 a helper for do_list
538   ****************************************************************************/
539 static void do_list_helper(file_info *f, const char *mask, void *state)
540 {
541         if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
542                 if (do_list_dirs && do_this_one(f)) {
543                         do_list_fn(f);
544                 }
545                 if (do_list_recurse && 
546                     !strequal(f->name,".") && 
547                     !strequal(f->name,"..")) {
548                         pstring mask2;
549                         char *p;
550
551                         pstrcpy(mask2, mask);
552                         p = strrchr_m(mask2,'\\');
553                         if (!p) return;
554                         p[1] = 0;
555                         pstrcat(mask2, f->name);
556                         pstrcat(mask2,"\\*");
557                         add_to_do_list_queue(mask2);
558                 }
559                 return;
560         }
561
562         if (do_this_one(f)) {
563                 do_list_fn(f);
564         }
565 }
566
567
568 /****************************************************************************
569 a wrapper around cli_list that adds recursion
570   ****************************************************************************/
571 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
572 {
573         static int in_do_list = 0;
574
575         if (in_do_list && rec)
576         {
577                 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
578                 exit(1);
579         }
580
581         in_do_list = 1;
582
583         do_list_recurse = rec;
584         do_list_dirs = dirs;
585         do_list_fn = fn;
586
587         if (rec)
588         {
589                 init_do_list_queue();
590                 add_to_do_list_queue(mask);
591                 
592                 while (! do_list_queue_empty())
593                 {
594                         /*
595                          * Need to copy head so that it doesn't become
596                          * invalid inside the call to cli_list.  This
597                          * would happen if the list were expanded
598                          * during the call.
599                          * Fix from E. Jay Berkenbilt (ejb@ql.org)
600                          */
601                         pstring head;
602                         pstrcpy(head, do_list_queue_head());
603                         cli_list(cli, head, attribute, do_list_helper, NULL);
604                         remove_do_list_queue_head();
605                         if ((! do_list_queue_empty()) && (fn == display_finfo))
606                         {
607                                 char* next_file = do_list_queue_head();
608                                 char* save_ch = 0;
609                                 if ((strlen(next_file) >= 2) &&
610                                     (next_file[strlen(next_file) - 1] == '*') &&
611                                     (next_file[strlen(next_file) - 2] == '\\'))
612                                 {
613                                         save_ch = next_file +
614                                                 strlen(next_file) - 2;
615                                         *save_ch = '\0';
616                                 }
617                                 d_printf("\n%s\n",next_file);
618                                 if (save_ch)
619                                 {
620                                         *save_ch = '\\';
621                                 }
622                         }
623                 }
624         }
625         else
626         {
627                 if (cli_list(cli, mask, attribute, do_list_helper, NULL) == -1)
628                 {
629                         d_printf("%s listing %s\n", cli_errstr(cli), mask);
630                 }
631         }
632
633         in_do_list = 0;
634         reset_do_list_queue();
635 }
636
637 /****************************************************************************
638   get a directory listing
639   ****************************************************************************/
640 static int cmd_dir(void)
641 {
642         uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
643         pstring mask;
644         fstring buf;
645         char *p=buf;
646         int rc;
647         
648         dir_total = 0;
649         pstrcpy(mask,cur_dir);
650         if(mask[strlen(mask)-1]!='\\')
651                 pstrcat(mask,"\\");
652         
653         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
654                 dos_format(p);
655                 if (*p == '\\')
656                         pstrcpy(mask,p);
657                 else
658                         pstrcat(mask,p);
659         }
660         else {
661                 pstrcat(mask,"*");
662         }
663
664         do_list(mask, attribute, display_finfo, recurse, True);
665
666         rc = do_dskattr();
667
668         DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
669
670         return rc;
671 }
672
673
674 /****************************************************************************
675   get a directory listing
676   ****************************************************************************/
677 static int cmd_du(void)
678 {
679         uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
680         pstring mask;
681         fstring buf;
682         char *p=buf;
683         int rc;
684         
685         dir_total = 0;
686         pstrcpy(mask,cur_dir);
687         if(mask[strlen(mask)-1]!='\\')
688                 pstrcat(mask,"\\");
689         
690         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
691                 dos_format(p);
692                 if (*p == '\\')
693                         pstrcpy(mask,p);
694                 else
695                         pstrcat(mask,p);
696         } else {
697                 pstrcat(mask,"*");
698         }
699
700         do_list(mask, attribute, do_du, recurse, True);
701
702         rc = do_dskattr();
703
704         d_printf("Total number of bytes: %.0f\n", dir_total);
705
706         return rc;
707 }
708
709
710 /****************************************************************************
711   get a file from rname to lname
712   ****************************************************************************/
713 static int do_get(char *rname, const char *lname, BOOL reget)
714 {  
715         int handle = 0, fnum;
716         BOOL newhandle = False;
717         char *data;
718         struct timeval tp_start;
719         int read_size = io_bufsize;
720         uint16 attr;
721         size_t size;
722         off_t start = 0;
723         off_t nread = 0;
724         int rc = 0;
725
726         GetTimeOfDay(&tp_start);
727
728         if (lowercase) {
729                 strlower(lname);
730         }
731
732         fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
733
734         if (fnum == -1) {
735                 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
736                 return 1;
737         }
738
739         if(!strcmp(lname,"-")) {
740                 handle = fileno(stdout);
741         } else {
742                 if (reget) {
743                         handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
744                         if (handle >= 0) {
745                                 start = sys_lseek(handle, 0, SEEK_END);
746                                 if (start == -1) {
747                                         d_printf("Error seeking local file\n");
748                                         return 1;
749                                 }
750                         }
751                 } else {
752                         handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
753                 }
754                 newhandle = True;
755         }
756         if (handle < 0) {
757                 d_printf("Error opening local file %s\n",lname);
758                 return 1;
759         }
760
761
762         if (!cli_qfileinfo(cli, fnum, 
763                            &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
764             !cli_getattrE(cli, fnum, 
765                           &attr, &size, NULL, NULL, NULL)) {
766                 d_printf("getattrib: %s\n",cli_errstr(cli));
767                 return 1;
768         }
769
770         DEBUG(2,("getting file %s of size %.0f as %s ", 
771                  rname, (double)size, lname));
772
773         if(!(data = (char *)malloc(read_size))) { 
774                 d_printf("malloc fail for size %d\n", read_size);
775                 cli_close(cli, fnum);
776                 return 1;
777         }
778
779         while (1) {
780                 int n = cli_read(cli, fnum, data, nread + start, read_size);
781
782                 if (n <= 0) break;
783  
784                 if (writefile(handle,data, n) != n) {
785                         d_printf("Error writing local file\n");
786                         rc = 1;
787                         break;
788                 }
789       
790                 nread += n;
791         }
792
793         if (nread + start < size) {
794                 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
795                             rname, (long)nread));
796
797                 rc = 1;
798         }
799
800         SAFE_FREE(data);
801         
802         if (!cli_close(cli, fnum)) {
803                 d_printf("Error %s closing remote file\n",cli_errstr(cli));
804                 rc = 1;
805         }
806
807         if (newhandle) {
808                 close(handle);
809         }
810
811         if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
812                 cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
813         }
814
815         {
816                 struct timeval tp_end;
817                 int this_time;
818                 
819                 GetTimeOfDay(&tp_end);
820                 this_time = 
821                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
822                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
823                 get_total_time_ms += this_time;
824                 get_total_size += nread;
825                 
826                 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
827                          nread / (1.024*this_time + 1.0e-4),
828                          get_total_size / (1.024*get_total_time_ms)));
829         }
830         
831         return rc;
832 }
833
834
835 /****************************************************************************
836   get a file
837   ****************************************************************************/
838 static int cmd_get(void)
839 {
840         pstring lname;
841         pstring rname;
842         char *p;
843
844         pstrcpy(rname,cur_dir);
845         pstrcat(rname,"\\");
846         
847         p = rname + strlen(rname);
848         
849         if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
850                 d_printf("get <filename>\n");
851                 return 1;
852         }
853         pstrcpy(lname,p);
854         dos_clean_name(rname);
855         
856         next_token_nr(NULL,lname,NULL,sizeof(lname));
857         
858         return do_get(rname, lname, False);
859 }
860
861
862 /****************************************************************************
863   do a mget operation on one file
864   ****************************************************************************/
865 static void do_mget(file_info *finfo)
866 {
867         pstring rname;
868         pstring quest;
869         pstring saved_curdir;
870         pstring mget_mask;
871
872         if (strequal(finfo->name,".") || strequal(finfo->name,".."))
873                 return;
874
875         if (abort_mget) {
876                 d_printf("mget aborted\n");
877                 return;
878         }
879
880         if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY)
881                 slprintf(quest,sizeof(pstring)-1,
882                          "Get directory %s? ",finfo->name);
883         else
884                 slprintf(quest,sizeof(pstring)-1,
885                          "Get file %s? ",finfo->name);
886
887         if (prompt && !yesno(quest)) return;
888
889         if (!(finfo->mode & FILE_ATTRIBUTE_DIRECTORY)) {
890                 pstrcpy(rname,cur_dir);
891                 pstrcat(rname,finfo->name);
892                 do_get(rname, finfo->name, False);
893                 return;
894         }
895
896         /* handle directories */
897         pstrcpy(saved_curdir,cur_dir);
898
899         pstrcat(cur_dir,finfo->name);
900         pstrcat(cur_dir,"\\");
901
902         unix_format(finfo->name);
903         if (lowercase)
904                 strlower(finfo->name);
905         
906         if (!directory_exist(finfo->name,NULL) && 
907             mkdir(finfo->name,0777) != 0) {
908                 d_printf("failed to create directory %s\n",finfo->name);
909                 pstrcpy(cur_dir,saved_curdir);
910                 return;
911         }
912         
913         if (chdir(finfo->name) != 0) {
914                 d_printf("failed to chdir to directory %s\n",finfo->name);
915                 pstrcpy(cur_dir,saved_curdir);
916                 return;
917         }
918
919         pstrcpy(mget_mask,cur_dir);
920         pstrcat(mget_mask,"*");
921         
922         do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,False, True);
923         chdir("..");
924         pstrcpy(cur_dir,saved_curdir);
925 }
926
927
928 /****************************************************************************
929 view the file using the pager
930 ****************************************************************************/
931 static int cmd_more(void)
932 {
933         fstring rname,lname,pager_cmd;
934         char *pager;
935         int fd;
936         int rc = 0;
937
938         fstrcpy(rname,cur_dir);
939         fstrcat(rname,"\\");
940         
941         slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir());
942         fd = smb_mkstemp(lname);
943         if (fd == -1) {
944                 d_printf("failed to create temporary file for more\n");
945                 return 1;
946         }
947         close(fd);
948
949         if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
950                 d_printf("more <filename>\n");
951                 unlink(lname);
952                 return 1;
953         }
954         dos_clean_name(rname);
955
956         rc = do_get(rname, lname, False);
957
958         pager=getenv("PAGER");
959
960         slprintf(pager_cmd,sizeof(pager_cmd)-1,
961                  "%s %s",(pager? pager:PAGER), lname);
962         system(pager_cmd);
963         unlink(lname);
964         
965         return rc;
966 }
967
968
969
970 /****************************************************************************
971 do a mget command
972 ****************************************************************************/
973 static int cmd_mget(void)
974 {
975         uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
976         pstring mget_mask;
977         fstring buf;
978         char *p=buf;
979
980         *mget_mask = 0;
981
982         if (recurse)
983                 attribute |= FILE_ATTRIBUTE_DIRECTORY;
984         
985         abort_mget = False;
986
987         while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
988                 pstrcpy(mget_mask,cur_dir);
989                 if(mget_mask[strlen(mget_mask)-1]!='\\')
990                         pstrcat(mget_mask,"\\");
991                 
992                 if (*p == '\\')
993                         pstrcpy(mget_mask,p);
994                 else
995                         pstrcat(mget_mask,p);
996                 do_list(mget_mask, attribute,do_mget,False,True);
997         }
998
999         if (!*mget_mask) {
1000                 pstrcpy(mget_mask,cur_dir);
1001                 if(mget_mask[strlen(mget_mask)-1]!='\\')
1002                         pstrcat(mget_mask,"\\");
1003                 pstrcat(mget_mask,"*");
1004                 do_list(mget_mask, attribute,do_mget,False,True);
1005         }
1006         
1007         return 0;
1008 }
1009
1010
1011 /****************************************************************************
1012 make a directory of name "name"
1013 ****************************************************************************/
1014 static BOOL do_mkdir(char *name)
1015 {
1016         if (!cli_mkdir(cli, name)) {
1017                 d_printf("%s making remote directory %s\n",
1018                          cli_errstr(cli),name);
1019                 return(False);
1020         }
1021
1022         return(True);
1023 }
1024
1025 /****************************************************************************
1026 show 8.3 name of a file
1027 ****************************************************************************/
1028 static BOOL do_altname(char *name)
1029 {
1030         const char *altname;
1031         if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, &altname))) {
1032                 d_printf("%s getting alt name for %s\n",
1033                          cli_errstr(cli),name);
1034                 return(False);
1035         }
1036         d_printf("%s\n", altname);
1037
1038         return(True);
1039 }
1040
1041
1042 /****************************************************************************
1043  Exit client.
1044 ****************************************************************************/
1045 static int cmd_quit(void)
1046 {
1047         cli_shutdown(cli);
1048         exit(0);
1049         /* NOTREACHED */
1050         return 0;
1051 }
1052
1053
1054 /****************************************************************************
1055   make a directory
1056   ****************************************************************************/
1057 static int cmd_mkdir(void)
1058 {
1059         pstring mask;
1060         fstring buf;
1061         char *p=buf;
1062   
1063         pstrcpy(mask,cur_dir);
1064
1065         if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1066                 if (!recurse)
1067                         d_printf("mkdir <dirname>\n");
1068                 return 1;
1069         }
1070         pstrcat(mask,p);
1071
1072         if (recurse) {
1073                 pstring ddir;
1074                 pstring ddir2;
1075                 *ddir2 = 0;
1076                 
1077                 pstrcpy(ddir,mask);
1078                 trim_string(ddir,".",NULL);
1079                 p = strtok(ddir,"/\\");
1080                 while (p) {
1081                         pstrcat(ddir2,p);
1082                         if (!cli_chkpath(cli, ddir2)) { 
1083                                 do_mkdir(ddir2);
1084                         }
1085                         pstrcat(ddir2,"\\");
1086                         p = strtok(NULL,"/\\");
1087                 }        
1088         } else {
1089                 do_mkdir(mask);
1090         }
1091         
1092         return 0;
1093 }
1094
1095
1096 /****************************************************************************
1097   show alt name
1098   ****************************************************************************/
1099 static int cmd_altname(void)
1100 {
1101         pstring name;
1102         fstring buf;
1103         char *p=buf;
1104   
1105         pstrcpy(name,cur_dir);
1106
1107         if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1108                 d_printf("altname <file>\n");
1109                 return 1;
1110         }
1111         pstrcat(name,p);
1112
1113         do_altname(name);
1114
1115         return 0;
1116 }
1117
1118
1119 /****************************************************************************
1120   put a single file
1121   ****************************************************************************/
1122 static int do_put(char *rname, char *lname, BOOL reput)
1123 {
1124         int fnum;
1125         XFILE *f;
1126         size_t start = 0;
1127         off_t nread = 0;
1128         char *buf = NULL;
1129         int maxwrite = io_bufsize;
1130         int rc = 0;
1131         
1132         struct timeval tp_start;
1133         GetTimeOfDay(&tp_start);
1134
1135         if (reput) {
1136                 fnum = cli_open(cli, rname, O_RDWR|O_CREAT, DENY_NONE);
1137                 if (fnum >= 0) {
1138                         if (!cli_qfileinfo(cli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
1139                             !cli_getattrE(cli, fnum, NULL, &start, NULL, NULL, NULL)) {
1140                                 d_printf("getattrib: %s\n",cli_errstr(cli));
1141                                 return 1;
1142                         }
1143                 }
1144         } else {
1145                 fnum = cli_open(cli, rname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
1146         }
1147   
1148         if (fnum == -1) {
1149                 d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
1150                 return 1;
1151         }
1152
1153         /* allow files to be piped into smbclient
1154            jdblair 24.jun.98
1155
1156            Note that in this case this function will exit(0) rather
1157            than returning. */
1158         if (!strcmp(lname, "-")) {
1159                 f = x_stdin;
1160                 /* size of file is not known */
1161         } else {
1162                 f = x_fopen(lname,O_RDONLY, 0);
1163                 if (f && reput) {
1164                         if (x_tseek(f, start, SEEK_SET) == -1) {
1165                                 d_printf("Error seeking local file\n");
1166                                 return 1;
1167                         }
1168                 }
1169         }
1170
1171         if (!f) {
1172                 d_printf("Error opening local file %s\n",lname);
1173                 return 1;
1174         }
1175
1176   
1177         DEBUG(1,("putting file %s as %s ",lname,
1178                  rname));
1179   
1180         buf = (char *)malloc(maxwrite);
1181         if (!buf) {
1182                 d_printf("ERROR: Not enough memory!\n");
1183                 return 1;
1184         }
1185         while (!x_feof(f)) {
1186                 int n = maxwrite;
1187                 int ret;
1188
1189                 if ((n = readfile(buf,n,f)) < 1) {
1190                         if((n == 0) && x_feof(f))
1191                                 break; /* Empty local file. */
1192
1193                         d_printf("Error reading local file: %s\n", strerror(errno));
1194                         rc = 1;
1195                         break;
1196                 }
1197
1198                 ret = cli_write(cli, fnum, 0, buf, nread + start, n);
1199
1200                 if (n != ret) {
1201                         d_printf("Error writing file: %s\n", cli_errstr(cli));
1202                         rc = 1;
1203                         break;
1204                 } 
1205
1206                 nread += n;
1207         }
1208
1209         if (!cli_close(cli, fnum)) {
1210                 d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
1211                 x_fclose(f);
1212                 SAFE_FREE(buf);
1213                 return 1;
1214         }
1215
1216         
1217         if (f != x_stdin) {
1218                 x_fclose(f);
1219         }
1220
1221         SAFE_FREE(buf);
1222
1223         {
1224                 struct timeval tp_end;
1225                 int this_time;
1226                 
1227                 GetTimeOfDay(&tp_end);
1228                 this_time = 
1229                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1230                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
1231                 put_total_time_ms += this_time;
1232                 put_total_size += nread;
1233                 
1234                 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1235                          nread / (1.024*this_time + 1.0e-4),
1236                          put_total_size / (1.024*put_total_time_ms)));
1237         }
1238
1239         if (f == x_stdin) {
1240                 cli_shutdown(cli);
1241                 exit(0);
1242         }
1243         
1244         return rc;
1245 }
1246
1247  
1248
1249 /****************************************************************************
1250   put a file
1251   ****************************************************************************/
1252 static int cmd_put(void)
1253 {
1254         pstring lname;
1255         pstring rname;
1256         fstring buf;
1257         char *p=buf;
1258         
1259         pstrcpy(rname,cur_dir);
1260         pstrcat(rname,"\\");
1261   
1262         if (!next_token_nr(NULL,p,NULL,sizeof(buf))) {
1263                 d_printf("put <filename>\n");
1264                 return 1;
1265         }
1266         pstrcpy(lname,p);
1267   
1268         if (next_token_nr(NULL,p,NULL,sizeof(buf)))
1269                 pstrcat(rname,p);      
1270         else
1271                 pstrcat(rname,lname);
1272         
1273         dos_clean_name(rname);
1274
1275         {
1276                 SMB_STRUCT_STAT st;
1277                 /* allow '-' to represent stdin
1278                    jdblair, 24.jun.98 */
1279                 if (!file_exist(lname,&st) &&
1280                     (strcmp(lname,"-"))) {
1281                         d_printf("%s does not exist\n",lname);
1282                         return 1;
1283                 }
1284         }
1285
1286         return do_put(rname, lname, False);
1287 }
1288
1289 /*************************************
1290   File list structure
1291 *************************************/
1292
1293 static struct file_list {
1294         struct file_list *prev, *next;
1295         char *file_path;
1296         BOOL isdir;
1297 } *file_list;
1298
1299 /****************************************************************************
1300   Free a file_list structure
1301 ****************************************************************************/
1302
1303 static void free_file_list (struct file_list * list)
1304 {
1305         struct file_list *tmp;
1306         
1307         while (list)
1308         {
1309                 tmp = list;
1310                 DLIST_REMOVE(list, list);
1311                 SAFE_FREE(tmp->file_path);
1312                 SAFE_FREE(tmp);
1313         }
1314 }
1315
1316 /****************************************************************************
1317   seek in a directory/file list until you get something that doesn't start with
1318   the specified name
1319   ****************************************************************************/
1320 static BOOL seek_list(struct file_list *list, char *name)
1321 {
1322         while (list) {
1323                 trim_string(list->file_path,"./","\n");
1324                 if (strncmp(list->file_path, name, strlen(name)) != 0) {
1325                         return(True);
1326                 }
1327                 list = list->next;
1328         }
1329       
1330         return(False);
1331 }
1332
1333 /****************************************************************************
1334   set the file selection mask
1335   ****************************************************************************/
1336 static int cmd_select(void)
1337 {
1338         pstrcpy(fileselection,"");
1339         next_token_nr(NULL,fileselection,NULL,sizeof(fileselection));
1340
1341         return 0;
1342 }
1343
1344 /****************************************************************************
1345   Recursive file matching function act as find
1346   match must be always set to True when calling this function
1347 ****************************************************************************/
1348 static int file_find(struct file_list **list, const char *directory, 
1349                       const char *expression, BOOL match)
1350 {
1351         DIR *dir;
1352         struct file_list *entry;
1353         struct stat statbuf;
1354         int ret;
1355         char *path;
1356         BOOL isdir;
1357         const char *dname;
1358
1359         dir = opendir(directory);
1360         if (!dir) return -1;
1361         
1362         while ((dname = readdirname(dir))) {
1363                 if (!strcmp("..", dname)) continue;
1364                 if (!strcmp(".", dname)) continue;
1365                 
1366                 if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
1367                         continue;
1368                 }
1369
1370                 isdir = False;
1371                 if (!match || !gen_fnmatch(expression, dname)) {
1372                         if (recurse) {
1373                                 ret = stat(path, &statbuf);
1374                                 if (ret == 0) {
1375                                         if (S_ISDIR(statbuf.st_mode)) {
1376                                                 isdir = True;
1377                                                 ret = file_find(list, path, expression, False);
1378                                         }
1379                                 } else {
1380                                         d_printf("file_find: cannot stat file %s\n", path);
1381                                 }
1382                                 
1383                                 if (ret == -1) {
1384                                         SAFE_FREE(path);
1385                                         closedir(dir);
1386                                         return -1;
1387                                 }
1388                         }
1389                         entry = (struct file_list *) malloc(sizeof (struct file_list));
1390                         if (!entry) {
1391                                 d_printf("Out of memory in file_find\n");
1392                                 closedir(dir);
1393                                 return -1;
1394                         }
1395                         entry->file_path = path;
1396                         entry->isdir = isdir;
1397                         DLIST_ADD(*list, entry);
1398                 } else {
1399                         SAFE_FREE(path);
1400                 }
1401         }
1402
1403         closedir(dir);
1404         return 0;
1405 }
1406
1407 /****************************************************************************
1408   mput some files
1409   ****************************************************************************/
1410 static int cmd_mput(void)
1411 {
1412         fstring buf;
1413         char *p=buf;
1414         
1415         while (next_token_nr(NULL,p,NULL,sizeof(buf))) {
1416                 int ret;
1417                 struct file_list *temp_list;
1418                 char *quest, *lname, *rname;
1419         
1420                 file_list = NULL;
1421
1422                 ret = file_find(&file_list, ".", p, True);
1423                 if (ret) {
1424                         free_file_list(file_list);
1425                         continue;
1426                 }
1427                 
1428                 quest = NULL;
1429                 lname = NULL;
1430                 rname = NULL;
1431                                 
1432                 for (temp_list = file_list; temp_list; 
1433                      temp_list = temp_list->next) {
1434
1435                         SAFE_FREE(lname);
1436                         if (asprintf(&lname, "%s/", temp_list->file_path) <= 0)
1437                                 continue;
1438                         trim_string(lname, "./", "/");
1439                         
1440                         /* check if it's a directory */
1441                         if (temp_list->isdir) {
1442                                 /* if (!recurse) continue; */
1443                                 
1444                                 SAFE_FREE(quest);
1445                                 if (asprintf(&quest, "Put directory %s? ", lname) < 0) break;
1446                                 if (prompt && !yesno(quest)) { /* No */
1447                                         /* Skip the directory */
1448                                         lname[strlen(lname)-1] = '/';
1449                                         if (!seek_list(temp_list, lname))
1450                                                 break;              
1451                                 } else { /* Yes */
1452                                         SAFE_FREE(rname);
1453                                         if(asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1454                                         dos_format(rname);
1455                                         if (!cli_chkpath(cli, rname) && 
1456                                             !do_mkdir(rname)) {
1457                                                 DEBUG (0, ("Unable to make dir, skipping..."));
1458                                                 /* Skip the directory */
1459                                                 lname[strlen(lname)-1] = '/';
1460                                                 if (!seek_list(temp_list, lname))
1461                                                         break;
1462                                         }
1463                                 }
1464                                 continue;
1465                         } else {
1466                                 SAFE_FREE(quest);
1467                                 if (asprintf(&quest,"Put file %s? ", lname) < 0) break;
1468                                 if (prompt && !yesno(quest)) /* No */
1469                                         continue;
1470                                 
1471                                 /* Yes */
1472                                 SAFE_FREE(rname);
1473                                 if (asprintf(&rname, "%s%s", cur_dir, lname) < 0) break;
1474                         }
1475
1476                         dos_format(rname);
1477
1478                         do_put(rname, lname, False);
1479                 }
1480                 free_file_list(file_list);
1481                 SAFE_FREE(quest);
1482                 SAFE_FREE(lname);
1483                 SAFE_FREE(rname);
1484         }
1485
1486         return 0;
1487 }
1488
1489
1490 /****************************************************************************
1491   cancel a print job
1492   ****************************************************************************/
1493 static int do_cancel(int job)
1494 {
1495         d_printf("REWRITE: print job cancel not implemented\n");
1496         return 1;
1497 }
1498
1499
1500 /****************************************************************************
1501   cancel a print job
1502   ****************************************************************************/
1503 static int cmd_cancel(void)
1504 {
1505         fstring buf;
1506         int job; 
1507
1508         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1509                 d_printf("cancel <jobid> ...\n");
1510                 return 1;
1511         }
1512         do {
1513                 job = atoi(buf);
1514                 do_cancel(job);
1515         } while (next_token_nr(NULL,buf,NULL,sizeof(buf)));
1516         
1517         return 0;
1518 }
1519
1520
1521 /****************************************************************************
1522   print a file
1523   ****************************************************************************/
1524 static int cmd_print(void)
1525 {
1526         pstring lname;
1527         pstring rname;
1528         char *p;
1529
1530         if (!next_token_nr(NULL,lname,NULL, sizeof(lname))) {
1531                 d_printf("print <filename>\n");
1532                 return 1;
1533         }
1534
1535         pstrcpy(rname,lname);
1536         p = strrchr_m(rname,'/');
1537         if (p) {
1538                 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1539         }
1540
1541         if (strequal(lname,"-")) {
1542                 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1543         }
1544
1545         return do_put(rname, lname, False);
1546 }
1547
1548
1549 /****************************************************************************
1550  show a print queue
1551 ****************************************************************************/
1552 static int cmd_queue(void)
1553 {
1554         d_printf("REWRITE: print job queue not implemented\n");
1555         
1556         return 0;
1557 }
1558
1559 /****************************************************************************
1560 delete some files
1561 ****************************************************************************/
1562 static void do_del(file_info *finfo)
1563 {
1564         pstring mask;
1565
1566         pstrcpy(mask,cur_dir);
1567         pstrcat(mask,finfo->name);
1568
1569         if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) 
1570                 return;
1571
1572         if (!cli_unlink(cli, mask)) {
1573                 d_printf("%s deleting remote file %s\n",cli_errstr(cli),mask);
1574         }
1575 }
1576
1577 /****************************************************************************
1578 delete some files
1579 ****************************************************************************/
1580 static int cmd_del(void)
1581 {
1582         pstring mask;
1583         fstring buf;
1584         uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
1585
1586         if (recurse)
1587                 attribute |= FILE_ATTRIBUTE_DIRECTORY;
1588         
1589         pstrcpy(mask,cur_dir);
1590         
1591         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1592                 d_printf("del <filename>\n");
1593                 return 1;
1594         }
1595         pstrcat(mask,buf);
1596
1597         do_list(mask, attribute,do_del,False,False);
1598         
1599         return 0;
1600 }
1601
1602
1603 /****************************************************************************
1604 delete a whole directory tree
1605 ****************************************************************************/
1606 static int cmd_deltree(void)
1607 {
1608         pstring dname;
1609         fstring buf;
1610         int ret;
1611
1612         pstrcpy(dname,cur_dir);
1613         
1614         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1615                 d_printf("deltree <dirname>\n");
1616                 return 1;
1617         }
1618         pstrcat(dname,buf);
1619
1620         ret = cli_deltree(cli, dname);
1621
1622         if (ret == -1) {
1623                 printf("Failed to delete tree %s - %s\n", dname, cli_errstr(cli));
1624                 return -1;
1625         }
1626
1627         printf("Deleted %d files in %s\n", ret, dname);
1628         
1629         return 0;
1630 }
1631
1632
1633 /****************************************************************************
1634 show as much information as possible about a file
1635 ****************************************************************************/
1636 static int cmd_allinfo(void)
1637 {
1638         pstring fname;
1639         fstring buf;
1640         int ret = 0;
1641         TALLOC_CTX *mem_ctx;
1642         union smb_fileinfo finfo;
1643         NTSTATUS status;
1644
1645         pstrcpy(fname,cur_dir);
1646         
1647         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1648                 d_printf("allinfo <filename>\n");
1649                 return 1;
1650         }
1651         pstrcat(fname,buf);
1652
1653         mem_ctx = talloc_init(fname);
1654
1655         /* first a ALL_INFO QPATHINFO */
1656         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
1657         finfo.generic.in.fname = fname;
1658         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1659         if (!NT_STATUS_IS_OK(status)) {
1660                 d_printf("%s - %s\n", fname, nt_errstr(status));
1661                 ret = 1;
1662                 goto done;
1663         }
1664
1665         d_printf("\tcreate_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.create_time));
1666         d_printf("\taccess_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.access_time));
1667         d_printf("\twrite_time:     %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.write_time));
1668         d_printf("\tchange_time:    %s\n", nt_time_string(mem_ctx, &finfo.all_info.out.change_time));
1669         d_printf("\tattrib:         0x%x\n", finfo.all_info.out.attrib);
1670         d_printf("\talloc_size:     %lu\n", (unsigned long)finfo.all_info.out.alloc_size);
1671         d_printf("\tsize:           %lu\n", (unsigned long)finfo.all_info.out.size);
1672         d_printf("\tnlink:          %u\n", finfo.all_info.out.nlink);
1673         d_printf("\tdelete_pending: %u\n", finfo.all_info.out.delete_pending);
1674         d_printf("\tdirectory:      %u\n", finfo.all_info.out.directory);
1675         d_printf("\tea_size:        %u\n", finfo.all_info.out.ea_size);
1676         d_printf("\tfname:          '%s'\n", finfo.all_info.out.fname.s);
1677
1678         /* 8.3 name if any */
1679         finfo.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
1680         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1681         if (NT_STATUS_IS_OK(status)) {
1682                 d_printf("\talt_name:       %s\n", finfo.alt_name_info.out.fname.s);
1683         }
1684
1685         /* file_id if available */
1686         finfo.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
1687         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1688         if (NT_STATUS_IS_OK(status)) {
1689                 d_printf("\tfile_id         %.0f\n", finfo.internal_information.out.file_id);
1690         }
1691
1692         /* the EAs, if any */
1693         finfo.generic.level = RAW_FILEINFO_ALL_EAS;
1694         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1695         if (NT_STATUS_IS_OK(status)) {
1696                 int i;
1697                 for (i=0;i<finfo.all_eas.out.num_eas;i++) {
1698                         d_printf("\tEA[%d] flags=%d %s=%*.*s\n", i,
1699                                  finfo.all_eas.out.eas[i].flags,
1700                                  finfo.all_eas.out.eas[i].name.s,
1701                                  finfo.all_eas.out.eas[i].value.length,
1702                                  finfo.all_eas.out.eas[i].value.length,
1703                                  finfo.all_eas.out.eas[i].value.data);
1704                 }
1705         }
1706
1707         /* streams, if available */
1708         finfo.generic.level = RAW_FILEINFO_STREAM_INFO;
1709         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1710         if (NT_STATUS_IS_OK(status)) {
1711                 int i;
1712                 for (i=0;i<finfo.stream_info.out.num_streams;i++) {
1713                         d_printf("\tstream %d:\n", i);
1714                         d_printf("\t\tsize       %ld\n", 
1715                                  (long)finfo.stream_info.out.streams[i].size);
1716                         d_printf("\t\talloc size %ld\n", 
1717                                  (long)finfo.stream_info.out.streams[i].alloc_size);
1718                         d_printf("\t\tname       %s\n", finfo.stream_info.out.streams[i].stream_name.s);
1719                 }
1720         }       
1721
1722         /* dev/inode if available */
1723         finfo.generic.level = RAW_FILEINFO_COMPRESSION_INFORMATION;
1724         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
1725         if (NT_STATUS_IS_OK(status)) {
1726                 d_printf("\tcompressed size %ld\n", (long)finfo.compression_info.out.compressed_size);
1727                 d_printf("\tformat          %ld\n", (long)finfo.compression_info.out.format);
1728                 d_printf("\tunit_shift      %ld\n", (long)finfo.compression_info.out.unit_shift);
1729                 d_printf("\tchunk_shift     %ld\n", (long)finfo.compression_info.out.chunk_shift);
1730                 d_printf("\tcluster_shift   %ld\n", (long)finfo.compression_info.out.cluster_shift);
1731         }
1732
1733         talloc_destroy(mem_ctx);
1734
1735 done:
1736         return ret;
1737 }
1738
1739
1740 /****************************************************************************
1741 show any ACL on a file
1742 ****************************************************************************/
1743 static int cmd_acl(void)
1744 {
1745         pstring fname;
1746         fstring buf;
1747         int ret = 0;
1748         TALLOC_CTX *mem_ctx;
1749         struct smb_query_secdesc query;
1750         NTSTATUS status;
1751         int fnum;
1752
1753         pstrcpy(fname,cur_dir);
1754         
1755         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1756                 d_printf("acl <filename>\n");
1757                 return 1;
1758         }
1759         pstrcat(fname,buf);
1760
1761         fnum = cli_open(cli, fname, O_RDONLY, DENY_NONE);
1762         if (fnum == -1) {
1763                 d_printf("%s - %s\n", fname, cli_errstr(cli));
1764                 return -1;
1765         }
1766
1767         mem_ctx = talloc_init(fname);
1768
1769         query.in.fnum = fnum;
1770         query.in.secinfo_flags = 0x7;
1771
1772         status = smb_raw_query_secdesc(cli->tree, mem_ctx, &query);
1773         if (!NT_STATUS_IS_OK(status)) {
1774                 d_printf("%s - %s\n", fname, nt_errstr(status));
1775                 ret = 1;
1776                 goto done;
1777         }
1778
1779         talloc_destroy(mem_ctx);
1780
1781 done:
1782         return ret;
1783 }
1784
1785
1786 /****************************************************************************
1787 ****************************************************************************/
1788 static int cmd_open(void)
1789 {
1790         pstring mask;
1791         fstring buf;
1792         
1793         pstrcpy(mask,cur_dir);
1794         
1795         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1796                 d_printf("open <filename>\n");
1797                 return 1;
1798         }
1799         pstrcat(mask,buf);
1800
1801         cli_open(cli, mask, O_RDWR, DENY_ALL);
1802
1803         return 0;
1804 }
1805
1806
1807 /****************************************************************************
1808 remove a directory
1809 ****************************************************************************/
1810 static int cmd_rmdir(void)
1811 {
1812         pstring mask;
1813         fstring buf;
1814   
1815         pstrcpy(mask,cur_dir);
1816         
1817         if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
1818                 d_printf("rmdir <dirname>\n");
1819                 return 1;
1820         }
1821         pstrcat(mask,buf);
1822
1823         if (!cli_rmdir(cli, mask)) {
1824                 d_printf("%s removing remote directory file %s\n",
1825                          cli_errstr(cli),mask);
1826         }
1827         
1828         return 0;
1829 }
1830
1831 /****************************************************************************
1832  UNIX hardlink.
1833 ****************************************************************************/
1834 static int cmd_link(void)
1835 {
1836         pstring src,dest;
1837         fstring buf,buf2;
1838   
1839         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1840                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1841                 return 1;
1842         }
1843
1844         pstrcpy(src,cur_dir);
1845         pstrcpy(dest,cur_dir);
1846   
1847         if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
1848             !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1849                 d_printf("link <src> <dest>\n");
1850                 return 1;
1851         }
1852
1853         pstrcat(src,buf);
1854         pstrcat(dest,buf2);
1855
1856         if (!cli_unix_hardlink(cli, src, dest)) {
1857                 d_printf("%s linking files (%s -> %s)\n", cli_errstr(cli), src, dest);
1858                 return 1;
1859         }  
1860
1861         return 0;
1862 }
1863
1864 /****************************************************************************
1865  UNIX symlink.
1866 ****************************************************************************/
1867
1868 static int cmd_symlink(void)
1869 {
1870         pstring src,dest;
1871         fstring buf,buf2;
1872   
1873         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1874                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1875                 return 1;
1876         }
1877
1878         pstrcpy(src,cur_dir);
1879         pstrcpy(dest,cur_dir);
1880         
1881         if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
1882             !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1883                 d_printf("symlink <src> <dest>\n");
1884                 return 1;
1885         }
1886
1887         pstrcat(src,buf);
1888         pstrcat(dest,buf2);
1889
1890         if (!cli_unix_symlink(cli, src, dest)) {
1891                 d_printf("%s symlinking files (%s -> %s)\n",
1892                         cli_errstr(cli), src, dest);
1893                 return 1;
1894         } 
1895
1896         return 0;
1897 }
1898
1899 /****************************************************************************
1900  UNIX chmod.
1901 ****************************************************************************/
1902
1903 static int cmd_chmod(void)
1904 {
1905         pstring src;
1906         mode_t mode;
1907         fstring buf, buf2;
1908   
1909         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1910                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1911                 return 1;
1912         }
1913
1914         pstrcpy(src,cur_dir);
1915         
1916         if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
1917             !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1918                 d_printf("chmod mode file\n");
1919                 return 1;
1920         }
1921
1922         mode = (mode_t)strtol(buf, NULL, 8);
1923         pstrcat(src,buf2);
1924
1925         if (!cli_unix_chmod(cli, src, mode)) {
1926                 d_printf("%s chmod file %s 0%o\n",
1927                         cli_errstr(cli), src, (unsigned int)mode);
1928                 return 1;
1929         } 
1930
1931         return 0;
1932 }
1933
1934 /****************************************************************************
1935  UNIX chown.
1936 ****************************************************************************/
1937
1938 static int cmd_chown(void)
1939 {
1940         pstring src;
1941         uid_t uid;
1942         gid_t gid;
1943         fstring buf, buf2, buf3;
1944   
1945         if (!(cli->transport->negotiate.capabilities & CAP_UNIX)) {
1946                 d_printf("Server doesn't support UNIX CIFS calls.\n");
1947                 return 1;
1948         }
1949
1950         pstrcpy(src,cur_dir);
1951         
1952         if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
1953             !next_token(NULL,buf2,NULL, sizeof(buf2)) ||
1954             !next_token(NULL,buf3,NULL, sizeof(buf3))) {
1955                 d_printf("chown uid gid file\n");
1956                 return 1;
1957         }
1958
1959         uid = (uid_t)atoi(buf);
1960         gid = (gid_t)atoi(buf2);
1961         pstrcat(src,buf3);
1962
1963         if (!cli_unix_chown(cli, src, uid, gid)) {
1964                 d_printf("%s chown file %s uid=%d, gid=%d\n",
1965                         cli_errstr(cli), src, (int)uid, (int)gid);
1966                 return 1;
1967         } 
1968
1969         return 0;
1970 }
1971
1972 /****************************************************************************
1973 rename some files
1974 ****************************************************************************/
1975 static int cmd_rename(void)
1976 {
1977         pstring src,dest;
1978         fstring buf,buf2;
1979   
1980         pstrcpy(src,cur_dir);
1981         pstrcpy(dest,cur_dir);
1982         
1983         if (!next_token_nr(NULL,buf,NULL,sizeof(buf)) || 
1984             !next_token_nr(NULL,buf2,NULL, sizeof(buf2))) {
1985                 d_printf("rename <src> <dest>\n");
1986                 return 1;
1987         }
1988
1989         pstrcat(src,buf);
1990         pstrcat(dest,buf2);
1991
1992         if (!cli_rename(cli, src, dest)) {
1993                 d_printf("%s renaming files\n",cli_errstr(cli));
1994                 return 1;
1995         }
1996         
1997         return 0;
1998 }
1999
2000
2001 /****************************************************************************
2002 toggle the prompt flag
2003 ****************************************************************************/
2004 static int cmd_prompt(void)
2005 {
2006         prompt = !prompt;
2007         DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
2008         
2009         return 1;
2010 }
2011
2012
2013 /****************************************************************************
2014 set the newer than time
2015 ****************************************************************************/
2016 static int cmd_newer(void)
2017 {
2018         fstring buf;
2019         BOOL ok;
2020         SMB_STRUCT_STAT sbuf;
2021
2022         ok = next_token_nr(NULL,buf,NULL,sizeof(buf));
2023         if (ok && (sys_stat(buf,&sbuf) == 0)) {
2024                 newer_than = sbuf.st_mtime;
2025                 DEBUG(1,("Getting files newer than %s",
2026                          asctime(LocalTime(&newer_than))));
2027         } else {
2028                 newer_than = 0;
2029         }
2030
2031         if (ok && newer_than == 0) {
2032                 d_printf("Error setting newer-than time\n");
2033                 return 1;
2034         }
2035
2036         return 0;
2037 }
2038
2039 /****************************************************************************
2040 set the archive level
2041 ****************************************************************************/
2042 static int cmd_archive(void)
2043 {
2044         fstring buf;
2045
2046         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2047                 archive_level = atoi(buf);
2048         } else
2049                 d_printf("Archive level is %d\n",archive_level);
2050
2051         return 0;
2052 }
2053
2054 /****************************************************************************
2055 toggle the lowercaseflag
2056 ****************************************************************************/
2057 static int cmd_lowercase(void)
2058 {
2059         lowercase = !lowercase;
2060         DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
2061
2062         return 0;
2063 }
2064
2065
2066
2067
2068 /****************************************************************************
2069 toggle the recurse flag
2070 ****************************************************************************/
2071 static int cmd_recurse(void)
2072 {
2073         recurse = !recurse;
2074         DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
2075
2076         return 0;
2077 }
2078
2079 /****************************************************************************
2080 toggle the translate flag
2081 ****************************************************************************/
2082 static int cmd_translate(void)
2083 {
2084         translation = !translation;
2085         DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
2086                  translation?"on":"off"));
2087
2088         return 0;
2089 }
2090
2091
2092 /****************************************************************************
2093 do a printmode command
2094 ****************************************************************************/
2095 static int cmd_printmode(void)
2096 {
2097         fstring buf;
2098         fstring mode;
2099
2100         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2101                 if (strequal(buf,"text")) {
2102                         printmode = 0;      
2103                 } else {
2104                         if (strequal(buf,"graphics"))
2105                                 printmode = 1;
2106                         else
2107                                 printmode = atoi(buf);
2108                 }
2109         }
2110
2111         switch(printmode)
2112                 {
2113                 case 0: 
2114                         fstrcpy(mode,"text");
2115                         break;
2116                 case 1: 
2117                         fstrcpy(mode,"graphics");
2118                         break;
2119                 default: 
2120                         slprintf(mode,sizeof(mode)-1,"%d",printmode);
2121                         break;
2122                 }
2123         
2124         DEBUG(2,("the printmode is now %s\n",mode));
2125
2126         return 0;
2127 }
2128
2129 /****************************************************************************
2130  do the lcd command
2131  ****************************************************************************/
2132 static int cmd_lcd(void)
2133 {
2134         fstring buf;
2135         pstring d;
2136         
2137         if (next_token_nr(NULL,buf,NULL,sizeof(buf)))
2138                 chdir(buf);
2139         DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
2140
2141         return 0;
2142 }
2143
2144 /****************************************************************************
2145  get a file restarting at end of local file
2146  ****************************************************************************/
2147 static int cmd_reget(void)
2148 {
2149         pstring local_name;
2150         pstring remote_name;
2151         char *p;
2152
2153         pstrcpy(remote_name, cur_dir);
2154         pstrcat(remote_name, "\\");
2155         
2156         p = remote_name + strlen(remote_name);
2157         
2158         if (!next_token_nr(NULL, p, NULL, sizeof(remote_name) - strlen(remote_name))) {
2159                 d_printf("reget <filename>\n");
2160                 return 1;
2161         }
2162         pstrcpy(local_name, p);
2163         dos_clean_name(remote_name);
2164         
2165         next_token_nr(NULL, local_name, NULL, sizeof(local_name));
2166         
2167         return do_get(remote_name, local_name, True);
2168 }
2169
2170 /****************************************************************************
2171  put a file restarting at end of local file
2172  ****************************************************************************/
2173 static int cmd_reput(void)
2174 {
2175         pstring local_name;
2176         pstring remote_name;
2177         fstring buf;
2178         char *p = buf;
2179         SMB_STRUCT_STAT st;
2180         
2181         pstrcpy(remote_name, cur_dir);
2182         pstrcat(remote_name, "\\");
2183   
2184         if (!next_token_nr(NULL, p, NULL, sizeof(buf))) {
2185                 d_printf("reput <filename>\n");
2186                 return 1;
2187         }
2188         pstrcpy(local_name, p);
2189   
2190         if (!file_exist(local_name, &st)) {
2191                 d_printf("%s does not exist\n", local_name);
2192                 return 1;
2193         }
2194
2195         if (next_token_nr(NULL, p, NULL, sizeof(buf)))
2196                 pstrcat(remote_name, p);
2197         else
2198                 pstrcat(remote_name, local_name);
2199         
2200         dos_clean_name(remote_name);
2201
2202         return do_put(remote_name, local_name, True);
2203 }
2204
2205
2206 /****************************************************************************
2207 try and browse available connections on a host
2208 ****************************************************************************/
2209 static BOOL browse_host(BOOL sort)
2210 {
2211         d_printf("REWRITE: host browsing not implemented\n");
2212
2213         return False;
2214 }
2215
2216 /****************************************************************************
2217 try and browse available connections on a host
2218 ****************************************************************************/
2219 static BOOL list_servers(char *wk_grp)
2220 {
2221         d_printf("REWRITE: list servers not implemented\n");
2222         return False;
2223 }
2224
2225 /* Some constants for completing filename arguments */
2226
2227 #define COMPL_NONE        0          /* No completions */
2228 #define COMPL_REMOTE      1          /* Complete remote filename */
2229 #define COMPL_LOCAL       2          /* Complete local filename */
2230
2231 /* This defines the commands supported by this client.
2232  * NOTE: The "!" must be the last one in the list because it's fn pointer
2233  *       field is NULL, and NULL in that field is used in process_tok()
2234  *       (below) to indicate the end of the list.  crh
2235  */
2236 static struct
2237 {
2238   const char *name;
2239   int (*fn)(void);
2240   const char *description;
2241   char compl_args[2];      /* Completion argument info */
2242 } commands[] = 
2243 {
2244   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2245   {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
2246   {"acl",cmd_acl,"<file> show file ACL",{COMPL_NONE,COMPL_NONE}},
2247   {"allinfo",cmd_allinfo,"<file> show all possible info about a file",{COMPL_NONE,COMPL_NONE}},
2248   {"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}},
2249   {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
2250   {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
2251   {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
2252   {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
2253   {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
2254   {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2255   {"deltree",cmd_deltree,"<dir> delete a whole directory tree",{COMPL_REMOTE,COMPL_NONE}},
2256   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2257   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2258   {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2259   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
2260   {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
2261   {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
2262   {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
2263   {"link",cmd_link,"<src> <dest> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
2264   {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
2265   {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
2266   {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
2267   {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2268   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
2269   {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
2270   {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
2271   {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
2272   {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
2273   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
2274   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
2275   {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
2276   {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
2277   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
2278   {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
2279   {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2280   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
2281   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
2282   {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2283   {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
2284   {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
2285   {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
2286   {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
2287   {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
2288   {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
2289   {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
2290   {"symlink",cmd_symlink,"<src> <dest> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
2291   {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
2292   {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
2293   {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
2294   
2295   /* Yes, this must be here, see crh's comment above. */
2296   {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
2297   {NULL,NULL,NULL,{COMPL_NONE,COMPL_NONE}}
2298 };
2299
2300
2301 /*******************************************************************
2302   lookup a command string in the list of commands, including 
2303   abbreviations
2304   ******************************************************************/
2305 static int process_tok(fstring tok)
2306 {
2307         int i = 0, matches = 0;
2308         int cmd=0;
2309         int tok_len = strlen(tok);
2310         
2311         while (commands[i].fn != NULL) {
2312                 if (strequal(commands[i].name,tok)) {
2313                         matches = 1;
2314                         cmd = i;
2315                         break;
2316                 } else if (strnequal(commands[i].name, tok, tok_len)) {
2317                         matches++;
2318                         cmd = i;
2319                 }
2320                 i++;
2321         }
2322   
2323         if (matches == 0)
2324                 return(-1);
2325         else if (matches == 1)
2326                 return(cmd);
2327         else
2328                 return(-2);
2329 }
2330
2331 /****************************************************************************
2332 help
2333 ****************************************************************************/
2334 static int cmd_help(void)
2335 {
2336         int i=0,j;
2337         fstring buf;
2338         
2339         if (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
2340                 if ((i = process_tok(buf)) >= 0)
2341                         d_printf("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
2342         } else {
2343                 while (commands[i].description) {
2344                         for (j=0; commands[i].description && (j<5); j++) {
2345                                 d_printf("%-15s",commands[i].name);
2346                                 i++;
2347                         }
2348                         d_printf("\n");
2349                 }
2350         }
2351         return 0;
2352 }
2353
2354 /****************************************************************************
2355 process a -c command string
2356 ****************************************************************************/
2357 static int process_command_string(char *cmd)
2358 {
2359         pstring line;
2360         const char *ptr;
2361         int rc = 0;
2362
2363         /* establish the connection if not already */
2364         
2365         if (!cli) {
2366                 cli = do_connect(desthost, service);
2367                 if (!cli)
2368                         return 0;
2369         }
2370         
2371         while (cmd[0] != '\0')    {
2372                 char *p;
2373                 fstring tok;
2374                 int i;
2375                 
2376                 if ((p = strchr_m(cmd, ';')) == 0) {
2377                         strncpy(line, cmd, 999);
2378                         line[1000] = '\0';
2379                         cmd += strlen(cmd);
2380                 } else {
2381                         if (p - cmd > 999) p = cmd + 999;
2382                         strncpy(line, cmd, p - cmd);
2383                         line[p - cmd] = '\0';
2384                         cmd = p + 1;
2385                 }
2386                 
2387                 /* and get the first part of the command */
2388                 ptr = line;
2389                 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2390                 
2391                 if ((i = process_tok(tok)) >= 0) {
2392                         rc = commands[i].fn();
2393                 } else if (i == -2) {
2394                         d_printf("%s: command abbreviation ambiguous\n",tok);
2395                 } else {
2396                         d_printf("%s: command not found\n",tok);
2397                 }
2398         }
2399         
2400         return rc;
2401 }       
2402
2403 #define MAX_COMPLETIONS 100
2404
2405 typedef struct {
2406         pstring dirmask;
2407         char **matches;
2408         int count, samelen;
2409         const char *text;
2410         int len;
2411 } completion_remote_t;
2412
2413 static void completion_remote_filter(file_info *f, const char *mask, void *state)
2414 {
2415         completion_remote_t *info = (completion_remote_t *)state;
2416
2417         if ((info->count < MAX_COMPLETIONS - 1) && (strncmp(info->text, f->name, info->len) == 0) && (strcmp(f->name, ".") != 0) && (strcmp(f->name, "..") != 0)) {
2418                 if ((info->dirmask[0] == 0) && !(f->mode & FILE_ATTRIBUTE_DIRECTORY))
2419                         info->matches[info->count] = strdup(f->name);
2420                 else {
2421                         pstring tmp;
2422
2423                         if (info->dirmask[0] != 0)
2424                                 pstrcpy(tmp, info->dirmask);
2425                         else
2426                                 tmp[0] = 0;
2427                         pstrcat(tmp, f->name);
2428                         if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
2429                                 pstrcat(tmp, "/");
2430                         info->matches[info->count] = strdup(tmp);
2431                 }
2432                 if (info->matches[info->count] == NULL)
2433                         return;
2434                 if (f->mode & FILE_ATTRIBUTE_DIRECTORY)
2435                         smb_readline_ca_char(0);
2436
2437                 if (info->count == 1)
2438                         info->samelen = strlen(info->matches[info->count]);
2439                 else
2440                         while (strncmp(info->matches[info->count], info->matches[info->count-1], info->samelen) != 0)
2441                                 info->samelen--;
2442                 info->count++;
2443         }
2444 }
2445
2446 static char **remote_completion(const char *text, int len)
2447 {
2448         pstring dirmask;
2449         int i;
2450         completion_remote_t info = { "", NULL, 1, NULL, NULL, NULL };
2451
2452         info.samelen = len;
2453         info.text = text;
2454         info.len = len;
2455  
2456         if (len >= PATH_MAX)
2457                 return(NULL);
2458
2459         info.matches = (char **)malloc(sizeof(info.matches[0])*MAX_COMPLETIONS);
2460         if (!info.matches) return NULL;
2461         info.matches[0] = NULL;
2462
2463         for (i = len-1; i >= 0; i--)
2464                 if ((text[i] == '/') || (text[i] == '\\'))
2465                         break;
2466         info.text = text+i+1;
2467         info.samelen = info.len = len-i-1;
2468
2469         if (i > 0) {
2470                 strncpy(info.dirmask, text, i+1);
2471                 info.dirmask[i+1] = 0;
2472                 snprintf(dirmask, sizeof(dirmask), "%s%*s*", cur_dir, i-1, text);
2473         } else
2474                 snprintf(dirmask, sizeof(dirmask), "%s*", cur_dir);
2475
2476         if (cli_list(cli, dirmask, 
2477                      FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN, 
2478                      completion_remote_filter, &info) < 0)
2479                 goto cleanup;
2480
2481         if (info.count == 2)
2482                 info.matches[0] = strdup(info.matches[1]);
2483         else {
2484                 info.matches[0] = malloc(info.samelen+1);
2485                 if (!info.matches[0])
2486                         goto cleanup;
2487                 strncpy(info.matches[0], info.matches[1], info.samelen);
2488                 info.matches[0][info.samelen] = 0;
2489         }
2490         info.matches[info.count] = NULL;
2491         return info.matches;
2492
2493 cleanup:
2494         for (i = 0; i < info.count; i++)
2495                 free(info.matches[i]);
2496         free(info.matches);
2497         return NULL;
2498 }
2499
2500 static char **completion_fn(const char *text, int start, int end)
2501 {
2502         smb_readline_ca_char(' ');
2503
2504         if (start) {
2505                 const char *buf, *sp;
2506                 int i;
2507                 char compl_type;
2508
2509                 buf = smb_readline_get_line_buffer();
2510                 if (buf == NULL)
2511                         return NULL;
2512                 
2513                 sp = strchr(buf, ' ');
2514                 if (sp == NULL)
2515                         return NULL;
2516                 
2517                 for (i = 0; commands[i].name; i++)
2518                         if ((strncmp(commands[i].name, text, sp - buf) == 0) && (commands[i].name[sp - buf] == 0))
2519                                 break;
2520                 if (commands[i].name == NULL)
2521                         return NULL;
2522
2523                 while (*sp == ' ')
2524                         sp++;
2525
2526                 if (sp == (buf + start))
2527                         compl_type = commands[i].compl_args[0];
2528                 else
2529                         compl_type = commands[i].compl_args[1];
2530
2531                 if (compl_type == COMPL_REMOTE)
2532                         return remote_completion(text, end - start);
2533                 else /* fall back to local filename completion */
2534                         return NULL;
2535         } else {
2536                 char **matches;
2537                 int i, len, samelen, count=1;
2538
2539                 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
2540                 if (!matches) return NULL;
2541                 matches[0] = NULL;
2542
2543                 len = strlen(text);
2544                 for (i=0;commands[i].fn && count < MAX_COMPLETIONS-1;i++) {
2545                         if (strncmp(text, commands[i].name, len) == 0) {
2546                                 matches[count] = strdup(commands[i].name);
2547                                 if (!matches[count])
2548                                         goto cleanup;
2549                                 if (count == 1)
2550                                         samelen = strlen(matches[count]);
2551                                 else
2552                                         while (strncmp(matches[count], matches[count-1], samelen) != 0)
2553                                                 samelen--;
2554                                 count++;
2555                         }
2556                 }
2557
2558                 switch (count) {
2559                 case 0: /* should never happen */
2560                 case 1:
2561                         goto cleanup;
2562                 case 2:
2563                         matches[0] = strdup(matches[1]);
2564                         break;
2565                 default:
2566                         matches[0] = malloc(samelen+1);
2567                         if (!matches[0])
2568                                 goto cleanup;
2569                         strncpy(matches[0], matches[1], samelen);
2570                         matches[0][samelen] = 0;
2571                 }
2572                 matches[count] = NULL;
2573                 return matches;
2574
2575 cleanup:
2576                 while (i >= 0) {
2577                         free(matches[i]);
2578                         i--;
2579                 }
2580                 free(matches);
2581                 return NULL;
2582         }
2583 }
2584
2585 /****************************************************************************
2586 make sure we swallow keepalives during idle time
2587 ****************************************************************************/
2588 static void readline_callback(void)
2589 {
2590         fd_set fds;
2591         struct timeval timeout;
2592         static time_t last_t;
2593         time_t t;
2594
2595         t = time(NULL);
2596
2597         if (t - last_t < 5) return;
2598
2599         last_t = t;
2600
2601  again:
2602         if (cli->transport->socket->fd == -1)
2603                 return;
2604
2605         FD_ZERO(&fds);
2606         FD_SET(cli->transport->socket->fd, &fds);
2607
2608         timeout.tv_sec = 0;
2609         timeout.tv_usec = 0;
2610         sys_select_intr(cli->transport->socket->fd+1,&fds,NULL,NULL,&timeout);
2611                 
2612         /* We deliberately use cli_swallow_keepalives instead of
2613            client_receive_smb as we want to receive
2614            session keepalives and then drop them here.
2615         */
2616         if (FD_ISSET(cli->transport->socket->fd, &fds)) {
2617                 if (!cli_request_receive_next(cli->transport)) {
2618                         d_printf("Lost connection to server\n");
2619                         exit(1);
2620                 }
2621                 goto again;
2622         }
2623
2624         if (cli->tree) {
2625                 cli_chkpath(cli, "\\");
2626         }
2627 }
2628
2629
2630 /****************************************************************************
2631 process commands on stdin
2632 ****************************************************************************/
2633 static void process_stdin(void)
2634 {
2635         const char *ptr;
2636
2637         while (1) {
2638                 fstring tok;
2639                 fstring the_prompt;
2640                 char *cline;
2641                 pstring line;
2642                 int i;
2643                 
2644                 /* display a prompt */
2645                 slprintf(the_prompt, sizeof(the_prompt)-1, "smb: %s> ", cur_dir);
2646                 cline = smb_readline(the_prompt, readline_callback, completion_fn);
2647                         
2648                 if (!cline) break;
2649                 
2650                 pstrcpy(line, cline);
2651
2652                 /* special case - first char is ! */
2653                 if (*line == '!') {
2654                         system(line + 1);
2655                         continue;
2656                 }
2657       
2658                 /* and get the first part of the command */
2659                 ptr = line;
2660                 if (!next_token_nr(&ptr,tok,NULL,sizeof(tok))) continue;
2661
2662                 if ((i = process_tok(tok)) >= 0) {
2663                         commands[i].fn();
2664                 } else if (i == -2) {
2665                         d_printf("%s: command abbreviation ambiguous\n",tok);
2666                 } else {
2667                         d_printf("%s: command not found\n",tok);
2668                 }
2669         }
2670 }
2671
2672
2673 /***************************************************** 
2674 return a connection to a server
2675 *******************************************************/
2676 static struct cli_state *do_connect(const char *server, const char *share)
2677 {
2678         struct cli_state *c;
2679         struct nmb_name called, calling;
2680         const char *server_n;
2681         struct in_addr ip;
2682         fstring servicename;
2683         char *sharename;
2684         
2685         /* make a copy so we don't modify the global string 'service' */
2686         fstrcpy(servicename, share);
2687         sharename = servicename;
2688         if (*sharename == '\\') {
2689                 server = sharename+2;
2690                 sharename = strchr_m(server,'\\');
2691                 if (!sharename) return NULL;
2692                 *sharename = 0;
2693                 sharename++;
2694         }
2695
2696         asprintf(&sharename, "\\\\%s\\%s", server, sharename);
2697
2698         server_n = server;
2699         
2700         zero_ip(&ip);
2701
2702         make_nmb_name(&calling, lp_netbios_name(), 0x0);
2703         make_nmb_name(&called , server, name_type);
2704
2705  again:
2706         zero_ip(&ip);
2707         if (have_ip) ip = dest_ip;
2708
2709         /* have to open a new connection */
2710         if (!(c=cli_state_init()) || !cli_socket_connect(c, server_n, &ip)) {
2711                 d_printf("Connection to %s failed\n", server_n);
2712                 return NULL;
2713         }
2714
2715         if (!cli_transport_establish(c, &calling, &called)) {
2716                 char *p;
2717                 d_printf("session request to %s failed (%s)\n", 
2718                          called.name, cli_errstr(c));
2719                 cli_shutdown(c);
2720                 if ((p=strchr_m(called.name, '.'))) {
2721                         *p = 0;
2722                         goto again;
2723                 }
2724                 if (strcmp(called.name, "*SMBSERVER")) {
2725                         make_nmb_name(&called , "*SMBSERVER", 0x20);
2726                         goto again;
2727                 }
2728                 return NULL;
2729         }
2730
2731         DEBUG(4,(" session request ok\n"));
2732
2733         if (!cli_negprot(c)) {
2734                 d_printf("protocol negotiation failed\n");
2735                 cli_shutdown(c);
2736                 return NULL;
2737         }
2738
2739         if (!got_pass) {
2740                 const char *pass = getpass("Password: ");
2741                 if (pass) {
2742                         pstrcpy(password, pass);
2743                 }
2744         }
2745
2746         if (!cli_session_setup(c, username, password, 
2747                                lp_workgroup())) {
2748                 /* if a password was not supplied then try again with a null username */
2749                 if (password[0] || !username[0] || use_kerberos ||
2750                     !cli_session_setup(c, "", "", lp_workgroup())) { 
2751                         d_printf("session setup failed: %s\n", cli_errstr(c));
2752                         cli_shutdown(c);
2753                         return NULL;
2754                 }
2755                 d_printf("Anonymous login successful\n");
2756         }
2757
2758         DEBUG(4,(" session setup ok\n"));
2759
2760         if (!cli_send_tconX(c, sharename, "?????", password)) {
2761                 d_printf("tree connect failed: %s\n", cli_errstr(c));
2762                 cli_shutdown(c);
2763                 return NULL;
2764         }
2765
2766         DEBUG(4,(" tconx ok\n"));
2767
2768         return c;
2769 }
2770
2771
2772 /****************************************************************************
2773   process commands from the client
2774 ****************************************************************************/
2775 static int process(char *base_directory)
2776 {
2777         int rc = 0;
2778
2779         cli = do_connect(desthost, service);
2780         if (!cli) {
2781                 return 1;
2782         }
2783
2784         if (*base_directory) do_cd(base_directory);
2785         
2786         if (cmdstr) {
2787                 rc = process_command_string(cmdstr);
2788         } else {
2789                 process_stdin();
2790         }
2791   
2792         cli_shutdown(cli);
2793         return rc;
2794 }
2795
2796 /****************************************************************************
2797 handle a -L query
2798 ****************************************************************************/
2799 static int do_host_query(char *query_host)
2800 {
2801         cli = do_connect(query_host, "IPC$");
2802         if (!cli)
2803                 return 1;
2804
2805         browse_host(True);
2806         list_servers(lp_workgroup());
2807
2808         cli_shutdown(cli);
2809         
2810         return(0);
2811 }
2812
2813
2814 /****************************************************************************
2815 handle a tar operation
2816 ****************************************************************************/
2817 static int do_tar_op(char *base_directory)
2818 {
2819         int ret;
2820
2821         /* do we already have a connection? */
2822         if (!cli) {
2823                 cli = do_connect(desthost, service);    
2824                 if (!cli)
2825                         return 1;
2826         }
2827
2828         recurse=True;
2829
2830         if (*base_directory) do_cd(base_directory);
2831         
2832         ret=process_tar();
2833
2834         cli_shutdown(cli);
2835
2836         return(ret);
2837 }
2838
2839 /****************************************************************************
2840 handle a message operation
2841 ****************************************************************************/
2842 static int do_message_op(void)
2843 {
2844         struct in_addr ip;
2845         struct nmb_name called, calling;
2846         fstring server_name;
2847         char name_type_hex[10];
2848
2849         make_nmb_name(&calling, lp_netbios_name(), 0x0);
2850         make_nmb_name(&called , desthost, name_type);
2851
2852         fstrcpy(server_name, desthost);
2853         snprintf(name_type_hex, sizeof(name_type_hex), "#%X", name_type);
2854         fstrcat(server_name, name_type_hex);
2855
2856         zero_ip(&ip);
2857         if (have_ip) ip = dest_ip;
2858
2859         if (!(cli=cli_state_init()) || !cli_socket_connect(cli, server_name, &ip)) {
2860                 d_printf("Connection to %s failed\n", desthost);
2861                 return 1;
2862         }
2863
2864         if (!cli_transport_establish(cli, &calling, &called)) {
2865                 d_printf("session request failed\n");
2866                 cli_shutdown(cli);
2867                 return 1;
2868         }
2869
2870         send_message();
2871         cli_shutdown(cli);
2872
2873         return 0;
2874 }
2875
2876
2877 /**
2878  * Process "-L hostname" option.
2879  *
2880  * We don't actually do anything yet -- we just stash the name in a
2881  * global variable and do the query when all options have been read.
2882  **/
2883 static void remember_query_host(const char *arg,
2884                                 pstring query_host)
2885 {
2886         char *slash;
2887         
2888         while (*arg == '\\' || *arg == '/')
2889                 arg++;
2890         pstrcpy(query_host, arg);
2891         if ((slash = strchr(query_host, '/'))
2892             || (slash = strchr(query_host, '\\'))) {
2893                 *slash = 0;
2894         }
2895 }
2896
2897
2898 /****************************************************************************
2899   main program
2900 ****************************************************************************/
2901  int main(int argc,char *argv[])
2902 {
2903         fstring base_directory;
2904         int opt;
2905         pstring query_host;
2906         BOOL message = False;
2907         extern char tar_type;
2908         pstring term_code;
2909         poptContext pc;
2910         char *p;
2911         int rc = 0;
2912         TALLOC_CTX *mem_ctx;
2913         struct poptOption long_options[] = {
2914                 POPT_AUTOHELP
2915
2916                 { "name-resolve", 'R', POPT_ARG_STRING, NULL, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
2917                 { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
2918                 { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
2919                 { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
2920                 { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
2921                 { "terminal", 't', POPT_ARG_STRING, NULL, 't', "Terminal I/O code {sjis|euc|jis7|jis8|junet|hex}", "CODE" },
2922                 { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
2923                 { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
2924                 { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
2925                 { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" }, 
2926                 { "send-buffer", 'b', POPT_ARG_INT, NULL, 'b', "Changes the transmit/send buffer", "BYTES" },
2927                 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
2928                 POPT_COMMON_SAMBA
2929                 POPT_COMMON_CONNECTION
2930                 POPT_COMMON_CREDENTIALS
2931                 POPT_TABLEEND
2932         };
2933         
2934
2935 #ifdef KANJI
2936         pstrcpy(term_code, KANJI);
2937 #else /* KANJI */
2938         *term_code = 0;
2939 #endif /* KANJI */
2940
2941         *query_host = 0;
2942         *base_directory = 0;
2943
2944         setup_logging(argv[0],DEBUG_STDOUT);
2945         mem_ctx = talloc_init("client.c/main");
2946         if (!mem_ctx) {
2947                 d_printf("\nclient.c: Not enough memory\n");
2948                 exit(1);
2949         }
2950
2951         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
2952                 fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
2953                         argv[0], dyn_CONFIGFILE);
2954         }
2955         
2956         pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
2957                                 POPT_CONTEXT_KEEP_FIRST);
2958         poptSetOtherOptionHelp(pc, "service <password>");
2959
2960         in_client = True;   /* Make sure that we tell lp_load we are */
2961
2962         while ((opt = poptGetNextOpt(pc)) != -1) {
2963                 switch (opt) {
2964                 case 'M':
2965                         /* Messages are sent to NetBIOS name type 0x3
2966                          * (Messenger Service).  Make sure we default
2967                          * to port 139 instead of port 445. srl,crh
2968                          */
2969                         name_type = 0x03; 
2970                         pstrcpy(desthost,poptGetOptArg(pc));
2971                         if( 0 == port ) port = 139;
2972                         message = True;
2973                         break;
2974                 case 'I':
2975                         {
2976                                 dest_ip = *interpret_addr2(mem_ctx, poptGetOptArg(pc));
2977                                 if (is_zero_ip(dest_ip))
2978                                         exit(1);
2979                                 have_ip = True;
2980                         }
2981                         break;
2982                 case 'E':
2983                         setup_logging("client", DEBUG_STDERR);
2984                         break;
2985
2986                 case 'L':
2987                         remember_query_host(poptGetOptArg(pc), query_host);
2988                         break;
2989                 case 't':
2990                         pstrcpy(term_code, poptGetOptArg(pc));
2991                         break;
2992                 case 'm':
2993                         lp_set_cmdline("max protocol", poptGetOptArg(pc));
2994                         break;
2995                 case 'R':
2996                         lp_set_cmdline("name resolve order", poptGetOptArg(pc));
2997                         break;
2998                 case 'T':
2999                         if (!tar_parseargs(argc, argv, poptGetOptArg(pc), optind)) {
3000                                 poptPrintUsage(pc, stderr, 0);
3001                                 exit(1);
3002                         }
3003                         break;
3004                 case 'D':
3005                         fstrcpy(base_directory,poptGetOptArg(pc));
3006                         break;
3007                 case 'b':
3008                         io_bufsize = MAX(1, atoi(poptGetOptArg(pc)));
3009                         break;
3010                 }
3011         }
3012
3013         poptGetArg(pc);
3014         
3015         load_interfaces();
3016
3017         if(poptPeekArg(pc)) {
3018                 pstrcpy(service,poptGetArg(pc));  
3019                 /* Convert any '/' characters in the service name to '\' characters */
3020                 string_replace(service, '/','\\');
3021
3022                 if (count_chars(service,'\\') < 3) {
3023                         d_printf("\n%s: Not enough '\\' characters in service\n",service);
3024                         poptPrintUsage(pc, stderr, 0);
3025                         exit(1);
3026                 }
3027         }
3028
3029         if (poptPeekArg(pc)) { 
3030                 cmdline_auth_info.got_pass = True;
3031                 pstrcpy(cmdline_auth_info.password,poptGetArg(pc));  
3032         }
3033
3034         /*init_names(); */
3035
3036         if (!tar_type && !*query_host && !*service && !message) {
3037                 poptPrintUsage(pc, stderr, 0);
3038                 exit(1);
3039         }
3040
3041         poptFreeContext(pc);
3042
3043         pstrcpy(username, cmdline_auth_info.username);
3044         pstrcpy(password, cmdline_auth_info.password);
3045         use_kerberos = cmdline_auth_info.use_kerberos;
3046         got_pass = cmdline_auth_info.got_pass;
3047
3048         DEBUG( 3, ( "Client started (version %s).\n", SAMBA_VERSION ) );
3049
3050         talloc_destroy(mem_ctx);
3051         if (tar_type) {
3052                 if (cmdstr)
3053                         process_command_string(cmdstr);
3054                 return do_tar_op(base_directory);
3055         }
3056
3057         if ((p=strchr_m(query_host,'#'))) {
3058                 *p = 0;
3059                 p++;
3060                 sscanf(p, "%x", &name_type);
3061         }
3062   
3063         if (*query_host) {
3064                 return do_host_query(query_host);
3065         }
3066
3067         if (message) {
3068                 return do_message_op();
3069         }
3070         
3071         if (process(base_directory)) {
3072                 return 1;
3073         }
3074
3075         return rc;
3076 }