commands.c 42.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * @(#) $Id$
 *
 * commands.c
 */

/* lsftp, an implementation of the sftp protocol
 *
 * Copyright (C) 2001, Pontus Sköld
 * Portions of this code originately from the readline manual
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "commands.h"

int ls_longlist = 1;
int ls_all = 0;
int gp_cont = 1;


command commands[] = {
  { "cd", com_cd, "Change to directory DIR", 
    "Change to directory DIR, relative paths\n"
    "are handled by the other side. If DIR is"
    "the empty string, go to your home directory", REMOTEFILE, 1, 2 },

  { "rm", com_rm, "Delete file or directory ", 
    "rm is used to delete files or directories. To delete\n" 
    "directories, you need to specify the r option. Normally,\n"
    "the rm command doesn't glob (you have to give the exact\n"
    "of what you want to delete), but that can be enabled with\n"
    "the -g option.\n",
    REMOTEFILE, 1, 2 },

  { "delete", com_rm, "Synonym for `rm'", 
    "Please see the help for rm.", REMOTEFILE, 0, 2 },

  { "about", com_about, "Display information about lsftp", 
    "Displays some information about lsftp.", NOARG, 1, 1 },  

54
55
56
57
58
59
60
  { "help", com_help, "List available commands or help for command", 
    "help also displays more specific information for certain commands,\n"
    "as you have likely already discovered.", COMMAND, 1, 1 },

  { "longhelp", com_longhelp, "Give more help", 
    "If given a command, this is equal to help (help actually uses longhelp for this).\n"
    "used without arguments, longhelp displays short help texts for all commands.", COMMAND, 1, 2 },
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

  { "?", com_help, "Synonym for `help'", 
    "You can type help COMMAND, but you've already discovered that\n", 
    COMMAND, 0, 1 },

  { "list", com_ls, "Synonym for ls", 
    "Please see the help for ls.", REMOTEFILE, 0, 2 },

  { "ls", com_ls, "List files remotely", 
    "ls lists files or folder remotely. It takes options to modify\n"
    "it's behaviour (normally derived from the global options that\n"
    "you can modify with set). Options may be given after a -(minus)\n"
    "sign. The option l toggles whatever files will be displayed in long\n"
    "listing mode. The a option toggles whatever files with names starting\n"
    "with dots will be shown unless specifically asked for.\n"
    "\n"
    "The ls command accepts globs, put for efficiency everything to the\n"
    "right of the last slash (/) is considered a filename-matching pattern\n"
    "meaning ls fo* lists information about files or folders with names\n"
    "starting with fo. ls fo*/ on the other hand will list information\n"
    "about files in directories with names starting with fo (and give\n"
    "error messages if you have any files with names starting with fo).",
    REMOTEFILE, 1, 2 },


  { "dir", com_ls, "Synonym for `ls'", 
    "Please see the help for ls", REMOTEFILE, 0, 2 },

  { "pwd", com_pwd, "Print the current working directory", 
    "pwd longdoc", NOARG, 1, 2 },

  { "quit", com_quit, "Quit using lsftp",
    "Leave lsftp", NOARG, 1, 1 },

  { "exit", com_quit, "Synonym for `quit'",
    "Leave lsftp", NOARG, 0, 1 },

  { "set", com_set, "Show or set variables",
    "Allows you to show or set variables is lsftp, " 
    "useful for brining mayhem.", OTHERARG, 0, 1 },

  { "mail", com_mail, "Run mail",
    "Starts mail", OTHERARG, 1, 3 },

  { "umask", com_umask, "Show or set umask",
    "Shows or sets your umask", OTHERARG, 1, 1 },


  { "jobs", com_jobs, "Job info",
    "Show status for JOBID", JOBID, 0, 1 },

  { "open", com_open, "Open a new connection to a site",
    "Opens a connection to a site", OTHERARG, 0, 1 },
  { "close", com_close, "Close an existing connection",
    "Close kills an existing connection", NOARG, 0, 2 },



  { "rename", com_mv, "Synonym for `mv'",
    "rename longdoc", REMOTEFILE, 0, 2 },
  { "mv", com_mv, "Rename FILE to NEWNAME",
    "rename longdoc", REMOTEFILE, 1, 2 },


  { "ln", com_ln, "Symlink FILE",
    "ln longdoc", REMOTEFILE, 1, 2 },



  { "get", com_get, "Get FILE to local system",
    "Get longdoc", REMOTEFILE, 1, 1 },

  { "put", com_put, "Put FILE to remote system",
    "Put longdoc", LOCALFILE, 1, 2 },

  { "chown", com_chown, "Change owner of FILE to UID", 
    "chown UID FILE changes the owner of FILE to UID", 
    REMOTEFILE, 1, 3 },

  { "chgrp", com_chgrp, "Change group of FILE to GID", 
    "chgrp GID FILE changes the group of FILE to GID", 
    REMOTEFILE, 1, 3 },

  
  { "lcd", com_lcd, "LOCALLY Change to directory DIR",
    "lcd longdoc" , LOCALFILE, 1, 2 },

  { "lrm", com_lrm, "LOCALLY Delete FILE", 
    "lrm longdoc" , LOCALFILE, 1, 2 },

  { "ldelete", com_lrm, "Synonym for `lrm'", 
    "ldelete longdoc" , LOCALFILE, 0, 2 },


  { "mkdir", com_mkdir, "Makes directory DIR [DIR...]", 
    "Makes a new directory" , OTHERARG, 0, 2 },


  { "llist", com_lls, "List files in LOCAL [DIR]", 
    "llist longdoc" , LOCALFILE, 0, 3 },

  { "lls", com_lls, "List files in LOCAL [DIR]", 
    "lls" , LOCALFILE, 1, 3 },

  { "ldir", com_lls, "Synonym for `lls'", 
    "ldir longdoc" , LOCALFILE, 0, 3 },

  { "lpwd", com_lpwd, "Print the current (local) working directory", 
    "lpwd longdoc" , LOCALFILE, 1, 2 },


  /*  { "lmv", com_lmv, "LOCALLY Rename FILE to NEWNAME", 
    "lmv longdoc" , LOCALFILE, 1 },
    { "lrename", com_lmv, "Synonym for `lmv'", 
    "lrename longdoc" , LOCALFILE, 1 }, */
  
  { "!", com_escape, "Send COMMAND to /bin/sh", 
    "Executes system(\"COMMAND\");" , OTHERARG, 1, 1 },

180
  { NULL, NULL, NULL, NULL, 0, 0, 0 }
181
182
};

183
184
static int
com_disconnected(void)
185
186
187
188
189
{
  printf( "This command doesn't work in disconnected mode!\n" );
  return -1;
}

190
191
static int
com_connected(void)
192
193
194
195
196
{
  printf( "This command doesn't work in connected mode!\n" );
  return -1;
}

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

int       
com_help(const char *arg, const char *command UNUSED)
{ 
  const char *s;
  
  s = lsftp_s_skip(arg," \n\t\r"); /* Skip any initial whitespace */
  
  if( !s || ! (*s) ) /* End of string? */
    {
      int row = 0;
      char* cmdname;
      int i=0;
      
      /* No command given, show the short description */

      while( ( cmdname = commands[i++].name) )
	{
          printf( "%-10s",      /* Print each command */
		  cmdname
		  );
	  row++;

	  if( 8 == row )
	    {
	      printf("\n");
	      row = 0;
	    }
	}

      if( row ) /* Anything printed on this line? */
	printf("\n");

      printf("\nFor more help, try longhelp or help <COMMAND>\n");
      return 0;
    }
  else /* Command given, show long help */
    return com_longhelp( arg, command );

}


239
int
240
com_longhelp(const char *arg, const char *command UNUSED) 
241
{
242
  const char *s;
243
244
245
246
247
248
249
250
251
252
  char tmp[PATH_MAX];
  char* cmdname;
  int i=0;
  
  s = lsftp_s_skip(arg," \n\t\r"); /* Skip any initial whitespace */
  
  if( !s || ! (*s) ) /* End of string? */
    {
      /* No command given, show the short description */

253
254
255
256
257
      while( ( cmdname = commands[i++].name) )
	printf("%-10s -- %s\n",
	       cmdname, 
	       commands[i-1].doc
	       ); 
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
      /* Print command name (10 columns) -- documentation */
        
      return 0;
    }


  while(1)
    {
      int known=0;

      i=0;
      /* Get the first word from the line (the command) */
      s = lsftp_s_strtok(s," \n\t\r", tmp, PATH_MAX); 

  
      if( !s ) /* No more? */
	return 0;

      while( ( cmdname = commands[i++].name) )
	if ( ! strcmp(cmdname, tmp) )          /* We've got the command? */
	  {
	    known=1;
	    printf(
281
		   "%-10s -- %s\n\n%s\n", 
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
		   commands[i-1].name, 
		   commands[i-1].doc,
		   commands[i-1].longdoc
		   );
	    /* Print documentation */
	  }
  
      if( !known ) /* If no command matched, say so */
	printf("Unknown command %s\n", tmp);
    }

  return 0; /* Placeholder to stop complaints from compilers*/

}

297
298
int
com_mail(const char *arg UNUSED, const char *command) 
299
{
300
  return system(command);
301
302
}

303
304
int
com_umask(const char *arg, const char *command UNUSED) 
305
{
306
  unsigned char tmp[PATH_MAX];
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  int given = 0;

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      int newmask = 0;

      given++; /* User gave a new mask */

      if( isdigit( tmp[0] ) )
	{
	  newmask = atol( tmp );
	  umask( newmask );
	}
      else
	printf( "Sorry, only numeric masks allowed\n" );

    }

  if( !given )
    {
      /* FIXME: Better way to check? */

      int oldmask = umask( 0 ); /* Get old and reset it */
      umask( oldmask );

      printf( "Your current umask is %o.\n", oldmask );
    }

338
339
  /* FIXME: What does return value mean? */
  return 0;
340
341
}

342
343
int
com_escape(const char *arg, const char *command UNUSED) 
344
345
346
347
348
349
350
351
352
{
  int ret;
  printf( "Launching %s\n", arg );
  ret = system( arg );
  printf( "Returned from system call %s\n", arg );

  return ret;
}

353
354
int
com_quit(const char *arg UNUSED, const char *command UNUSED) 
355
{
356
357
358
  if( lsftp_connected() )   /* If connected */
    com_close( "", "INTERNAL CLOSE COMMAND" ); /* Close connection */

359
360
361
362
363
364
  printf( "Bye\n" );

  mainloop=0;
  return 0;
}

365
366
int
com_about(const char *arg UNUSED, const char *command UNUSED)
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
{
  printf("%s %s\n", PACKAGE, VERSION);
  printf("Copyright (C) 2001, Pontus Sköld and various contributors\n\n");

  printf("This program is free software, you may distribute it under the\n");
  printf("terms of the GNU Genereal Public License. \n\n");
  
  printf("This program is distributed in the hope that it will be useful,\n");
  printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n");
  printf("General Public License for more details.\n");

  return 0;
}


383
384
int
com_close(const char *arg UNUSED, const char *command UNUSED)
385
386
387
388
389
390
391
392
393
394
{
  /* Close any existing connections */

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  return lsftp_close_connection();
}


395
396
int
com_open(const char *arg, const char *command UNUSED)
397
398
399
{
  char** myargv;
  char** freeargv;
400
  const char *s;
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
  char tmp[PATH_MAX]; /* Used for arguments */

  int argcount = 0;
  int maxargs;

  if( lsftp_connected() )   /* Bail out *if* connected */
    return com_connected();

  if( arg )                        /* Any arguments given? */
    maxargs = strlen( arg ) + 2;  /* We will have at most one pointer per charater in arg */
  else
    maxargs = 2;

  myargv = malloc( maxargs * sizeof( char* ) ); 

  if( myargv )     /* Malloc OK? */
    {
      myargv[argcount++] = "Internal argv[0]"; 

      while( argcount < maxargs &&  /* Still room in our array */
	     arg && 
	     *arg && 
	     ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	     )
	myargv[argcount++] = strdup( tmp );

      if( argcount < maxargs )      /* Exited not because the array was full */
	myargv[argcount] = 0;
      else                          /* Array was full, incredible but true */
	{
	  myargv[--argcount] = 0;
	  printf( "Warning, too many arguments given\n" );
	}

      lsftp_open_connection( myargv, argcount );

      freeargv = myargv;

      freeargv++; /* Skip first entry, we may not free that one */

      while( (s = *(freeargv++) ) )
442
443
	/* Free doesn't take const pointers */
	free((void *) s );
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
      
      free( myargv );

      lsftp_handshake();
      lsftp_sftp_init();
      
      lsftp_do_cd( "" );                   /* Change to our home directory */
    }
  else
    {
      perror( "malloc failed" );
    }

  return 0;
}



462
463
int
com_cd(const char *arg, const char *command UNUSED) 
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
{
  char tmp[PATH_MAX];

  int didcd = 0;

  int fail = 0;


  while( !fail &&
	 !didcd &&
	 arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
479
480
      const char **mglob;
      const char *ptr = 0;
481
482
483
484
485
486
487
488
      const char* tmp2 = lsftp_qualify_path( tmp );

      if( !tmp2 )
	return -1;

      mglob = lsftp_dc_r_startglob( tmp2, 0, 1 );
      free( tmp2 );

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
      if( mglob ) /* Glob returned allright? */
	ptr = *mglob;
      
      if( ptr )
	{
	  if( lsftp_dc_r_isdir( ptr ) < 1 )
	    printf( "Error: %s: Not a directory!\n", ptr );
	  else
	    lsftp_do_cd( ptr );
	  
	  didcd = 1;
	}

      lsftp_dc_endglob( mglob );
    }

  if( !didcd )
    return lsftp_do_cd( "" );
507
508
509

  /* FIXME: What does return value mean? */
  return 0;
510
511
512
}


513
int com_mkdir(const char *arg, const char *command) 
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
{
  char tmp[PATH_MAX];

  int id;
  int mode = 0755;
  int fail = 0;

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( !fail &&
	 arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      id = lsftp_do_mkdir( tmp, mode, command ); /* FIXME: Default mode? */
      fail = ( -1 == lsftp_await_command( id ) );
    }

  return 0;
}

537
538
static int
rm_file_or_folder(const char *path, const char *command, int recursive )
539
{
540
541
542
543
544
  int isdir = lsftp_dc_r_isdir(path);
  char *sub = 0;
  const char **subglob = 0;
  const char **orgglob = 0;
  const char *ptr;
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
  int err = 0;
  int id;

  if( !isdir ) /* Not a directory? */
    {
      id = lsftp_do_rm( path, command ); /* Delete it */
      
      if( id > 0 )
	{
	  lsftp_await_command( id );
	  return 0;
	}

      return id;
    }

  if( -1 == isdir ) /* Status checking failed? */
    {
      printf( "Error: can't determine status of %s.\n", path );
      return -1;
    }

  if(                     /* path is a directory, but user don't want to recurse */
     !recursive 
     )
    {
      printf( "Error: %s is a directory, try -r.\n", path );
      
      return -1;
    }
  
  sub = lsftp_concat( path, "/.*" );

  if( !sub )
    {
      perror( "concat failed" );
      return -1;
    }

  subglob = lsftp_dc_r_startglob( sub, 0, 0 );
  orgglob = subglob;

  if( subglob )
    while( !err && 
	   ( ptr = *(subglob++) ) ) 
      if(
	 !(                                         /* Not . or .. */
	   !strcmp( filename_part( ptr ), "." ) ||
	   !strcmp( filename_part( ptr ), ".." ) 
	   )
	 )
	err = rm_file_or_folder( ptr, command, recursive );
  
  lsftp_dc_endglob( orgglob );
  free( sub );
  
  if( err ) /* Bail out if failed */
    return -1; 
  
  /* Now do all files not starting with a period */
  
  sub = lsftp_concat( path, "/*" );
  
  if( !sub )
    {
      perror( "concat failed" );
      return -1;
    }
  
  subglob = lsftp_dc_r_startglob( sub, 0, 0 );
  orgglob = subglob;

  if( subglob ) /* Glob returned allright */
    while( !err && 
619
	   ( (ptr = *subglob++) ) ) 
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
      err = rm_file_or_folder( ptr, command, recursive );
  
  lsftp_dc_endglob( orgglob );
  free( sub );
  
  if( err ) /* Bail out if failed */
    return -1; 
  
  id = lsftp_do_rmdir( path, command );        /* Delete it */
  
  if( id > 0 )
    {
      lsftp_await_command( id );
      return 0;
     }
  else
    return id;
  
  return 0;
}


642
643
int
com_rm(const char *arg, const char *command) 
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
{
  char tmp[PATH_MAX];

  int recurse = 0;
  int globbing = 0;

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    if( lsftp_get_opts( "rg", tmp )  )
      switch( lsftp_get_opts( "rg", tmp ) )
	{
	case 3: /* Both set? */
	  recurse = !recurse;
	  globbing = !globbing;
	  break;
	case 2: /* -r */
	  recurse = !recurse;
	  break;
	case 1: /* -g */
	  globbing = !globbing;
	  break;
	default:
	  printf( "Unknown option(s) %s\n", tmp );
	}
    else 
      {
	if( !globbing )
	  rm_file_or_folder( tmp, command, recurse );
	else      /* Globbing desired */
	  {
679
680
681
	    const char **mglob;
	    const char **orgglob;
	    const char *ptr;
682
	    
683
684
685
686
687
688
689
690
	    const char* tmp2 = lsftp_qualify_path( tmp );

	    if( !tmp2 )
	      return -1;


	    mglob = lsftp_dc_r_startglob( tmp2, 0, 1 );
	    free( tmp2 );
691
692
693
	    orgglob = mglob;

	    if( mglob ) /* Glob returned allright? */		
694
	      while( (ptr = *mglob++) ) 
695
696
697
698
699
700
701
702
703
704
		rm_file_or_folder( ptr, command, recurse );
	    
	    lsftp_dc_endglob( orgglob );
	  }
      }


  return 0;
}

705
706
int
com_ls(const char *arg, const char *command) 
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
{
  char tmp[PATH_MAX];

  int didls = 0;
  int longlist = ls_longlist;
  int all = ls_all;
  int id;

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      if( lsftp_get_opts( "la", tmp )  ) 
	switch( lsftp_get_opts( "la", tmp ) )
	  {
	  case 3: /* Both set? */
	    longlist = !longlist;
	    all = !all;
	    break;
	  case 2: /* -l */
	    longlist = !longlist;
	    break;
	  case 1: /* -a */
	    all = !all;
	    break;
	  default:
	    printf( "Unknown option(s) %s\n", tmp );
	  }
      else
	{
	  id = lsftp_do_ls( tmp, command, longlist, all ); /* ls does globbing by itself */
	  didls++;
	  
	  lsftp_await_command( id );

/*  	  printf( "Returned from command\n" ); */
	} 
    }
  
  if( !didls ) /* No argument given? */
    {
      id = lsftp_do_ls( "", command, longlist, all );
      lsftp_await_command( id );
    }
  
/*    printf( "Returned from ls\n" ); */
  return 0;
}

760
761
int
com_set(const char *arg, const char *command UNUSED) 
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
{
  char tmp[PATH_MAX];

  int show = 1;

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      if( !strncmp( "sloppy_complete", tmp, strlen( "sloppy_complete" ) ) )
	{
	  char c = tmp[ strlen( "sloppy_complete" ) ];

	  if( !c )
	    printf( "sloppy_complete: %d\n", sloppy_complete );
	  else 
	    if( '=' == c )
	      sloppy_complete = atoi( tmp + strlen( "sloppy_complete=" ) ); 
	    else
	      printf( "I don't understand what you want to do, try sloppy_complete=2\n" );
	  
	  show = 0;
	}

      if( !strncmp( "buggy_server_treshold", tmp, strlen( "buggy_server_treshold" ) ) )
	{
	  char c = tmp[ strlen( "buggy_server_treshold" ) ];

	  if( !c )
	    printf( "buggy_server_treshold: %d\n", buggy_server_treshold );
	  else 
	    if( '=' == c )
	      buggy_server_treshold = atoi( tmp + 
					    strlen( "buggy_server_treshold=" ) ); 
	    else
	      printf( "I don't understand what you want to do, "
		      " try buggy_server_treshold=2\n" );
	  
	  show = 0;
	}

      if( !strncmp( "ls_all", tmp, strlen( "ls_all" ) ) )
	{
	  char c = tmp[ strlen( "ls_all" ) ];

	  if( !c )
	    printf( "ls_all: %d\n", ls_all );
	  else 
	    if( '=' == c )
	      ls_all = atoi( tmp + strlen( "ls_all=" ) ); 
	    else
	      printf( "I don't understand what you want to do, try ls_all=2\n" );

	  show = 0;
	}

      if( !strncmp( "ls_longlist", tmp, strlen( "ls_longlist" ) ) )
	{
	  char c = tmp[ strlen( "ls_longlist" ) ];

	  if( !c )
	    printf( "ls_longlist: %d\n", ls_longlist );
	  else 
	    if( '=' == c )
	      ls_longlist = atoi( tmp + strlen( "ls_longlist=" ) ); 
	    else
	      printf( "I don't understand what you want to do, try ls_longlist=2\n" );

	  show = 0;
	}

      if( !strncmp( "gp_cont", tmp, strlen( "gp_cont" ) ) )
	{
	  char c = tmp[ strlen( "gp_cont" ) ];

	  if( !c )
	    printf( "gp_cont: %d\n", gp_cont );
	  else 
	    if( '=' == c )
	      gp_cont = atoi( tmp + strlen( "gp_cont=" ) ); 
	    else
	      printf( "I don't understand what you want to do, try gp_cont=2\n" );

	  show = 0;
	}

      if( show ) /* Obviously something was given, but we didn't recognise it */
	printf( "Unknown command or variable: %s\n", tmp );
    }
  
  if( show ) /* No argument given? */
    {
      printf( "sloppy_complete: %d\n", sloppy_complete );
      printf( "ls_all: %d\n", ls_all );
      printf( "ls_longlist: %d\n", ls_longlist );
      printf( "gp_cont: %d\n", gp_cont );
      printf( "buggy_server_treshold: %d\n", buggy_server_treshold );
    }
  
  return 0;
}

865
866
867
static int
get_file_or_folder(const char *arg, const char *command,
		   int cont, const char *destname )
868
869
{

870
871
  const char **dirinfo = NULL;
  const char **curdirinfo;
872

873
874
  const char *localwd;
  const char *remotewd;
875

876
  const char *tmp;
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
  char buffer[PATH_MAX+1];

  struct stat st;
  int id;
  int mode = 0700;
  int mask;
  int isdir = lsftp_dc_r_isdir( arg );
  int ret;

  if( -1 == isdir ) /* Unable to determine dir or not? */
    {
      if( !strncmp( filename_part( arg ), "*", 2 ) ||
	  !strncmp( filename_part( arg ), ".*", 3 )
	  )  /* Empty directory will make globbing fail => * (or .*) */
	return 0;
      
      printf( "Unable to determine status of %s\n", arg );
      return -1;
    }

  if( !isdir )  /* Not a directory? */
    {
      int id;
      int ret = -1;
      
      if( !destname )
	id = lsftp_do_get( filename_part( arg ), arg, command, cont ); 
      else
	id = lsftp_do_get( destname, arg, command, cont ); 

      if( id > 0) /* Not a failure? */
	ret = lsftp_await_command( id );

      return ret;
    }

  /* Obviously a directory */
  
  /* FIXME: What do we expect filename_part( "foobar/" ) to return? */
  
  id = lsftp_do_stat( arg, &st );
  
  if( id > 0) /* Not a failure? */
    {
      lsftp_await_command( id );
      mode = st.st_mode;

      if( -1 == mode )
	mode = 0700;       /* FIXME: Default mode */
    }

  tmp = filename_part( arg ); /* Destination */

  if( destname )              /* If given, use it */
    tmp = destname;

  ret = lsftp_dc_l_isdir( tmp );

  if( !ret )       /* Exists, but not a dir? */
    {
      printf( 
	     "Error, %s exists but is not a directory\n", 
	      tmp
	     );
      return -1;
    }
  
  if( -1 == ret ) /* stat failed (probably because no such dir) */
  {
    mask = umask( 0 );
    umask( mask );


    if( mkdir( tmp, (mode & ~mask) | 0700 ) )  /* Make sure we have proper access */
      {
	/* Problem while creating the directory? */
	
	perror( "mkdir failed" );
	return -1;
      }
  }
  
  ret = lsftp_dc_l_isdir( tmp ); /* Check status */
  
  if( 1 != ret ) /* If not directory, bail out */
    {
      printf( "Unable to create directory %s\n", tmp );
      return ret;
    }
  
  localwd = realpath( tmp, buffer ); /* FIXME: Should check boundary in configure somehow */  

  if( !destname ) /* No destination given => likely first call,
		   * make sure arg is an absolute path.
		   */
    {
      id = lsftp_do_realpath( arg, &remotewd );
      
      if( id > 0) /* Not a failure? */
	{
	  lsftp_await_command( id );      
	}
    }
  else
    {
      /* destname is given, trust arg is an absoulte path */

      remotewd = strdup( arg );
    }


  if( !localwd )
    {
      printf( "Error: failed to determine local cwd\n" );
      return -1;
    }
  else if( !remotewd )
    {
      printf( "Error: failed to determine remote cwd\n" );
      return -1;
    }

  /* Place trailing slashes after both paths */

  tmp = lsftp_concat( localwd, "/" );

  if( !tmp )
    {
      perror( "Concat failed\n" );
      return -1;
    }

  /* localwd is a static buffer, so don't free it */

  localwd = tmp;

  tmp = lsftp_concat( remotewd, "/" );

  if( !tmp )
    {
      perror( "Concat failed\n" );
      return -1;
    }

1021
  free( (void *) remotewd );
1022
1023
1024
1025
1026
1027
1028
1029
  remotewd = tmp;
  
  /* Do globbing */

  tmp = lsftp_concat( remotewd, "*" );
  
  if( tmp )
    dirinfo = lsftp_dc_r_startglob( tmp, 0, 1 );
1030
1031
  /* free doesn't accept const poitners */
  free( (void *) tmp );
1032
1033
1034
1035
  
  tmp = lsftp_concat( remotewd, ".*" );
  if( tmp )
    dirinfo = lsftp_dc_r_contglob( tmp, dirinfo, 1 );
1036
1037
  /* free doesn't accept const pointers */
  free( (void *) tmp );
1038
1039
1040
1041
1042
1043
1044

  if( dirinfo )
    {
      curdirinfo = dirinfo;
      
      while( *curdirinfo )
	{
1045
	  const char *curentry = lsftp_skip_common( *curdirinfo, remotewd );   
1046
1047
1048
1049
1050
1051
1052
	  
	  if( !(                                  /* Not . or .. */
		!strcmp( curentry, "." ) ||
		!strcmp( curentry, ".." ) 
		)
	      )
	    {
1053
1054
	      char *src;
	      char *dst;
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

	      src = lsftp_concat( remotewd, curentry );	      
	      dst = lsftp_concat( localwd, curentry );

	      if( src && dst )
		{
		  get_file_or_folder( src, command, cont, dst ); 
		  
		  free( src );
		  free( dst );
		}
	      else
		perror( "Concat failed" );	      
	    }
	  
	  curdirinfo++;  /* Next entry */
	}
      
      lsftp_dc_endglob( dirinfo );
    }
  
1076
1077
  free( (void *) remotewd ); /* Free memory used by to hold remote wd */
  free( (void *) localwd ); /* Free memory used by to hold remote wd */
1078
1079
1080

  return 0;

1081
}
1082

1083
1084
int
com_get(const char *arg, const char *command) 
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
{
  int cont = gp_cont;
  char tmp[PATH_MAX];

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    if( lsftp_get_opts( "c", tmp )  ) 
      switch( lsftp_get_opts( "c", tmp ) )
	{
	case 1:
	  cont = !cont;
	  break;
	default:
	  printf( "Unknown option(s) %s\n", tmp );
	}
    else
      {
1107
1108
1109
	const char **glob;
	const char **orgglob;
	const char *ptr;
1110

1111
1112
1113
1114
1115
1116
1117
1118
	const char* tmp2 = lsftp_qualify_path( tmp );

	if( !tmp2 )
	  return -1;

	glob = lsftp_dc_r_startglob( tmp2, 0, 1 );
	free( tmp2 );

1119
1120
	orgglob = glob;
	
1121
	while( (ptr = *glob++) ) 
1122
1123
1124
1125
1126
1127
1128
1129
	  get_file_or_folder( ptr, command, cont, 0 );
	  
	lsftp_dc_endglob( orgglob );
      }
 
  return 0;
}

1130
1131
1132
static int
put_file_or_folder(const char *arg, const char *command,
		   int cont, const char *destname )
1133
{
1134
1135
  const char **dirinfo = NULL;
  const char **curdirinfo;
1136

1137
1138
  const char *localwd;
  const char *remotewd;
1139

1140
  const char *tmp;
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
  char buffer[PATH_MAX];

  struct stat st;
  int id;
  int mode = 0700;
  int isdir = lsftp_dc_l_isdir( arg );
  int err;
  int ret;

  if( -1 == isdir ) /* Unable to determine dir or not? */
    {
1152
1153
      if( !strncmp( filename_part( arg ), "*", 2 ) )
	/* Empty directory will make globbing fail => * */
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
	return 0;
      
      printf( "Unable to determine status of %s\n", arg );
      return -1;
    }

  if( !isdir )  /* Not a directory? */
    {
      int id;
      int ret = -1;
      
      if( !destname )
	id = lsftp_do_put( arg, filename_part( arg ), command, cont ); 
      else
	id = lsftp_do_put( arg, destname, command, cont ); 

      if( id > 0) /* Not a failure? */
	ret = lsftp_await_command( id );

      return ret;
    }

  /* Obviously a directory */
  
  /* FIXME: What do we expect filename_part( "foobar/" ) to return? */
  
  err = stat( arg, &st );

  if( -1 != err )      /* Not a failure? */
    mode = st.st_mode; /* Assign same modes as we have */
  else
    mode = 0700;       /* FIXME: Default mode */
    
  tmp = filename_part( arg ); /* Destination */

  if( destname )              /* If given, use it */
    tmp = destname;

  ret = lsftp_dc_r_isdir( tmp );
  
  if( !ret )       /* Exists, but not a dir? */
    {
      printf( 
	     "Error, %s exists but is not a directory\n", 
	      tmp
	     );
      return -1;
    }
  
  if( -1 == ret ) /* stat failed (probably because no such dir) */
  {
    id = lsftp_do_mkdir( tmp, mode, command ); /* Make directory */
    
    if( id > 0) /* Not a failure? */
      ret = lsftp_await_command( id );

    ret = lsftp_dc_r_isdir( tmp ); /* Check status */

    if( 1 != ret ) /* If not directory, bail out */
      {
	printf( "Unable to create directory %s\n", tmp );
	return ret;
      }

  }

  localwd = realpath( arg, buffer );

  if( !destname ) /* No destination given => Make path absolute */
    {
 
      id = lsftp_do_realpath( filename_part( arg ), &remotewd );
  
      if( id > 0) /* Not a failure? */
	{
	  lsftp_await_command( id );      
	}
    }
  else
    {
      /* destname is given, trust it is an absoulte path */

      remotewd = strdup( destname );
    }


  if( !localwd )
    {
      printf( "Error: failed to determine local cwd\n" );
      return -1;
    }
  else if( !remotewd )
    {
      printf( "Error: failed to determine remote cwd\n" );
      return -1;

    }


  /* Place trailing slashes after both paths */

  tmp = lsftp_concat( localwd, "/" );

  if( !tmp )
    {
      perror( "Concat failed\n" );
      return -1;
    }

  /* localwd is a static buffer, so don't free it */

  localwd = tmp;

  tmp = lsftp_concat( remotewd, "/" );

  if( !tmp )
    {
      perror( "Concat failed\n" );
      return -1;
    }

1275
  free( (void *) remotewd );
1276
1277
1278
1279
1280
1281
1282
1283
  remotewd = tmp;
  
  /* Do globbing */

  tmp = lsftp_concat( localwd, "*" );
  
  if( tmp )
    dirinfo = lsftp_dc_l_startglob( tmp, 1 );
1284
  free( (void *) tmp );
1285
1286
1287
1288
  
  tmp = lsftp_concat( localwd, ".*" );
  if( tmp )
    dirinfo = lsftp_dc_l_contglob( tmp, dirinfo, 1 );
1289
  free( (void *) tmp );
1290
1291
1292
1293
1294
1295
1296

  if( dirinfo )
    {
      curdirinfo = dirinfo;
      
      while( *curdirinfo )
	{
1297
	  const char *curentry = lsftp_skip_common( *curdirinfo, localwd );   
1298
1299
1300
1301
1302
1303
1304
	  
	  if( !(                                  /* Not . or .. */
		!strcmp( curentry, "." ) ||
		!strcmp( curentry, ".." ) 
		)
	      )
	    {
1305
1306
	      char *src;
	      char *dst;
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
	      
	      src = lsftp_concat( localwd, curentry );
	      dst = lsftp_concat( remotewd, curentry );

	      if( src && dst )
		{

		  put_file_or_folder( src, command, cont, dst ); 
		  
		  free( src );
		  free( dst );
		}
	      else
		perror( "Concat failed" );	      
	    }
	  
	  curdirinfo++;  /* Next entry */
	}
      
      lsftp_dc_endglob( dirinfo );
    }
  
1329
1330
  free( (void *) remotewd ); /* Free memory used by to hold remote wd */
  free( (void *) localwd ); /* Free memory used by to hold remote wd */
1331
1332
1333
1334
1335
1336

  return 0;
}    



1337
int com_put(const char *arg, const char *command) 
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
{
  char tmp[PATH_MAX];
  int cont = gp_cont;

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    if( lsftp_get_opts( "c", tmp )  ) 
      switch( lsftp_get_opts( "c", tmp ) )
	{
	case 1:
	  cont = !cont;
	  break;
	default:
	  printf( "Unknown option(s) %s\n", tmp );
	}
    else
      {
1360
1361
1362
	const char **glob;
	const char **orgglob;
	const char *ptr;
1363
1364
1365
1366
	
	glob = lsftp_dc_l_startglob( tmp, 1 );
	orgglob = glob;
	
1367
	while( (ptr = *glob++) ) 
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
	  put_file_or_folder( ptr, command, cont, 0 );
	  
	lsftp_dc_endglob( orgglob );
      }
  
 
  return 0;
}


1378
int com_chown(const char *arg, const char *command) 
1379
{
1380
  unsigned char tmp[PATH_MAX];
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
  int gotuid = 0;
  int enough_parameters = 0;
  long newuid = 0;
  struct stat st;

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    if( !gotuid ) 
      {
	if( isdigit( tmp[0] ) ) /* Numeric GID? FIXME: Better check? */
	  newuid = atol( tmp );
	else
	  {
	    struct passwd* pwd;

	    printf( "Warning: performing LOCAL lookup of user %s\n", tmp ); /* Warn user */
	    
	    pwd = getpwnam( tmp );

	    if( pwd ) /* OK? */
	      newuid = pwd->pw_uid; /* get uid */
	    else
	      {
		printf( "Lookup failed!\n" );
		return -1; /* Signal failure */
	      }
	  }

	gotuid = 1;
      }
    else
      {
	int id;
	
1420
1421
1422
	const char **glob;
	const char **orgglob;
	const char *ptr;
1423
1424
1425
1426
1427
1428

	enough_parameters++;
	
	glob = lsftp_dc_l_startglob( tmp, 1 );
	orgglob = glob;
	
1429
	while( (ptr = *glob++) )
1430
1431
1432
1433
1434
1435
1436
1437
1438
	  {
	    id = lsftp_do_stat( ptr, &st );

       	    if( id > 0) /* Not a failure? */
	      {
		lsftp_await_command( id );
		lsftp_do_chown( ptr, newuid, st.st_gid, command );
	      }
	    
1439
	    free( (void *) ptr );
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
	  }
	
	free( orgglob );
      }
	

  if( !enough_parameters )
    {
      printf( "chown needs at least two parameters\n" );
      return -1;
    }

  return 0;
}


1456
int com_chgrp(const char *arg, const char *command) 
1457
{
1458
  unsigned char tmp[PATH_MAX];
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
  int gotgid = 0;
  long newgid = 0;
  int enough_parameters = 0;
  struct stat st;

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    if( !gotgid )  /* First time? */
      {
	if( isdigit( tmp[0] ) ) /* Numeric GID? FIXME: Better check? */
	  newgid = atol( tmp );
	else
	  {
	    struct group* gr;

	    printf("Warning: performing LOCAL lookup of group %s\n", tmp ); /* Warn user */
	    
	    gr = getgrnam( tmp );

	    if( gr ) /* OK? */
	      newgid = gr->gr_gid; /* Get gid */
	    else
	      {
		printf("Lookup failed!\n");
		return -1; /* Signal failure */
	      }
	  }

	gotgid = 1;
      }
    else
      {
	int id;

	enough_parameters++;
	id = lsftp_do_stat( tmp, &st );

	if( id > 0) /* Not a failure? */
	  {
	    lsftp_await_command( id );
	  
	    lsftp_do_chown( tmp, st.st_uid, newgid, command );
	  }
      }

  if( !enough_parameters )
    {
      printf( "chgrp needs at least two parameters\n" );
      return -1;
    }

  return 0;
}

1518
1519
int
com_jobs(const char *arg, const char *command UNUSED)
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
{
  int listjobs = 0;
  char tmp[PATH_MAX];

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      int jobid;

      jobid = atoi( tmp );
      lsftp_cb_status( jobid ); 
      listjobs++;
    }

  if( !listjobs )
    lsftp_cb_list();

  return 0;
}


1543
1544
int
com_pwd(const char *arg UNUSED, const char *command UNUSED) 
1545
1546
1547
1548
1549
1550
1551
1552
{
  char* pwd;

  pwd = lsftp_pwd();

  if( pwd )
    printf( "%s\n", pwd );
  else
1553
    printf( "Current working directory not available\n" );
1554
1555
1556
1557

  return 0;
}

1558
1559
int
com_mv(const char *arg, const char *command) 
1560
1561
1562
{
  char tmp[PATH_MAX];
  char dst[PATH_MAX+1];
1563
  const char* orgarg = arg;
1564
1565
  int i = 0;
  int j = 0;
1566
1567
1568
  const char **glob;
  const char **orgglob;
  const char *ptr;  
1569
1570
1571
1572
1573
1574
1575
1576
1577

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
1578
1579
1580
1581
1582
1583
1584
1585
      const char* tmp2 = lsftp_qualify_path( tmp );

      if( !tmp2 )
	return -1;

      glob = lsftp_dc_r_startglob( tmp2, 0, 1 );
      free( tmp2 );

1586
1587
      orgglob = glob;
      
1588
      while( (ptr = *glob++) ) 
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
	{
	  strncpy( dst, ptr, PATH_MAX );
	  i++;
	}

      lsftp_dc_endglob( orgglob );
      }
  
  if( i <= 1 ) /* To few arguments */
    {
      printf( "mv needs at least two parameters\n" );
      return -1;
    }
  
  arg = orgarg;

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
1610
1611
1612
1613
1614
1615
1616
1617
      const char* tmp2 = lsftp_qualify_path( tmp );

      if( !tmp2 )
	return -1;

      glob = lsftp_dc_r_startglob( tmp2, 0, 1 );
      free( tmp2 );

1618
1619
      orgglob = glob;
      
1620
      while( (ptr = *glob++) ) 
1621
1622
1623
1624
	{
	  j++;
	  if( j != i ) /* Not the last argument? */
	    {
1625
1626
1627
1628
1629
1630
	      /* More than one source and one destination => we should append src to dst/.
	       * Same goes if dst is a directory */

	      if( 2 != i ||
		  ( lsftp_dc_r_isdir( dst ) > 0 )
		  ) 
1631
		{
1632
1633
		  char *tmp1 = lsftp_concat( dst, "/" );
		  char *tmp2 = NULL;
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648

		  if( tmp1 )
		    tmp2 = lsftp_concat( tmp1, filename_part( ptr ) );          /* And make the new path for ptr (dst/ptr or so) */
		  
		  if( tmp2 )		    
		    lsftp_do_mv( ptr, tmp2, command );
		  else
		    perror( "problem while working with strings" );
	
		  free( tmp1 );
		  free( tmp2 );
		}
	      else
		lsftp_do_mv( ptr, dst, command );
	    }
1649
	  free( (void *) ptr );
1650
1651
1652
1653
1654
1655
1656
1657
	}

      free( orgglob );
      }

  return 0;
}

1658
1659
int
com_ln(const char *arg, const char *command) 
1660
1661
1662
1663
{
  char link[PATH_MAX];
  char tmp[PATH_MAX];

1664
  const char *orgarg = arg;
1665
1666
  int i = 0;
  int j = 0;
1667
1668
1669
  const char **glob;
  const char **orgglob;
  const char *ptr;  
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();




  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      glob = lsftp_dc_r_startglob( tmp, 0, 1 );
      orgglob = glob;
      
1685
      while( (ptr = *glob++) ) 
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
	{
	  strncpy( link, ptr, PATH_MAX );
	  i++;
	}
      
      lsftp_dc_endglob( orgglob );
    }
  
  if( i < 1 ) /* To few arguments */
    {
      printf( "ln needs at least one parameters\n" );
      return -1;
    }
  
  if( 1 == i ) /* Exactly one argument? */
    {
      /* User hopefully wants to create a symlink to that argument in the current directory 
       *
       * So linkpath will be the the filename part of the argument,
       * and linkpath the full argument.
       * 
       */

      lsftp_do_ln( filename_part( link ), link, command );

      return 0;
   }

  /* Many arguments 
   *
   * The user wants to make links 
   *
   */
  
  arg = orgarg;

  while( arg && 
	 *arg && 
	 ( arg = lsftp_s_strtok( arg," \n\t\r", tmp, PATH_MAX ) ) 
	 )
    {
      glob = lsftp_dc_r_startglob( tmp, 0, 1 );
      orgglob = glob;
      
1730
      while( (ptr = *glob++) ) 
1731
1732
1733
1734
1735
1736
	{
	  j++;
	  if( j != i ) /* Not the last argument? */
	    {
	      if( 1 == lsftp_dc_r_isdir( link ) ) /* Link is a directory?  */
		{
1737
1738
		  char *tmp1 = lsftp_concat( link, "/" );
		  char *tmp2 = NULL;
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753

		  if( tmp1 ) /* And make the new path for ptr (link/ptr or so) */
		    tmp2 = lsftp_concat( tmp1, filename_part( ptr ) ); 
		  
		  if( tmp2 )		    
		    lsftp_do_ln( link, tmp2, command );
		  else
		    perror( "problem while working with strings" );
	
		  free( tmp1 );
		  free( tmp2 );
		}
	      else
		lsftp_do_ln( link, ptr, command );
	    }
1754
	  free( (void *) ptr );
1755
1756
1757
1758
1759
1760
1761
1762
	}

      free( orgglob );
      }

  return 0;
}

1763
1764
int
com_lcd(const char *arg, const char *command UNUSED) 
1765
1766
{
  char tmp[PATH_MAX];
1767
  const char **gldata = 0;
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
  int ret;

  while( 
	( arg =
	  lsftp_s_strtok( arg, " \n\t\r", tmp, PATH_MAX )
	  )
	) /* Get argument */    
    gldata = lsftp_dc_l_contglob( tmp, gldata, 1 );

  switch( lsftp_dc_numglob( gldata )  )
    {
    case 0:        /* No path given, go home */
      ret = chdir("~");
      break;
      
    case 1:
      ret = chdir( gldata[0] );
      break;
      
    default:
      printf( "lcd: To many arguments (expected one, got %d)\n", 
	      lsftp_dc_numglob( gldata )
	      );
    }
  
  lsftp_dc_endglob( gldata );
  
  return 0;
}

1798
1799
int
com_lrm(const char *arg UNUSED, const char *command UNUSED) 
1800
1801
1802
1803
1804
1805
1806
1807
1808
{
  printf("lrm\n");

  if( !lsftp_connected() )   /* Bail out if not connected */
    return com_disconnected();

  return 0;
}

1809
1810
1811
/* FIXME: Use idcache.c? */
static const char *
uidstring( int uid )
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
{
  static char uids[10];
  struct passwd* p = getpwuid( uid );

  if( p ) /* Negative result */
    return p->pw_name;

  snprintf( uids, 9, "%d", uid );

  return uids;
}

1824
1825
static const char *
gidstring( int gid )
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
{
  static char gids[10];
  struct group* g = getgrgid( gid );

  if( g )
    return g->gr_name;

  snprintf( gids, 9, "%d", gid );
  return gids;
}

1837
1838
1839
/* FIXME: Use filemode.c? */
int
com_lls(const char *arg, const char *command UNUSED) 
1840
1841
{
  char tmp[PATH_MAX];
1842
  const char **gldata = 0;
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
  struct stat st;
  int i = 0;

  int longlist = ls_longlist;
  int all = ls_all;

  int args = 0;
  int err;

  while( 
	( arg =
	  lsftp_s_strtok( arg, " \n\t\r", tmp, PATH_MAX )
	  )
	) /* Get argument */    
    {
      if( lsftp_get_opts( "la", tmp )  ) 
	switch( lsftp_get_opts( "la", tmp ) )
	  {
	  case 3: /* Both set? */
	    longlist = !longlist;
	    all = !all;
	    break;
	  case 2: /* -l */
	    longlist = !longlist;
	    break;
	  case 1: /* -a */
	    all = !all;
	    break;
	  default:
	    printf( "Unknown option(s) %s\n", tmp );
	  }
      else
	{
	  gldata = lsftp_dc_l_contglob( tmp, gldata, 1 );
	  args++;
	}
    }

  if( !args )
    {
       gldata = lsftp_dc_l_contglob( ".*", gldata, 1 );
       gldata = lsftp_dc_l_contglob( "*", gldata, 1 );
    }

  if( gldata )

    while( gldata[i] )
      {
	err = stat( gldata[i], &st );

	if( err )
	  printf( "%s: not found\n", gldata[i] );
1895
1896
1897
	else if( all || ( gldata[i][0] != '.' ) )
	  {
	    /* Show all files or not staring with . */
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
	    if( ! longlist )
	      printf( "%s\n", gldata[i] );
	    else
	      {
		int j;
		char modestring[11];

		for( j=0; j < 11; j++ )
		  modestring[j] = '-';
		modestring[10] = 0;

		if( S_ISDIR( st.st_mode ) )
		  modestring[0] = 'd';
		else if( S_ISBLK( st.st_mode ) )
		  modestring[0] = 'b';
		else if( S_ISCHR( st.st_mode ) )
		  modestring[0] = 'c';
		else if( S_ISSOCK( st.st_mode ) )
		  modestring[0] = 's';
		else if( S_ISFIFO( st.st_mode ) )
		  modestring[0] = 'p';

		if( st.st_mode & S_IRUSR )
		  modestring[1] = 'r';
		if( st.st_mode & S_IWUSR )
		  modestring[2] = 'w';
		if( st.st_mode & S_IXUSR )
		  modestring[3] = 'x';

		if( st.st_mode & S_IRGRP )
		  modestring[4] = 'r';
		if( st.st_mode & S_IWGRP )
		  modestring[5] = 'w';
		if( st.st_mode & S_IXGRP )
		  modestring[6] = 'x';

		if( st.st_mode & S_IROTH )
		  modestring[7] = 'r';
		if( st.st_mode & S_IWOTH )
		  modestring[8] = 'w';
		if( st.st_mode & S_IXOTH )
		  modestring[9] = 'x';

		if( st.st_mode & S_ISUID )
1942
1943
1944
1945
1946
1947
1948
		  {
		    if( st.st_mode & S_IXUSR )
		      modestring[3] = 's';
		    else
		      modestring[3] = 'S';
		  }
		
1949
		if( st.st_mode & S_ISGID )
1950
1951
1952
1953
1954
1955
		  {
		    if( st.st_mode & S_IXGRP )
		      modestring[6] = 's';
		    else
		      modestring[6] = 'S';
		  }
1956
		
1957
1958
1959
1960
1961
1962
1963
1964
		if( st.st_mode & S_ISVTX )
		  {
		    if( st.st_mode & S_IXOTH )
		      modestring[6] = 't';
		    else
		      modestring[6] = 'T';
		  }
		/* FIXME: Doesn't handle off_t larger than long */
1965
		/* FIXME: st_nlink is a long on Solaris */
1966
		printf( 
1967
		       "%s %4ld %-8s %-8s %8ld ", 
1968
		       modestring, 
1969
		       (long) st.st_nlink, 
1970
1971
		       uidstring( st.st_uid ), 
		       gidstring( st.st_gid ),
1972
		       (long) st.st_size 
1973
1974
1975
1976
1977
		       );
		
		printf( "%s\n", gldata[i] );

	      }
1978
	  }
1979
1980
1981
1982
1983
1984
1985
1986
	i++;
      }
  
  lsftp_dc_endglob( gldata );
  
  return 0;
}

1987
1988
int
com_lpwd(const char *arg UNUSED, const char *command UNUSED) 
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
{
  char* pwd;
  pwd = getcwd( NULL, PATH_MAX ); /* Get where we are now */

  if( !pwd )            /* Failed? */
    return -1;

  printf( "%s\n",pwd ); /*  Print it */
  free( pwd );
  return 0;      
  
}

2002
2003
int
com_lmv(const char *arg UNUSED, const char *command UNUSED) 
2004
2005
2006
2007
2008
2009
2010
{
  printf("lmv\n");

  return 0;
}


2011
int
2012
handle_command(const unsigned char *s)
2013
{
2014
  const char *entered_cmd = s;
2015
  char tmp[WORDLENMAX];
2016
  char *cmdname;
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
  int i=0;
    
  s = lsftp_s_skip(s," \n\t\r"); /* Skip any initial whitespace */
  
  if( !s || !(*s) ) /* s is NULL or contains only whitespace */
    /* No command */
    return  NOCOMMAND;

  /* Special cases for ! and ? commands - allow arguments without whitespace separator */

  if( s && s[0] == '!' && isalnum( s[1] ) ) /* Commands is !x... ? */
    return com_escape( s+1, entered_cmd );

  if( s && s[0] == '?' && isalnum( s[1] ) ) /* Commands is ?x... ? */
2031
    return com_longhelp( s+1, entered_cmd );
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061

  /* Get the first word from the line (the command) */

  s = lsftp_s_strtok(s," \n\t\r", tmp, WORDLENMAX); 
  
  if(!s) /* s equals null ? */
    return NOCOMMAND;

  while( 
	( cmdname = commands[i].name ) 
	)
    {
      int tmplen = strlen( tmp );
      int neededlen = commands[i++].uniquelen;
      
      if( neededlen <= tmplen  &&  /* Sufficiently long string? */
	  !strncmp( cmdname, tmp, tmplen )  /* Matching command? */                
	  )          
	return commands[i-1].func( 
				lsftp_s_skip(s," \n\t\r"), 
				entered_cmd 
				);
	
    }

  printf("Unknown command %s\n", tmp);
  return -UNKNOWNCOMMAND;

}