client/client.c: I18N fixes.
[kai/samba.git] / source3 / client / client.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 #ifndef REGISTER
27 #define REGISTER 0
28 #endif
29
30 struct cli_state *cli;
31 extern BOOL in_client;
32 static int port = SMB_PORT;
33 pstring cur_dir = "\\";
34 pstring cd_path = "";
35 static pstring service;
36 static pstring desthost;
37 extern pstring global_myname;
38 static pstring password;
39 static pstring username;
40 static pstring workgroup;
41 static char *cmdstr;
42 static BOOL got_pass;
43 static int io_bufsize = 65520;
44 extern struct in_addr ipzero;
45 extern pstring scope;
46
47 static int name_type = 0x20;
48
49 extern pstring user_socket_options;
50
51 static int process_tok(fstring tok);
52 static void cmd_help(void);
53
54 /* 30 second timeout on most commands */
55 #define CLIENT_TIMEOUT (30*1000)
56 #define SHORT_TIMEOUT (5*1000)
57
58 /* value for unused fid field in trans2 secondary request */
59 #define FID_UNUSED (0xFFFF)
60
61 time_t newer_than = 0;
62 int archive_level = 0;
63
64 extern pstring debugf;
65 extern int DEBUGLEVEL;
66
67 BOOL translation = False;
68
69 static BOOL have_ip;
70
71 /* clitar bits insert */
72 extern int blocksize;
73 extern BOOL tar_inc;
74 extern BOOL tar_reset;
75 /* clitar bits end */
76  
77
78 mode_t myumask = 0755;
79
80 BOOL prompt = True;
81
82 int printmode = 1;
83
84 static BOOL recurse = False;
85 BOOL lowercase = False;
86
87 struct in_addr dest_ip;
88
89 #define SEPARATORS " \t\n\r"
90
91 BOOL abort_mget = True;
92
93 pstring fileselection = "";
94
95 extern file_info def_finfo;
96
97 /* timing globals */
98 int get_total_size = 0;
99 int get_total_time_ms = 0;
100 int put_total_size = 0;
101 int put_total_time_ms = 0;
102
103 /* totals globals */
104 static double dir_total;
105
106 #define USENMB
107
108 /****************************************************************************
109 write to a local file with CR/LF->LF translation if appropriate. return the 
110 number taken from the buffer. This may not equal the number written.
111 ****************************************************************************/
112 static int writefile(int f, char *b, int n)
113 {
114         int i;
115
116         if (!translation) {
117                 return write(f,b,n);
118         }
119
120         i = 0;
121         while (i < n) {
122                 if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
123                         b++;i++;
124                 }
125                 if (write(f, b, 1) != 1) {
126                         break;
127                 }
128                 b++;
129                 i++;
130         }
131   
132         return(i);
133 }
134
135 /****************************************************************************
136   read from a file with LF->CR/LF translation if appropriate. return the 
137   number read. read approx n bytes.
138 ****************************************************************************/
139 static int readfile(char *b, int size, int n, FILE *f)
140 {
141         int i;
142         int c;
143
144         if (!translation || (size != 1))
145                 return(fread(b,size,n,f));
146   
147         i = 0;
148         while (i < (n - 1) && (i < BUFFER_SIZE)) {
149                 if ((c = getc(f)) == EOF) {
150                         break;
151                 }
152       
153                 if (c == '\n') { /* change all LFs to CR/LF */
154                         b[i++] = '\r';
155                 }
156       
157                 b[i++] = c;
158         }
159   
160         return(i);
161 }
162  
163
164 /****************************************************************************
165 send a message
166 ****************************************************************************/
167 static void send_message(void)
168 {
169         int total_len = 0;
170         int grp_id;
171
172         if (!cli_message_start(cli, desthost, username, &grp_id)) {
173                 DEBUG(0,("message start: %s\n", cli_errstr(cli)));
174                 return;
175         }
176
177
178         printf("Connected. Type your message, ending it with a Control-D\n");
179
180         while (!feof(stdin) && total_len < 1600) {
181                 int maxlen = MIN(1600 - total_len,127);
182                 pstring msg;
183                 int l=0;
184                 int c;
185
186                 ZERO_ARRAY(msg);
187
188                 for (l=0;l<maxlen && (c=fgetc(stdin))!=EOF;l++) {
189                         if (c == '\n')
190                                 msg[l++] = '\r';
191                         msg[l] = c;   
192                 }
193
194                 if (!cli_message_text(cli, msg, l, grp_id)) {
195                         printf("SMBsendtxt failed (%s)\n",cli_errstr(cli));
196                         return;
197                 }      
198                 
199                 total_len += l;
200         }
201
202         if (total_len >= 1600)
203                 printf("the message was truncated to 1600 bytes\n");
204         else
205                 printf("sent %d bytes\n",total_len);
206
207         if (!cli_message_end(cli, grp_id)) {
208                 printf("SMBsendend failed (%s)\n",cli_errstr(cli));
209                 return;
210         }      
211 }
212
213
214
215 /****************************************************************************
216 check the space on a device
217 ****************************************************************************/
218 static void do_dskattr(void)
219 {
220         int total, bsize, avail;
221
222         if (!cli_dskattr(cli, &bsize, &total, &avail)) {
223                 DEBUG(0,("Error in dskattr: %s\n",cli_errstr(cli))); 
224                 return;
225         }
226
227         DEBUG(0,("\n\t\t%d blocks of size %d. %d blocks available\n",
228                  total, bsize, avail));
229 }
230
231 /****************************************************************************
232 show cd/pwd
233 ****************************************************************************/
234 static void cmd_pwd(void)
235 {
236         DEBUG(0,("Current directory is %s",service));
237         DEBUG(0,("%s\n",cur_dir));
238 }
239
240
241 /****************************************************************************
242 change directory - inner section
243 ****************************************************************************/
244 static void do_cd(char *newdir)
245 {
246         char *p = newdir;
247         pstring saved_dir;
248         pstring dname;
249       
250         dos_format(newdir);
251
252         /* Save the current directory in case the
253            new directory is invalid */
254         pstrcpy(saved_dir, cur_dir);
255         if (*p == '\\')
256                 pstrcpy(cur_dir,p);
257         else
258                 pstrcat(cur_dir,p);
259         if (*(cur_dir+strlen(cur_dir)-1) != '\\') {
260                 pstrcat(cur_dir, "\\");
261         }
262         dos_clean_name(cur_dir);
263         pstrcpy(dname,cur_dir);
264         pstrcat(cur_dir,"\\");
265         dos_clean_name(cur_dir);
266         
267         if (!strequal(cur_dir,"\\")) {
268                 if (!cli_chkpath(cli, dname)) {
269                         DEBUG(0,("cd %s: %s\n", dname, cli_errstr(cli)));
270                         pstrcpy(cur_dir,saved_dir);
271                 }
272         }
273         
274         pstrcpy(cd_path,cur_dir);
275 }
276
277 /****************************************************************************
278 change directory
279 ****************************************************************************/
280 static void cmd_cd(void)
281 {
282         fstring buf;
283
284         if (next_token(NULL,buf,NULL,sizeof(buf)))
285                 do_cd(buf);
286         else
287                 DEBUG(0,("Current directory is %s\n",cur_dir));
288 }
289
290
291 /*******************************************************************
292   decide if a file should be operated on
293   ********************************************************************/
294 static BOOL do_this_one(file_info *finfo)
295 {
296         if (finfo->mode & aDIR) return(True);
297
298         if (*fileselection && 
299             !mask_match(finfo->name,fileselection,False,False)) {
300                 DEBUG(3,("match_match %s failed\n", finfo->name));
301                 return False;
302         }
303
304         if (newer_than && finfo->mtime < newer_than) {
305                 DEBUG(3,("newer_than %s failed\n", finfo->name));
306                 return(False);
307         }
308
309         if ((archive_level==1 || archive_level==2) && !(finfo->mode & aARCH)) {
310                 DEBUG(3,("archive %s failed\n", finfo->name));
311                 return(False);
312         }
313         
314         return(True);
315 }
316
317 /****************************************************************************
318   display info about a file
319   ****************************************************************************/
320 static void display_finfo(file_info *finfo)
321 {
322         if (do_this_one(finfo)) {
323                 time_t t = finfo->mtime; /* the time is assumed to be passed as GMT */
324                 DEBUG(0,("  %-30s%7.7s %8.0f  %s",
325                          finfo->name,
326                          attrib_string(finfo->mode),
327                          (double)finfo->size,
328                          asctime(LocalTime(&t))));
329                 dir_total += finfo->size;
330         }
331 }
332
333
334 /****************************************************************************
335    accumulate size of a file
336   ****************************************************************************/
337 static void do_du(file_info *finfo)
338 {
339         if (do_this_one(finfo)) {
340                 dir_total += finfo->size;
341         }
342 }
343
344 static BOOL do_list_recurse;
345 static BOOL do_list_dirs;
346 static char *do_list_queue = 0;
347 static long do_list_queue_size = 0;
348 static long do_list_queue_start = 0;
349 static long do_list_queue_end = 0;
350 static void (*do_list_fn)(file_info *);
351
352 /****************************************************************************
353 functions for do_list_queue
354   ****************************************************************************/
355
356 /*
357  * The do_list_queue is a NUL-separated list of strings stored in a
358  * char*.  Since this is a FIFO, we keep track of the beginning and
359  * ending locations of the data in the queue.  When we overflow, we
360  * double the size of the char*.  When the start of the data passes
361  * the midpoint, we move everything back.  This is logically more
362  * complex than a linked list, but easier from a memory management
363  * angle.  In any memory error condition, do_list_queue is reset.
364  * Functions check to ensure that do_list_queue is non-NULL before
365  * accessing it.
366  */
367 static void reset_do_list_queue(void)
368 {
369         if (do_list_queue)
370         {
371                 free(do_list_queue);
372         }
373         do_list_queue = 0;
374         do_list_queue_size = 0;
375         do_list_queue_start = 0;
376         do_list_queue_end = 0;
377 }
378
379 static void init_do_list_queue(void)
380 {
381         reset_do_list_queue();
382         do_list_queue_size = 1024;
383         do_list_queue = malloc(do_list_queue_size);
384         if (do_list_queue == 0) { 
385                 DEBUG(0,("malloc fail for size %d\n",
386                          (int)do_list_queue_size));
387                 reset_do_list_queue();
388         } else {
389                 memset(do_list_queue, 0, do_list_queue_size);
390         }
391 }
392
393 static void adjust_do_list_queue(void)
394 {
395         /*
396          * If the starting point of the queue is more than half way through,
397          * move everything toward the beginning.
398          */
399         if (do_list_queue && (do_list_queue_start == do_list_queue_end))
400         {
401                 DEBUG(4,("do_list_queue is empty\n"));
402                 do_list_queue_start = do_list_queue_end = 0;
403                 *do_list_queue = '\0';
404         }
405         else if (do_list_queue_start > (do_list_queue_size / 2))
406         {
407                 DEBUG(4,("sliding do_list_queue backward\n"));
408                 memmove(do_list_queue,
409                         do_list_queue + do_list_queue_start,
410                         do_list_queue_end - do_list_queue_start);
411                 do_list_queue_end -= do_list_queue_start;
412                 do_list_queue_start = 0;
413         }
414            
415 }
416
417 static void add_to_do_list_queue(const char* entry)
418 {
419         long new_end = do_list_queue_end + ((long)strlen(entry)) + 1;
420         while (new_end > do_list_queue_size)
421         {
422                 do_list_queue_size *= 2;
423                 DEBUG(4,("enlarging do_list_queue to %d\n",
424                          (int)do_list_queue_size));
425                 do_list_queue = Realloc(do_list_queue, do_list_queue_size);
426                 if (! do_list_queue) {
427                         DEBUG(0,("failure enlarging do_list_queue to %d bytes\n",
428                                  (int)do_list_queue_size));
429                         reset_do_list_queue();
430                 }
431                 else
432                 {
433                         memset(do_list_queue + do_list_queue_size / 2,
434                                0, do_list_queue_size / 2);
435                 }
436         }
437         if (do_list_queue)
438         {
439                 pstrcpy(do_list_queue + do_list_queue_end, entry);
440                 do_list_queue_end = new_end;
441                 DEBUG(4,("added %s to do_list_queue (start=%d, end=%d)\n",
442                          entry, (int)do_list_queue_start, (int)do_list_queue_end));
443         }
444 }
445
446 static char *do_list_queue_head(void)
447 {
448         return do_list_queue + do_list_queue_start;
449 }
450
451 static void remove_do_list_queue_head(void)
452 {
453         if (do_list_queue_end > do_list_queue_start)
454         {
455                 do_list_queue_start += strlen(do_list_queue_head()) + 1;
456                 adjust_do_list_queue();
457                 DEBUG(4,("removed head of do_list_queue (start=%d, end=%d)\n",
458                          (int)do_list_queue_start, (int)do_list_queue_end));
459         }
460 }
461
462 static int do_list_queue_empty(void)
463 {
464         return (! (do_list_queue && *do_list_queue));
465 }
466
467 /****************************************************************************
468 a helper for do_list
469   ****************************************************************************/
470 static void do_list_helper(file_info *f, const char *mask)
471 {
472         if (f->mode & aDIR) {
473                 if (do_list_dirs && do_this_one(f)) {
474                         do_list_fn(f);
475                 }
476                 if (do_list_recurse && 
477                     !strequal(f->name,".") && 
478                     !strequal(f->name,"..")) {
479                         pstring mask2;
480                         char *p;
481
482                         pstrcpy(mask2, mask);
483                         p = strrchr(mask2,'\\');
484                         if (!p) return;
485                         p[1] = 0;
486                         pstrcat(mask2, f->name);
487                         pstrcat(mask2,"\\*");
488                         add_to_do_list_queue(mask2);
489                 }
490                 return;
491         }
492
493         if (do_this_one(f)) {
494                 do_list_fn(f);
495         }
496 }
497
498
499 /****************************************************************************
500 a wrapper around cli_list that adds recursion
501   ****************************************************************************/
502 void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs)
503 {
504         static int in_do_list = 0;
505
506         if (in_do_list && rec)
507         {
508                 fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n");
509                 exit(1);
510         }
511
512         in_do_list = 1;
513
514         do_list_recurse = rec;
515         do_list_dirs = dirs;
516         do_list_fn = fn;
517
518         if (rec)
519         {
520                 init_do_list_queue();
521                 add_to_do_list_queue(mask);
522                 
523                 while (! do_list_queue_empty())
524                 {
525                         /*
526                          * Need to copy head so that it doesn't become
527                          * invalid inside the call to cli_list.  This
528                          * would happen if the list were expanded
529                          * during the call.
530                          * Fix from E. Jay Berkenbilt (ejb@ql.org)
531                          */
532                         pstring head;
533                         pstrcpy(head, do_list_queue_head());
534                         cli_list(cli, head, attribute, do_list_helper);
535                         remove_do_list_queue_head();
536                         if ((! do_list_queue_empty()) && (fn == display_finfo))
537                         {
538                                 char* next_file = do_list_queue_head();
539                                 char* save_ch = 0;
540                                 if ((strlen(next_file) >= 2) &&
541                                     (next_file[strlen(next_file) - 1] == '*') &&
542                                     (next_file[strlen(next_file) - 2] == '\\'))
543                                 {
544                                         save_ch = next_file +
545                                                 strlen(next_file) - 2;
546                                         *save_ch = '\0';
547                                 }
548                                 DEBUG(0,("\n%s\n",next_file));
549                                 if (save_ch)
550                                 {
551                                         *save_ch = '\\';
552                                 }
553                         }
554                 }
555         }
556         else
557         {
558                 if (cli_list(cli, mask, attribute, do_list_helper) == -1)
559                 {
560                         DEBUG(0, ("%s listing %s\n", cli_errstr(cli), mask));
561                 }
562         }
563
564         in_do_list = 0;
565         reset_do_list_queue();
566 }
567
568 /****************************************************************************
569   get a directory listing
570   ****************************************************************************/
571 static void cmd_dir(void)
572 {
573         uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
574         pstring mask;
575         fstring buf;
576         char *p=buf;
577         
578         dir_total = 0;
579         pstrcpy(mask,cur_dir);
580         if(mask[strlen(mask)-1]!='\\')
581                 pstrcat(mask,"\\");
582         
583         if (next_token(NULL,buf,NULL,sizeof(buf))) {
584                 dos_format(p);
585                 if (*p == '\\')
586                         pstrcpy(mask,p);
587                 else
588                         pstrcat(mask,p);
589         }
590         else {
591                 pstrcat(mask,"*");
592         }
593
594         do_list(mask, attribute, display_finfo, recurse, True);
595
596         do_dskattr();
597
598         DEBUG(3, ("Total bytes listed: %.0f\n", dir_total));
599 }
600
601
602 /****************************************************************************
603   get a directory listing
604   ****************************************************************************/
605 static void cmd_du(void)
606 {
607         uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
608         pstring mask;
609         fstring buf;
610         char *p=buf;
611         
612         dir_total = 0;
613         pstrcpy(mask,cur_dir);
614         if(mask[strlen(mask)-1]!='\\')
615                 pstrcat(mask,"\\");
616         
617         if (next_token(NULL,buf,NULL,sizeof(buf))) {
618                 dos_format(p);
619                 if (*p == '\\')
620                         pstrcpy(mask,p);
621                 else
622                         pstrcat(mask,p);
623         } else {
624                 pstrcat(mask,"*");
625         }
626
627         do_list(mask, attribute, do_du, recurse, True);
628
629         do_dskattr();
630
631         DEBUG(0, ("Total number of bytes: %.0f\n", dir_total));
632 }
633
634
635 /****************************************************************************
636   get a file from rname to lname
637   ****************************************************************************/
638 static void do_get(char *rname,char *lname)
639 {  
640         int handle=0,fnum;
641         BOOL newhandle = False;
642         char *data;
643         struct timeval tp_start;
644         int read_size = io_bufsize;
645         uint16 attr;
646         size_t size;
647         off_t nread = 0;
648
649         GetTimeOfDay(&tp_start);
650
651         if (lowercase) {
652                 strlower(lname);
653         }
654
655         fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
656
657         if (fnum == -1) {
658                 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
659                 return;
660         }
661
662         if(!strcmp(lname,"-")) {
663                 handle = fileno(stdout);
664         } else {
665                 handle = sys_open(lname,O_WRONLY|O_CREAT|O_TRUNC,0644);
666                 newhandle = True;
667         }
668         if (handle < 0) {
669                 DEBUG(0,("Error opening local file %s\n",lname));
670                 return;
671         }
672
673
674         if (!cli_qfileinfo(cli, fnum, 
675                            &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
676             !cli_getattrE(cli, fnum, 
677                           &attr, &size, NULL, NULL, NULL)) {
678                 DEBUG(0,("getattrib: %s\n",cli_errstr(cli)));
679                 return;
680         }
681
682         DEBUG(2,("getting file %s of size %.0f as %s ", 
683                  lname, (double)size, lname));
684
685         if(!(data = (char *)malloc(read_size))) { 
686                 DEBUG(0,("malloc fail for size %d\n", read_size));
687                 cli_close(cli, fnum);
688                 return;
689         }
690
691         while (1) {
692                 int n = cli_read(cli, fnum, data, nread, read_size);
693
694                 if (n <= 0) break;
695  
696                 if (writefile(handle,data, n) != n) {
697                         DEBUG(0,("Error writing local file\n"));
698                         break;
699                 }
700       
701                 nread += n;
702         }
703
704         if (nread < size) {
705                 DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n",
706                rname, (long)nread));
707         }
708
709         free(data);
710         
711         if (!cli_close(cli, fnum)) {
712                 DEBUG(0,("Error %s closing remote file\n",cli_errstr(cli)));
713         }
714
715         if (newhandle) {
716                 close(handle);
717         }
718
719         if (archive_level >= 2 && (attr & aARCH)) {
720                 cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0);
721         }
722
723         {
724                 struct timeval tp_end;
725                 int this_time;
726                 
727                 GetTimeOfDay(&tp_end);
728                 this_time = 
729                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
730                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
731                 get_total_time_ms += this_time;
732                 get_total_size += nread;
733                 
734                 DEBUG(2,("(%3.1f kb/s) (average %3.1f kb/s)\n",
735                          nread / (1.024*this_time + 1.0e-4),
736                          get_total_size / (1.024*get_total_time_ms)));
737         }
738 }
739
740
741 /****************************************************************************
742   get a file
743   ****************************************************************************/
744 static void cmd_get(void)
745 {
746         pstring lname;
747         pstring rname;
748         char *p;
749
750         pstrcpy(rname,cur_dir);
751         pstrcat(rname,"\\");
752         
753         p = rname + strlen(rname);
754         
755         if (!next_token(NULL,p,NULL,sizeof(rname)-strlen(rname))) {
756                 DEBUG(0,("get <filename>\n"));
757                 return;
758         }
759         pstrcpy(lname,p);
760         dos_clean_name(rname);
761         
762         next_token(NULL,lname,NULL,sizeof(lname));
763         
764         do_get(rname, lname);
765 }
766
767
768 /****************************************************************************
769   do a mget operation on one file
770   ****************************************************************************/
771 static void do_mget(file_info *finfo)
772 {
773         pstring rname;
774         pstring quest;
775         pstring saved_curdir;
776         pstring mget_mask;
777
778         if (strequal(finfo->name,".") || strequal(finfo->name,".."))
779                 return;
780
781         if (abort_mget) {
782                 DEBUG(0,("mget aborted\n"));
783                 return;
784         }
785
786         if (finfo->mode & aDIR)
787                 slprintf(quest,sizeof(pstring)-1,
788                          "Get directory %s? ",finfo->name);
789         else
790                 slprintf(quest,sizeof(pstring)-1,
791                          "Get file %s? ",finfo->name);
792
793         if (prompt && !yesno(quest)) return;
794
795         if (!(finfo->mode & aDIR)) {
796                 pstrcpy(rname,cur_dir);
797                 pstrcat(rname,finfo->name);
798                 do_get(rname,finfo->name);
799                 return;
800         }
801
802         /* handle directories */
803         pstrcpy(saved_curdir,cur_dir);
804
805         pstrcat(cur_dir,finfo->name);
806         pstrcat(cur_dir,"\\");
807
808         unix_format(finfo->name);
809         if (lowercase)
810                 strlower(finfo->name);
811         
812         if (!dos_directory_exist(finfo->name,NULL) && 
813             dos_mkdir(finfo->name,0777) != 0) {
814                 DEBUG(0,("failed to create directory %s\n",finfo->name));
815                 pstrcpy(cur_dir,saved_curdir);
816                 return;
817         }
818         
819         if (dos_chdir(finfo->name) != 0) {
820                 DEBUG(0,("failed to chdir to directory %s\n",finfo->name));
821                 pstrcpy(cur_dir,saved_curdir);
822                 return;
823         }
824
825         pstrcpy(mget_mask,cur_dir);
826         pstrcat(mget_mask,"*");
827         
828         do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True);
829         chdir("..");
830         pstrcpy(cur_dir,saved_curdir);
831 }
832
833
834 /****************************************************************************
835 view the file using the pager
836 ****************************************************************************/
837 static void cmd_more(void)
838 {
839         fstring rname,lname,tmpname,pager_cmd;
840         char *pager;
841
842         fstrcpy(rname,cur_dir);
843         fstrcat(rname,"\\");
844         slprintf(tmpname,
845                  sizeof(fstring)-1,
846                  "%s/smbmore.%d",tmpdir(),(int)getpid());
847         fstrcpy(lname,tmpname);
848         
849         if (!next_token(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) {
850                 DEBUG(0,("more <filename>\n"));
851                 return;
852         }
853         dos_clean_name(rname);
854
855         do_get(rname,lname);
856
857         pager=getenv("PAGER");
858
859         slprintf(pager_cmd,sizeof(pager_cmd)-1,
860                  "%s %s",(pager? pager:PAGER), tmpname);
861         system(pager_cmd);
862         unlink(tmpname);
863 }
864
865
866
867 /****************************************************************************
868 do a mget command
869 ****************************************************************************/
870 static void cmd_mget(void)
871 {
872         uint16 attribute = aSYSTEM | aHIDDEN;
873         pstring mget_mask;
874         fstring buf;
875         char *p=buf;
876
877         *mget_mask = 0;
878
879         if (recurse)
880                 attribute |= aDIR;
881         
882         abort_mget = False;
883
884         while (next_token(NULL,p,NULL,sizeof(buf))) {
885                 pstrcpy(mget_mask,cur_dir);
886                 if(mget_mask[strlen(mget_mask)-1]!='\\')
887                         pstrcat(mget_mask,"\\");
888                 
889                 if (*p == '\\')
890                         pstrcpy(mget_mask,p);
891                 else
892                         pstrcat(mget_mask,p);
893                 do_list(mget_mask, attribute,do_mget,False,True);
894         }
895
896         if (!*mget_mask) {
897                 pstrcpy(mget_mask,cur_dir);
898                 if(mget_mask[strlen(mget_mask)-1]!='\\')
899                         pstrcat(mget_mask,"\\");
900                 pstrcat(mget_mask,"*");
901                 do_list(mget_mask, attribute,do_mget,False,True);
902         }
903 }
904
905
906 /****************************************************************************
907 make a directory of name "name"
908 ****************************************************************************/
909 static BOOL do_mkdir(char *name)
910 {
911         if (!cli_mkdir(cli, name)) {
912                 DEBUG(0,("%s making remote directory %s\n",
913                          cli_errstr(cli),name));
914                 return(False);
915         }
916
917         return(True);
918 }
919
920
921 /****************************************************************************
922  Exit client.
923 ****************************************************************************/
924 static void cmd_quit(void)
925 {
926         cli_shutdown(cli);
927         exit(0);
928 }
929
930
931 /****************************************************************************
932   make a directory
933   ****************************************************************************/
934 static void cmd_mkdir(void)
935 {
936         pstring mask;
937         fstring buf;
938         char *p=buf;
939   
940         pstrcpy(mask,cur_dir);
941
942         if (!next_token(NULL,p,NULL,sizeof(buf))) {
943                 if (!recurse)
944                         DEBUG(0,("mkdir <dirname>\n"));
945                 return;
946         }
947         pstrcat(mask,p);
948
949         if (recurse) {
950                 pstring ddir;
951                 pstring ddir2;
952                 *ddir2 = 0;
953                 
954                 pstrcpy(ddir,mask);
955                 trim_string(ddir,".",NULL);
956                 p = strtok(ddir,"/\\");
957                 while (p) {
958                         pstrcat(ddir2,p);
959                         if (!cli_chkpath(cli, ddir2)) { 
960                                 do_mkdir(ddir2);
961                         }
962                         pstrcat(ddir2,"\\");
963                         p = strtok(NULL,"/\\");
964                 }        
965         } else {
966                 do_mkdir(mask);
967         }
968 }
969
970
971 /****************************************************************************
972   put a single file
973   ****************************************************************************/
974 static void do_put(char *rname,char *lname)
975 {
976         int fnum;
977         FILE *f;
978         int nread=0;
979         char *buf=NULL;
980         int maxwrite=io_bufsize;
981         
982         struct timeval tp_start;
983         GetTimeOfDay(&tp_start);
984
985         fnum = cli_open(cli, rname, O_WRONLY|O_CREAT|O_TRUNC, DENY_NONE);
986   
987         if (fnum == -1) {
988                 DEBUG(0,("%s opening remote file %s\n",cli_errstr(cli),rname));
989                 return;
990         }
991
992         /* allow files to be piped into smbclient
993            jdblair 24.jun.98 */
994         if (!strcmp(lname, "-")) {
995                 f = stdin;
996                 /* size of file is not known */
997         } else {
998                 f = sys_fopen(lname,"r");
999         }
1000
1001         if (!f) {
1002                 DEBUG(0,("Error opening local file %s\n",lname));
1003                 return;
1004         }
1005
1006   
1007         DEBUG(1,("putting file %s as %s ",lname,
1008                  rname));
1009   
1010         buf = (char *)malloc(maxwrite);
1011         while (!feof(f)) {
1012                 int n = maxwrite;
1013                 int ret;
1014
1015                 if ((n = readfile(buf,1,n,f)) < 1) {
1016                         DEBUG(0,("Error reading local file\n"));
1017                         break;
1018                 }
1019
1020                 ret = cli_write(cli, fnum, 0, buf, nread, n);
1021
1022                 if (n != ret) {
1023                         DEBUG(0,("Error writing file: %s\n", cli_errstr(cli)));
1024                         break;
1025                 } 
1026
1027                 nread += n;
1028         }
1029
1030         if (!cli_close(cli, fnum)) {
1031                 DEBUG(0,("%s closing remote file %s\n",cli_errstr(cli),rname));
1032                 fclose(f);
1033                 if (buf) free(buf);
1034                 return;
1035         }
1036
1037         
1038         fclose(f);
1039         if (buf) free(buf);
1040
1041         {
1042                 struct timeval tp_end;
1043                 int this_time;
1044                 
1045                 GetTimeOfDay(&tp_end);
1046                 this_time = 
1047                         (tp_end.tv_sec - tp_start.tv_sec)*1000 +
1048                         (tp_end.tv_usec - tp_start.tv_usec)/1000;
1049                 put_total_time_ms += this_time;
1050                 put_total_size += nread;
1051                 
1052                 DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n",
1053                          nread / (1.024*this_time + 1.0e-4),
1054                          put_total_size / (1.024*put_total_time_ms)));
1055         }
1056
1057         if (f == stdin) {
1058                 cli_shutdown(cli);
1059                 exit(0);
1060         }
1061 }
1062
1063  
1064
1065 /****************************************************************************
1066   put a file
1067   ****************************************************************************/
1068 static void cmd_put(void)
1069 {
1070         pstring lname;
1071         pstring rname;
1072         fstring buf;
1073         char *p=buf;
1074         
1075         pstrcpy(rname,cur_dir);
1076         pstrcat(rname,"\\");
1077   
1078         if (!next_token(NULL,p,NULL,sizeof(buf))) {
1079                 DEBUG(0,("put <filename>\n"));
1080                 return;
1081         }
1082         pstrcpy(lname,p);
1083   
1084         if (next_token(NULL,p,NULL,sizeof(buf)))
1085                 pstrcat(rname,p);      
1086         else
1087                 pstrcat(rname,lname);
1088         
1089         dos_clean_name(rname);
1090
1091         {
1092                 SMB_STRUCT_STAT st;
1093                 /* allow '-' to represent stdin
1094                    jdblair, 24.jun.98 */
1095                 if (!file_exist(lname,&st) &&
1096                     (strcmp(lname,"-"))) {
1097                         DEBUG(0,("%s does not exist\n",lname));
1098                         return;
1099                 }
1100         }
1101
1102         do_put(rname,lname);
1103 }
1104
1105
1106 /****************************************************************************
1107   seek in a directory/file list until you get something that doesn't start with
1108   the specified name
1109   ****************************************************************************/
1110 static BOOL seek_list(FILE *f,char *name)
1111 {
1112         pstring s;
1113         while (!feof(f)) {
1114                 if (fscanf(f,"%s",s) != 1) return(False);
1115                 trim_string(s,"./",NULL);
1116                 if (strncmp(s,name,strlen(name)) != 0) {
1117                         pstrcpy(name,s);
1118                         return(True);
1119                 }
1120         }
1121       
1122         return(False);
1123 }
1124
1125
1126 /****************************************************************************
1127   set the file selection mask
1128   ****************************************************************************/
1129 static void cmd_select(void)
1130 {
1131         pstrcpy(fileselection,"");
1132         next_token(NULL,fileselection,NULL,sizeof(fileselection));
1133 }
1134
1135
1136 /****************************************************************************
1137   mput some files
1138   ****************************************************************************/
1139 static void cmd_mput(void)
1140 {
1141         pstring lname;
1142         pstring rname;
1143         fstring buf;
1144         char *p=buf;
1145         
1146         while (next_token(NULL,p,NULL,sizeof(buf))) {
1147                 SMB_STRUCT_STAT st;
1148                 pstring cmd;
1149                 pstring tmpname;
1150                 FILE *f;
1151                 
1152                 slprintf(tmpname,sizeof(pstring)-1,
1153                          "%s/ls.smb.%d",tmpdir(),(int)getpid());
1154                 if (recurse)
1155                         slprintf(cmd,sizeof(pstring)-1,
1156                                  "find . -name \"%s\" -print > %s",p,tmpname);
1157                 else
1158                         slprintf(cmd,sizeof(pstring)-1,
1159                                  "/bin/ls %s > %s",p,tmpname);
1160                 system(cmd);
1161
1162                 f = sys_fopen(tmpname,"r");
1163                 if (!f) continue;
1164                 
1165                 while (!feof(f)) {
1166                         pstring quest;
1167
1168                         if (fscanf(f,"%s",lname) != 1) break;
1169                         trim_string(lname,"./",NULL);
1170                         
1171                 again1:
1172                         
1173                         /* check if it's a directory */
1174                         if (directory_exist(lname,&st)) {
1175                                 if (!recurse) continue;
1176                                 slprintf(quest,sizeof(pstring)-1,
1177                                          "Put directory %s? ",lname);
1178                                 if (prompt && !yesno(quest)) {
1179                                         pstrcat(lname,"/");
1180                                         if (!seek_list(f,lname))
1181                                                 break;
1182                                         goto again1;                
1183                                 }
1184               
1185                                 pstrcpy(rname,cur_dir);
1186                                 pstrcat(rname,lname);
1187                                 if (!cli_chkpath(cli, rname) && !do_mkdir(rname)) {
1188                                         pstrcat(lname,"/");
1189                                         if (!seek_list(f,lname))
1190                                                 break;
1191                                         goto again1;
1192                                 }
1193                                 continue;
1194                         } else {
1195                                 slprintf(quest,sizeof(quest)-1,
1196                                          "Put file %s? ",lname);
1197                                 if (prompt && !yesno(quest)) continue;
1198                                 
1199                                 pstrcpy(rname,cur_dir);
1200                                 pstrcat(rname,lname);
1201                         }
1202
1203                         dos_format(rname);
1204
1205                         do_put(rname,lname);
1206                 }
1207                 fclose(f);
1208                 unlink(tmpname);
1209         }
1210 }
1211
1212
1213 /****************************************************************************
1214   cancel a print job
1215   ****************************************************************************/
1216 static void do_cancel(int job)
1217 {
1218         if (cli_printjob_del(cli, job)) {
1219                 printf("Job %d cancelled\n",job);
1220         } else {
1221                 printf("Error calcelling job %d : %s\n",job,cli_errstr(cli));
1222         }
1223 }
1224
1225
1226 /****************************************************************************
1227   cancel a print job
1228   ****************************************************************************/
1229 static void cmd_cancel(void)
1230 {
1231         fstring buf;
1232         int job; 
1233
1234         if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1235                 printf("cancel <jobid> ...\n");
1236                 return;
1237         }
1238         do {
1239                 job = atoi(buf);
1240                 do_cancel(job);
1241         } while (next_token(NULL,buf,NULL,sizeof(buf)));
1242 }
1243
1244
1245 /****************************************************************************
1246   print a file
1247   ****************************************************************************/
1248 static void cmd_print(void)
1249 {
1250         pstring lname;
1251         pstring rname;
1252         char *p;
1253
1254         if (!next_token(NULL,lname,NULL, sizeof(lname))) {
1255                 DEBUG(0,("print <filename>\n"));
1256                 return;
1257         }
1258
1259         pstrcpy(rname,lname);
1260         p = strrchr(rname,'/');
1261         if (p) {
1262                 slprintf(rname, sizeof(rname)-1, "%s-%d", p+1, (int)getpid());
1263         }
1264
1265         if (strequal(lname,"-")) {
1266                 slprintf(rname, sizeof(rname)-1, "stdin-%d", (int)getpid());
1267         }
1268
1269         do_put(rname, lname);
1270 }
1271
1272
1273 /****************************************************************************
1274  show a print queue entry
1275 ****************************************************************************/
1276 static void queue_fn(struct print_job_info *p)
1277 {
1278         DEBUG(0,("%-6d   %-9d    %s\n", (int)p->id, (int)p->size, p->name));
1279 }
1280
1281 /****************************************************************************
1282  show a print queue
1283 ****************************************************************************/
1284 static void cmd_queue(void)
1285 {
1286         cli_print_queue(cli, queue_fn);  
1287 }
1288
1289 /****************************************************************************
1290 delete some files
1291 ****************************************************************************/
1292 static void do_del(file_info *finfo)
1293 {
1294         pstring mask;
1295
1296         pstrcpy(mask,cur_dir);
1297         pstrcat(mask,finfo->name);
1298
1299         if (finfo->mode & aDIR) 
1300                 return;
1301
1302         if (!cli_unlink(cli, mask)) {
1303                 DEBUG(0,("%s deleting remote file %s\n",cli_errstr(cli),mask));
1304         }
1305 }
1306
1307 /****************************************************************************
1308 delete some files
1309 ****************************************************************************/
1310 static void cmd_del(void)
1311 {
1312         pstring mask;
1313         fstring buf;
1314         uint16 attribute = aSYSTEM | aHIDDEN;
1315
1316         if (recurse)
1317                 attribute |= aDIR;
1318         
1319         pstrcpy(mask,cur_dir);
1320         
1321         if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1322                 DEBUG(0,("del <filename>\n"));
1323                 return;
1324         }
1325         pstrcat(mask,buf);
1326
1327         do_list(mask, attribute,do_del,False,False);
1328 }
1329
1330 /****************************************************************************
1331 ****************************************************************************/
1332 static void cmd_open(void)
1333 {
1334         pstring mask;
1335         fstring buf;
1336         
1337         pstrcpy(mask,cur_dir);
1338         
1339         if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1340                 DEBUG(0,("del <filename>\n"));
1341                 return;
1342         }
1343         pstrcat(mask,buf);
1344
1345         cli_open(cli, mask, O_RDWR, DENY_ALL);
1346 }
1347
1348
1349 /****************************************************************************
1350 remove a directory
1351 ****************************************************************************/
1352 static void cmd_rmdir(void)
1353 {
1354         pstring mask;
1355         fstring buf;
1356   
1357         pstrcpy(mask,cur_dir);
1358         
1359         if (!next_token(NULL,buf,NULL,sizeof(buf))) {
1360                 DEBUG(0,("rmdir <dirname>\n"));
1361                 return;
1362         }
1363         pstrcat(mask,buf);
1364
1365         if (!cli_rmdir(cli, mask)) {
1366                 DEBUG(0,("%s removing remote directory file %s\n",
1367                          cli_errstr(cli),mask));
1368         }  
1369 }
1370
1371 /****************************************************************************
1372 rename some files
1373 ****************************************************************************/
1374 static void cmd_rename(void)
1375 {
1376         pstring src,dest;
1377         fstring buf,buf2;
1378   
1379         pstrcpy(src,cur_dir);
1380         pstrcpy(dest,cur_dir);
1381         
1382         if (!next_token(NULL,buf,NULL,sizeof(buf)) || 
1383             !next_token(NULL,buf2,NULL, sizeof(buf2))) {
1384                 DEBUG(0,("rename <src> <dest>\n"));
1385                 return;
1386         }
1387
1388         pstrcat(src,buf);
1389         pstrcat(dest,buf2);
1390
1391         if (!cli_rename(cli, src, dest)) {
1392                 DEBUG(0,("%s renaming files\n",cli_errstr(cli)));
1393                 return;
1394         }  
1395 }
1396
1397
1398 /****************************************************************************
1399 toggle the prompt flag
1400 ****************************************************************************/
1401 static void cmd_prompt(void)
1402 {
1403         prompt = !prompt;
1404         DEBUG(2,("prompting is now %s\n",prompt?"on":"off"));
1405 }
1406
1407
1408 /****************************************************************************
1409 set the newer than time
1410 ****************************************************************************/
1411 static void cmd_newer(void)
1412 {
1413         fstring buf;
1414         BOOL ok;
1415         SMB_STRUCT_STAT sbuf;
1416
1417         ok = next_token(NULL,buf,NULL,sizeof(buf));
1418         if (ok && (dos_stat(buf,&sbuf) == 0)) {
1419                 newer_than = sbuf.st_mtime;
1420                 DEBUG(1,("Getting files newer than %s",
1421                          asctime(LocalTime(&newer_than))));
1422         } else {
1423                 newer_than = 0;
1424         }
1425
1426         if (ok && newer_than == 0)
1427                 DEBUG(0,("Error setting newer-than time\n"));
1428 }
1429
1430 /****************************************************************************
1431 set the archive level
1432 ****************************************************************************/
1433 static void cmd_archive(void)
1434 {
1435         fstring buf;
1436
1437         if (next_token(NULL,buf,NULL,sizeof(buf))) {
1438                 archive_level = atoi(buf);
1439         } else
1440                 DEBUG(0,("Archive level is %d\n",archive_level));
1441 }
1442
1443 /****************************************************************************
1444 toggle the lowercaseflag
1445 ****************************************************************************/
1446 static void cmd_lowercase(void)
1447 {
1448         lowercase = !lowercase;
1449         DEBUG(2,("filename lowercasing is now %s\n",lowercase?"on":"off"));
1450 }
1451
1452
1453
1454
1455 /****************************************************************************
1456 toggle the recurse flag
1457 ****************************************************************************/
1458 static void cmd_recurse(void)
1459 {
1460         recurse = !recurse;
1461         DEBUG(2,("directory recursion is now %s\n",recurse?"on":"off"));
1462 }
1463
1464 /****************************************************************************
1465 toggle the translate flag
1466 ****************************************************************************/
1467 static void cmd_translate(void)
1468 {
1469         translation = !translation;
1470         DEBUG(2,("CR/LF<->LF and print text translation now %s\n",
1471                  translation?"on":"off"));
1472 }
1473
1474
1475 /****************************************************************************
1476 do a printmode command
1477 ****************************************************************************/
1478 static void cmd_printmode(void)
1479 {
1480         fstring buf;
1481         fstring mode;
1482
1483         if (next_token(NULL,buf,NULL,sizeof(buf))) {
1484                 if (strequal(buf,"text")) {
1485                         printmode = 0;      
1486                 } else {
1487                         if (strequal(buf,"graphics"))
1488                                 printmode = 1;
1489                         else
1490                                 printmode = atoi(buf);
1491                 }
1492         }
1493
1494         switch(printmode)
1495                 {
1496                 case 0: 
1497                         fstrcpy(mode,"text");
1498                         break;
1499                 case 1: 
1500                         fstrcpy(mode,"graphics");
1501                         break;
1502                 default: 
1503                         slprintf(mode,sizeof(mode)-1,"%d",printmode);
1504                         break;
1505                 }
1506         
1507         DEBUG(2,("the printmode is now %s\n",mode));
1508 }
1509
1510 /****************************************************************************
1511 do the lcd command
1512 ****************************************************************************/
1513 static void cmd_lcd(void)
1514 {
1515         fstring buf;
1516         pstring d;
1517         
1518         if (next_token(NULL,buf,NULL,sizeof(buf)))
1519                 chdir(buf);
1520         DEBUG(2,("the local directory is now %s\n",sys_getwd(d)));
1521 }
1522
1523 /****************************************************************************
1524 list a share name
1525 ****************************************************************************/
1526 static void browse_fn(const char *name, uint32 m, const char *comment)
1527 {
1528         fstring typestr;
1529
1530         *typestr=0;
1531
1532         switch (m)
1533         {
1534           case STYPE_DISKTREE:
1535             fstrcpy(typestr,"Disk"); break;
1536           case STYPE_PRINTQ:
1537             fstrcpy(typestr,"Printer"); break;
1538           case STYPE_DEVICE:
1539             fstrcpy(typestr,"Device"); break;
1540           case STYPE_IPC:
1541             fstrcpy(typestr,"IPC"); break;
1542         }
1543
1544         printf("\t%-15.15s%-10.10s%s\n",
1545                name, typestr,comment);
1546 }
1547
1548
1549 /****************************************************************************
1550 try and browse available connections on a host
1551 ****************************************************************************/
1552 static BOOL browse_host(BOOL sort)
1553 {
1554         printf("\n\tSharename      Type      Comment\n");
1555         printf("\t---------      ----      -------\n");
1556
1557         return cli_RNetShareEnum(cli, browse_fn);
1558 }
1559
1560 /****************************************************************************
1561 list a server name
1562 ****************************************************************************/
1563 static void server_fn(const char *name, uint32 m, const char *comment)
1564 {
1565         printf("\t%-16.16s     %s\n", name, comment);
1566 }
1567
1568 /****************************************************************************
1569 try and browse available connections on a host
1570 ****************************************************************************/
1571 static BOOL list_servers(char *wk_grp)
1572 {
1573         if (!cli->server_domain) return False;
1574
1575         printf("\n\tServer               Comment\n");
1576         printf("\t---------            -------\n");
1577
1578         cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, server_fn);
1579
1580         printf("\n\tWorkgroup            Master\n");
1581         printf("\t---------            -------\n");
1582
1583         cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, server_fn);
1584         return True;
1585 }
1586
1587 #if defined(HAVE_LIBREADLINE)
1588 #  if defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H)
1589 /****************************************************************************
1590 history
1591 ****************************************************************************/
1592 static void cmd_history(void)
1593 {
1594         HIST_ENTRY **hlist;
1595         register int i;
1596
1597         hlist = history_list ();        /* Get pointer to history list */
1598         
1599         if (hlist)                      /* If list not empty */
1600         {
1601                 for (i = 0; hlist[i]; i++)      /* then display it */
1602                         DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
1603         }
1604 }
1605 #  endif 
1606 #endif
1607
1608 /* Some constants for completing filename arguments */
1609
1610 #define COMPL_NONE        0          /* No completions */
1611 #define COMPL_REMOTE      1          /* Complete remote filename */
1612 #define COMPL_LOCAL       2          /* Complete local filename */
1613
1614 /* This defines the commands supported by this client */
1615 struct
1616 {
1617   char *name;
1618   void (*fn)(void);
1619   char *description;
1620   char compl_args[2];      /* Completion argument info */
1621 } commands[] = 
1622 {
1623   {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1624   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1625   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
1626   {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
1627   {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
1628   {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
1629   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
1630   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
1631   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
1632   {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
1633   {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
1634   {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
1635   {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
1636   {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1637   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
1638   {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
1639   {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1640   {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
1641   {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1642   {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
1643   {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
1644   {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
1645   {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},  
1646   {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
1647   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
1648   {"printmode",cmd_printmode,"<graphics or text> set the print mode",{COMPL_NONE,COMPL_NONE}},
1649   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
1650   {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
1651   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1652   {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1653   {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
1654   {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
1655   {"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}},
1656   {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
1657   {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
1658   {"tarmode",cmd_tarmode,
1659      "<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
1660   {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
1661   {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1662   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
1663 #ifdef HAVE_LIBREADLINE
1664   {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
1665 #endif
1666   {"!",NULL,"run a shell command on the local system",{COMPL_NONE,COMPL_NONE}},
1667   {"",NULL,NULL,{COMPL_NONE,COMPL_NONE}}
1668 };
1669
1670
1671 /*******************************************************************
1672   lookup a command string in the list of commands, including 
1673   abbreviations
1674   ******************************************************************/
1675 static int process_tok(fstring tok)
1676 {
1677         int i = 0, matches = 0;
1678         int cmd=0;
1679         int tok_len = strlen(tok);
1680         
1681         while (commands[i].fn != NULL) {
1682                 if (strequal(commands[i].name,tok)) {
1683                         matches = 1;
1684                         cmd = i;
1685                         break;
1686                 } else if (strnequal(commands[i].name, tok, tok_len)) {
1687                         matches++;
1688                         cmd = i;
1689                 }
1690                 i++;
1691         }
1692   
1693         if (matches == 0)
1694                 return(-1);
1695         else if (matches == 1)
1696                 return(cmd);
1697         else
1698                 return(-2);
1699 }
1700
1701 /****************************************************************************
1702 help
1703 ****************************************************************************/
1704 static void cmd_help(void)
1705 {
1706         int i=0,j;
1707         fstring buf;
1708         
1709         if (next_token(NULL,buf,NULL,sizeof(buf))) {
1710                 if ((i = process_tok(buf)) >= 0)
1711                         DEBUG(0,("HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description));                   
1712         } else {
1713                 while (commands[i].description) {
1714                         for (j=0; commands[i].description && (j<5); j++) {
1715                                 DEBUG(0,("%-15s",commands[i].name));
1716                                 i++;
1717                         }
1718                         DEBUG(0,("\n"));
1719                 }
1720         }
1721 }
1722
1723 #ifndef HAVE_LIBREADLINE
1724 /****************************************************************************
1725 wait for keyboard activity, swallowing network packets
1726 ****************************************************************************/
1727 static void wait_keyboard(void)
1728 {
1729         fd_set fds;
1730         struct timeval timeout;
1731   
1732         while (1) {
1733                 FD_ZERO(&fds);
1734                 FD_SET(cli->fd,&fds);
1735                 FD_SET(fileno(stdin),&fds);
1736
1737                 timeout.tv_sec = 20;
1738                 timeout.tv_usec = 0;
1739                 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
1740       
1741                 if (FD_ISSET(fileno(stdin),&fds))
1742                         return;
1743
1744                 /* We deliberately use receive_smb instead of
1745                    client_receive_smb as we want to receive
1746                    session keepalives and then drop them here.
1747                 */
1748                 if (FD_ISSET(cli->fd,&fds))
1749                         receive_smb(cli->fd,cli->inbuf,0);
1750       
1751                 cli_chkpath(cli, "\\");
1752         }  
1753 }
1754 #endif
1755
1756 /****************************************************************************
1757 process a -c command string
1758 ****************************************************************************/
1759 static void process_command_string(char *cmd)
1760 {
1761         pstring line;
1762         char *ptr;
1763
1764         while (cmd[0] != '\0')    {
1765                 char *p;
1766                 fstring tok;
1767                 int i;
1768                 
1769                 if ((p = strchr(cmd, ';')) == 0) {
1770                         strncpy(line, cmd, 999);
1771                         line[1000] = '\0';
1772                         cmd += strlen(cmd);
1773                 } else {
1774                         if (p - cmd > 999) p = cmd + 999;
1775                         strncpy(line, cmd, p - cmd);
1776                         line[p - cmd] = '\0';
1777                         cmd = p + 1;
1778                 }
1779                 
1780                 /* and get the first part of the command */
1781                 ptr = line;
1782                 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1783                 
1784                 if ((i = process_tok(tok)) >= 0) {
1785                         commands[i].fn();
1786                 } else if (i == -2) {
1787                         DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1788                 } else {
1789                         DEBUG(0,("%s: command not found\n",tok));
1790                 }
1791         }
1792 }       
1793
1794 /****************************************************************************
1795 process commands on stdin
1796 ****************************************************************************/
1797 static void process_stdin(void)
1798 {
1799         pstring line;
1800         char *ptr;
1801
1802 #ifdef HAVE_LIBREADLINE
1803 /* Minimal readline support, 29Jun1999, s.xenitellis@rhbnc.ac.uk */
1804 #ifdef PROMPTSIZE
1805 #undef PROMPTSIZE
1806 #endif
1807 #define PROMPTSIZE 2048
1808         char prompt_str[PROMPTSIZE];    /* This holds the buffer "smb: \dir1\> " */
1809         
1810         char *temp;                     /* Gets the buffer from readline() */
1811         temp = (char *)NULL;
1812 #endif
1813         while (!feof(stdin)) {
1814                 fstring tok;
1815                 int i;
1816 #ifdef HAVE_LIBREADLINE
1817                 if ( temp != (char *)NULL )
1818                 {
1819                         free( temp );   /* Free memory allocated every time by readline() */
1820                         temp = (char *)NULL;
1821                 }
1822
1823                 snprintf( prompt_str, PROMPTSIZE - 1, "smb: %s> ", cur_dir );
1824
1825                 temp = readline( prompt_str );          /* We read the line here */
1826
1827                 if ( !temp )
1828                         break;          /* EOF occured */
1829
1830                 if ( *temp )            /* If non-empty line, save to history */
1831                         add_history (temp);
1832         
1833                 strncpy( line, temp, 1023 ); /* Maximum size of (pstring)line. Null is guarranteed. */
1834 #else 
1835                 /* display a prompt */
1836                 DEBUG(0,("smb: %s> ", cur_dir));
1837                 dbgflush( );
1838                 
1839                 wait_keyboard();
1840                 
1841                 /* and get a response */
1842                 if (!fgets(line,1000,stdin))
1843                         break;
1844 #endif
1845
1846                 /* special case - first char is ! */
1847                 if (*line == '!') {
1848                         system(line + 1);
1849                         continue;
1850                 }
1851       
1852                 /* and get the first part of the command */
1853                 ptr = line;
1854                 if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue;
1855
1856                 if ((i = process_tok(tok)) >= 0) {
1857                         commands[i].fn();
1858                 } else if (i == -2) {
1859                         DEBUG(0,("%s: command abbreviation ambiguous\n",tok));
1860                 } else {
1861                         DEBUG(0,("%s: command not found\n",tok));
1862                 }
1863         }
1864 }
1865
1866
1867 /***************************************************** 
1868 return a connection to a server
1869 *******************************************************/
1870 struct cli_state *do_connect(char *server, char *share)
1871 {
1872         struct cli_state *c;
1873         struct nmb_name called, calling;
1874         char *server_n;
1875         struct in_addr ip;
1876         extern struct in_addr ipzero;
1877
1878         if (*share == '\\') {
1879                 server = share+2;
1880                 share = strchr(server,'\\');
1881                 if (!share) return NULL;
1882                 *share = 0;
1883                 share++;
1884         }
1885
1886         server_n = server;
1887         
1888         ip = ipzero;
1889
1890         make_nmb_name(&calling, global_myname, 0x0, "");
1891         make_nmb_name(&called , server, name_type, "");
1892
1893  again:
1894         ip = ipzero;
1895         if (have_ip) ip = dest_ip;
1896
1897         /* have to open a new connection */
1898         if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
1899             !cli_connect(c, server_n, &ip)) {
1900                 DEBUG(0,("Connection to %s failed\n", server_n));
1901                 return NULL;
1902         }
1903
1904         if (!cli_session_request(c, &calling, &called)) {
1905                 char *p;
1906                 DEBUG(0,("session request to %s failed (%s)\n", 
1907                          called.name, cli_errstr(c)));
1908                 cli_shutdown(c);
1909                 if ((p=strchr(called.name, '.'))) {
1910                         *p = 0;
1911                         goto again;
1912                 }
1913                 if (strcmp(called.name, "*SMBSERVER")) {
1914                         make_nmb_name(&called , "*SMBSERVER", 0x20, "");
1915                         goto again;
1916                 }
1917                 return NULL;
1918         }
1919
1920         DEBUG(4,(" session request ok\n"));
1921
1922         if (!cli_negprot(c)) {
1923                 DEBUG(0,("protocol negotiation failed\n"));
1924                 cli_shutdown(c);
1925                 return NULL;
1926         }
1927
1928         if (!got_pass) {
1929                 char *pass = getpass("Password: ");
1930                 if (pass) {
1931                         pstrcpy(password, pass);
1932                 }
1933         }
1934
1935         if (!cli_session_setup(c, username, 
1936                                password, strlen(password),
1937                                password, strlen(password),
1938                                workgroup)) {
1939                 /* if a password was not supplied then try again with a null username */
1940                 if (password[0] || !username[0] || 
1941                     !cli_session_setup(c, "", "", 0, "", 0, workgroup)) { 
1942                         DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
1943                         return NULL;
1944                 }
1945                 DEBUG(0,("Anonymous login successful\n"));
1946         }
1947
1948         /*
1949          * These next two lines are needed to emulate
1950          * old client behaviour for people who have
1951          * scripts based on client output.
1952          * QUESTION ? Do we want to have a 'client compatibility
1953          * mode to turn these on/off ? JRA.
1954          */
1955
1956         if (*c->server_domain || *c->server_os || *c->server_type)
1957                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
1958                         c->server_domain,c->server_os,c->server_type));
1959         
1960         DEBUG(4,(" session setup ok\n"));
1961
1962         if (!cli_send_tconX(c, share, "?????",
1963                             password, strlen(password)+1)) {
1964                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
1965                 cli_shutdown(c);
1966                 return NULL;
1967         }
1968
1969         DEBUG(4,(" tconx ok\n"));
1970
1971         return c;
1972 }
1973
1974
1975 /****************************************************************************
1976   process commands from the client
1977 ****************************************************************************/
1978 static BOOL process(char *base_directory)
1979 {
1980         cli = do_connect(desthost, service);
1981         if (!cli) {
1982                 return(False);
1983         }
1984
1985         if (*base_directory) do_cd(base_directory);
1986         
1987         if (cmdstr) {
1988                 process_command_string(cmdstr);
1989         } else {
1990                 process_stdin();
1991         }
1992   
1993         cli_shutdown(cli);
1994         return(True);
1995 }
1996
1997 /****************************************************************************
1998 usage on the program
1999 ****************************************************************************/
2000 static void usage(char *pname)
2001 {
2002   DEBUG(0,("Usage: %s service <password> [options]", pname));
2003
2004   DEBUG(0,("\nVersion %s\n",VERSION));
2005   DEBUG(0,("\t-s smb.conf           pathname to smb.conf file\n"));
2006   DEBUG(0,("\t-O socket_options     socket options to use\n"));
2007   DEBUG(0,("\t-R name resolve order use these name resolution services only\n"));
2008   DEBUG(0,("\t-M host               send a winpopup message to the host\n"));
2009   DEBUG(0,("\t-i scope              use this NetBIOS scope\n"));
2010   DEBUG(0,("\t-N                    don't ask for a password\n"));
2011   DEBUG(0,("\t-n netbios name.      Use this name as my netbios name\n"));
2012   DEBUG(0,("\t-d debuglevel         set the debuglevel\n"));
2013   DEBUG(0,("\t-P                    connect to service as a printer\n"));
2014   DEBUG(0,("\t-p port               connect to the specified port\n"));
2015   DEBUG(0,("\t-l log basename.      Basename for log/debug files\n"));
2016   DEBUG(0,("\t-h                    Print this help message.\n"));
2017   DEBUG(0,("\t-I dest IP            use this IP to connect to\n"));
2018   DEBUG(0,("\t-E                    write messages to stderr instead of stdout\n"));
2019   DEBUG(0,("\t-U username           set the network username\n"));
2020   DEBUG(0,("\t-L host               get a list of shares available on a host\n"));
2021   DEBUG(0,("\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n"));
2022   DEBUG(0,("\t-m max protocol       set the max protocol level\n"));
2023   DEBUG(0,("\t-W workgroup          set the workgroup name\n"));
2024   DEBUG(0,("\t-T<c|x>IXFqgbNan      command line tar\n"));
2025   DEBUG(0,("\t-D directory          start from directory\n"));
2026   DEBUG(0,("\t-c command string     execute semicolon separated commands\n"));
2027   DEBUG(0,("\t-b xmit/send buffer   changes the transmit/send buffer (default: 65520)\n"));
2028   DEBUG(0,("\n"));
2029 }
2030
2031
2032 /****************************************************************************
2033 get a password from a a file or file descriptor
2034 exit on failure
2035 ****************************************************************************/
2036 static void get_password_file(void)
2037 {
2038         int fd = -1;
2039         char *p;
2040         BOOL close_it = False;
2041         pstring spec;
2042         char pass[128];
2043                 
2044         if ((p = getenv("PASSWD_FD")) != NULL) {
2045                 pstrcpy(spec, "descriptor ");
2046                 pstrcat(spec, p);
2047                 sscanf(p, "%d", &fd);
2048                 close_it = False;
2049         } else if ((p = getenv("PASSWD_FILE")) != NULL) {
2050                 fd = sys_open(p, O_RDONLY, 0);
2051                 pstrcpy(spec, p);
2052                 if (fd < 0) {
2053                         fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
2054                                 spec, strerror(errno));
2055                         exit(1);
2056                 }
2057                 close_it = True;
2058         }
2059
2060         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
2061             p && p - pass < sizeof(pass);) {
2062                 switch (read(fd, p, 1)) {
2063                 case 1:
2064                         if (*p != '\n' && *p != '\0') {
2065                                 *++p = '\0'; /* advance p, and null-terminate pass */
2066                                 break;
2067                         }
2068                 case 0:
2069                         if (p - pass) {
2070                                 *p = '\0'; /* null-terminate it, just in case... */
2071                                 p = NULL; /* then force the loop condition to become false */
2072                                 break;
2073                         } else {
2074                                 fprintf(stderr, "Error reading password from file %s: %s\n",
2075                                         spec, "empty password\n");
2076                                 exit(1);
2077                         }
2078                         
2079                 default:
2080                         fprintf(stderr, "Error reading password from file %s: %s\n",
2081                                 spec, strerror(errno));
2082                         exit(1);
2083                 }
2084         }
2085         pstrcpy(password, pass);
2086         if (close_it)
2087                 close(fd);
2088 }       
2089
2090
2091
2092 /****************************************************************************
2093 handle a -L query
2094 ****************************************************************************/
2095 static int do_host_query(char *query_host)
2096 {
2097         cli = do_connect(query_host, "IPC$");
2098         if (!cli)
2099                 return 1;
2100
2101         browse_host(True);
2102         list_servers(workgroup);
2103
2104         cli_shutdown(cli);
2105         
2106         return(0);
2107 }
2108
2109
2110 /****************************************************************************
2111 handle a tar operation
2112 ****************************************************************************/
2113 static int do_tar_op(char *base_directory)
2114 {
2115         int ret;
2116         cli = do_connect(desthost, service);
2117         if (!cli)
2118                 return 1;
2119
2120         recurse=True;
2121
2122         if (*base_directory) do_cd(base_directory);
2123         
2124         ret=process_tar();
2125
2126         cli_shutdown(cli);
2127
2128         return(ret);
2129 }
2130
2131 /****************************************************************************
2132 handle a message operation
2133 ****************************************************************************/
2134 static int do_message_op(void)
2135 {
2136         struct in_addr ip;
2137         struct nmb_name called, calling;
2138
2139         ip = ipzero;
2140
2141         make_nmb_name(&calling, global_myname, 0x0, "");
2142         make_nmb_name(&called , desthost, name_type, "");
2143
2144         ip = ipzero;
2145         if (have_ip) ip = dest_ip;
2146
2147         if (!(cli=cli_initialise(NULL)) || !cli_connect(cli, desthost, &ip)) {
2148                 DEBUG(0,("Connection to %s failed\n", desthost));
2149                 return 1;
2150         }
2151
2152         if (!cli_session_request(cli, &calling, &called)) {
2153                 DEBUG(0,("session request failed\n"));
2154                 cli_shutdown(cli);
2155                 return 1;
2156         }
2157
2158         send_message();
2159         cli_shutdown(cli);
2160
2161         return 0;
2162 }
2163
2164
2165 /****************************************************************************
2166   main program
2167 ****************************************************************************/
2168  int main(int argc,char *argv[])
2169 {
2170         fstring base_directory;
2171         char *pname = argv[0];
2172         int opt;
2173         extern FILE *dbf;
2174         extern char *optarg;
2175         extern int optind;
2176         pstring query_host;
2177         BOOL message = False;
2178         extern char tar_type;
2179         static pstring servicesf = CONFIGFILE;
2180         pstring term_code;
2181         pstring new_name_resolve_order;
2182         char *p;
2183
2184 #ifdef KANJI
2185         pstrcpy(term_code, KANJI);
2186 #else /* KANJI */
2187         *term_code = 0;
2188 #endif /* KANJI */
2189
2190         *query_host = 0;
2191         *base_directory = 0;
2192
2193         *new_name_resolve_order = 0;
2194
2195         DEBUGLEVEL = 2;
2196
2197 #ifdef HAVE_LIBREADLINE
2198         /* Allow conditional parsing of the ~/.inputrc file. */
2199         rl_readline_name = "smbclient";
2200 #endif    
2201         setup_logging(pname,True);
2202
2203         /*
2204          * If the -E option is given, be careful not to clobber stdout
2205          * before processing the options.  28.Feb.99, richard@hacom.nl.
2206          * Also pre-parse the -s option to get the service file name.
2207          */
2208
2209         for (opt = 1; opt < argc; opt++) {
2210                 if (strcmp(argv[opt], "-E") == 0)
2211                         dbf = stderr;
2212                 else if(strncmp(argv[opt], "-s", 2) == 0) {
2213                         if(argv[opt][2] != '\0')
2214                                 pstrcpy(servicesf, &argv[opt][2]);
2215                         else if(argv[opt+1] != NULL) {
2216                                 /*
2217                                  * At least one more arg left.
2218                                  */
2219                                 pstrcpy(servicesf, argv[opt+1]);
2220                         } else {
2221                                 usage(pname);
2222                                 exit(1);
2223                         }
2224                 }
2225         }
2226
2227         TimeInit();
2228         charset_initialise();
2229
2230         in_client = True;   /* Make sure that we tell lp_load we are */
2231
2232         if (!lp_load(servicesf,True,False,False)) {
2233                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
2234         }
2235         
2236         codepage_initialise(lp_client_code_page());
2237
2238 #ifdef WITH_SSL
2239         sslutil_init(0);
2240 #endif
2241
2242         pstrcpy(workgroup,lp_workgroup());
2243
2244         load_interfaces();
2245         myumask = umask(0);
2246         umask(myumask);
2247
2248         if (getenv("USER")) {
2249                 pstrcpy(username,getenv("USER"));
2250
2251                 /* modification to support userid%passwd syntax in the USER var
2252                    25.Aug.97, jdblair@uab.edu */
2253
2254                 if ((p=strchr(username,'%'))) {
2255                         *p = 0;
2256                         pstrcpy(password,p+1);
2257                         got_pass = True;
2258                         memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
2259                 }
2260                 strupper(username);
2261         }
2262
2263         /* modification to support PASSWD environmental var
2264            25.Aug.97, jdblair@uab.edu */
2265         if (getenv("PASSWD")) {
2266                 pstrcpy(password,getenv("PASSWD"));
2267                 got_pass = True;
2268         }
2269
2270         if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
2271                 get_password_file();
2272                 got_pass = True;
2273         }
2274
2275         if (*username == 0 && getenv("LOGNAME")) {
2276                 pstrcpy(username,getenv("LOGNAME"));
2277                 strupper(username);
2278         }
2279
2280         if (*username == 0) {
2281                 pstrcpy(username,"GUEST");
2282         }
2283
2284         if (argc < 2) {
2285                 usage(pname);
2286                 exit(1);
2287         }
2288   
2289         if (*argv[1] != '-') {
2290                 pstrcpy(service,argv[1]);  
2291                 /* Convert any '/' characters in the service name to '\' characters */
2292                 string_replace( service, '/','\\');
2293                 argc--;
2294                 argv++;
2295                 
2296                 if (count_chars(service,'\\') < 3) {
2297                         usage(pname);
2298                         printf("\n%s: Not enough '\\' characters in service\n",service);
2299                         exit(1);
2300                 }
2301
2302                 if (argc > 1 && (*argv[1] != '-')) {
2303                         got_pass = True;
2304                         pstrcpy(password,argv[1]);  
2305                         memset(argv[1],'X',strlen(argv[1]));
2306                         argc--;
2307                         argv++;
2308                 }
2309         }
2310
2311         while ((opt = 
2312                 getopt(argc, argv,"s:O:R:M:i:Nn:d:Pp:l:hI:EU:L:t:m:W:T:D:c:b:")) != EOF) {
2313                 switch (opt) {
2314                 case 's':
2315                         pstrcpy(servicesf, optarg);
2316                         break;
2317                 case 'O':
2318                         pstrcpy(user_socket_options,optarg);
2319                         break;  
2320                 case 'R':
2321                         pstrcpy(new_name_resolve_order, optarg);
2322                         break;
2323                 case 'M':
2324                         name_type = 0x03; /* messages are sent to NetBIOS name type 0x3 */
2325                         pstrcpy(desthost,optarg);
2326                         message = True;
2327                         break;
2328                 case 'i':
2329                         pstrcpy(scope,optarg);
2330                         break;
2331                 case 'N':
2332                         got_pass = True;
2333                         break;
2334                 case 'n':
2335                         pstrcpy(global_myname,optarg);
2336                         break;
2337                 case 'd':
2338                         if (*optarg == 'A')
2339                                 DEBUGLEVEL = 10000;
2340                         else
2341                                 DEBUGLEVEL = atoi(optarg);
2342                         break;
2343                 case 'P':
2344                         /* not needed anymore */
2345                         break;
2346                 case 'p':
2347                         port = atoi(optarg);
2348                         break;
2349                 case 'l':
2350                         slprintf(debugf,sizeof(debugf)-1, "%s.client",optarg);
2351                         break;
2352                 case 'h':
2353                         usage(pname);
2354                         exit(0);
2355                         break;
2356                 case 'I':
2357                         {
2358                                 dest_ip = *interpret_addr2(optarg);
2359                                 if (zero_ip(dest_ip))
2360                                         exit(1);
2361                                 have_ip = True;
2362                         }
2363                         break;
2364                 case 'E':
2365                         dbf = stderr;
2366                         break;
2367                 case 'U':
2368                         {
2369                                 char *lp;
2370                                 pstrcpy(username,optarg);
2371                                 if ((lp=strchr(username,'%'))) {
2372                                         *lp = 0;
2373                                         pstrcpy(password,lp+1);
2374                                         got_pass = True;
2375                                         memset(strchr(optarg,'%')+1,'X',strlen(password));
2376                                 }
2377                         }
2378                         break;
2379                 case 'L':
2380                         p = optarg;
2381                         while(*p == '\\' || *p == '/')
2382                                 p++;
2383                         pstrcpy(query_host,p);
2384                         break;
2385                 case 't':
2386                         pstrcpy(term_code, optarg);
2387                         break;
2388                 case 'm':
2389                         /* no longer supported */
2390                         break;
2391                 case 'W':
2392                         pstrcpy(workgroup,optarg);
2393                         break;
2394                 case 'T':
2395                         if (!tar_parseargs(argc, argv, optarg, optind)) {
2396                                 usage(pname);
2397                                 exit(1);
2398                         }
2399                         break;
2400                 case 'D':
2401                         pstrcpy(base_directory,optarg);
2402                         break;
2403                 case 'c':
2404                         cmdstr = optarg;
2405                         got_pass = True;
2406                         break;
2407                 case 'b':
2408                         io_bufsize = MAX(1, atoi(optarg));
2409                         break;
2410                 default:
2411                         usage(pname);
2412                         exit(1);
2413                 }
2414         }
2415
2416         get_myname((*global_myname)?NULL:global_myname);  
2417
2418         if(*new_name_resolve_order)
2419                 lp_set_name_resolve_order(new_name_resolve_order);
2420
2421         if (*term_code)
2422                 interpret_coding_system(term_code);
2423
2424         if (!tar_type && !*query_host && !*service && !message) {
2425                 usage(pname);
2426                 exit(1);
2427         }
2428
2429         DEBUG( 3, ( "Client started (version %s).\n", VERSION ) );
2430
2431         if (tar_type) {
2432                 return do_tar_op(base_directory);
2433         }
2434
2435         if ((p=strchr(query_host,'#'))) {
2436                 *p = 0;
2437                 p++;
2438                 sscanf(p, "%x", &name_type);
2439         }
2440   
2441         if (*query_host) {
2442                 return do_host_query(query_host);
2443         }
2444
2445         if (message) {
2446                 return do_message_op();
2447         }
2448
2449         if (!process(base_directory)) {
2450                 return(1);
2451         }
2452
2453         return(0);
2454 }