summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2004-06-22 13:06:45 +1000
committerDarren Tucker <dtucker@zip.com.au>2004-06-22 13:06:45 +1000
commitb9123453d015dd3e4590a4fc9970e973a0499a28 (patch)
tree4b9fde549c1ae8bf657cfe7cf8348d05c5a98952
parentefa62f98a140e238256c2f75f4cd8a282fe802a3 (diff)
- djm@cvs.openbsd.org 2004/06/21 22:04:50
[sftp.c] introduce sorting for ls, same options as /bin/ls; ok markus@
-rw-r--r--ChangeLog5
-rw-r--r--sftp.c52
2 files changed, 49 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index b1e713024..657147779 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -24,6 +24,9 @@
24 - djm@cvs.openbsd.org 2004/06/21 22:02:58 24 - djm@cvs.openbsd.org 2004/06/21 22:02:58
25 [log.h] 25 [log.h]
26 mark fatal and cleanup exit as __dead; ok markus@ 26 mark fatal and cleanup exit as __dead; ok markus@
27 - djm@cvs.openbsd.org 2004/06/21 22:04:50
28 [sftp.c]
29 introduce sorting for ls, same options as /bin/ls; ok markus@
27 30
2820040620 3120040620
29 - (tim) [configure.ac Makefile.in] Only change TEST_SHELL on broken platforms. 32 - (tim) [configure.ac Makefile.in] Only change TEST_SHELL on broken platforms.
@@ -1346,4 +1349,4 @@
1346 - (djm) Trim deprecated options from INSTALL. Mention UsePAM 1349 - (djm) Trim deprecated options from INSTALL. Mention UsePAM
1347 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu 1350 - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
1348 1351
1349$Id: ChangeLog,v 1.3423 2004/06/22 02:57:44 dtucker Exp $ 1352$Id: ChangeLog,v 1.3424 2004/06/22 03:06:45 dtucker Exp $
diff --git a/sftp.c b/sftp.c
index d818ef84e..91e9cfd81 100644
--- a/sftp.c
+++ b/sftp.c
@@ -16,7 +16,7 @@
16 16
17#include "includes.h" 17#include "includes.h"
18 18
19RCSID("$OpenBSD: sftp.c,v 1.51 2004/06/21 17:36:31 avsm Exp $"); 19RCSID("$OpenBSD: sftp.c,v 1.52 2004/06/21 22:04:50 djm Exp $");
20 20
21#include "buffer.h" 21#include "buffer.h"
22#include "xmalloc.h" 22#include "xmalloc.h"
@@ -49,6 +49,9 @@ int showprogress = 1;
49/* SIGINT received during command processing */ 49/* SIGINT received during command processing */
50volatile sig_atomic_t interrupted = 0; 50volatile sig_atomic_t interrupted = 0;
51 51
52/* I wish qsort() took a separate ctx for the comparison function...*/
53int sort_flag;
54
52int remote_glob(struct sftp_conn *, const char *, int, 55int remote_glob(struct sftp_conn *, const char *, int,
53 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ 56 int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
54 57
@@ -61,11 +64,17 @@ char *__progname;
61/* Separators for interactive commands */ 64/* Separators for interactive commands */
62#define WHITESPACE " \t\r\n" 65#define WHITESPACE " \t\r\n"
63 66
64/* Define what type of ls view */ 67/* ls flags */
65#define LONG_VIEW 1 /* Full view ala ls -l */ 68#define LONG_VIEW 0x01 /* Full view ala ls -l */
66#define SHORT_VIEW 2 /* Single row view ala ls -1 */ 69#define SHORT_VIEW 0x02 /* Single row view ala ls -1 */
67#define NUMERIC_VIEW 4 /* Long view with numeric uid/gid */ 70#define NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */
71#define NAME_SORT 0x08 /* Sort by name (default) */
72#define TIME_SORT 0x10 /* Sort by mtime */
73#define SIZE_SORT 0x20 /* Sort by file size */
74#define REVERSE_SORT 0x40 /* Reverse sort order */
75
68#define VIEW_FLAGS (LONG_VIEW|SHORT_VIEW|NUMERIC_VIEW) 76#define VIEW_FLAGS (LONG_VIEW|SHORT_VIEW|NUMERIC_VIEW)
77#define SORT_FLAGS (NAME_SORT|TIME_SORT|SIZE_SORT)
69 78
70/* Commands for interactive mode */ 79/* Commands for interactive mode */
71#define I_CHDIR 1 80#define I_CHDIR 1
@@ -336,6 +345,9 @@ parse_ls_flags(const char **cpp, int *lflag)
336{ 345{
337 const char *cp = *cpp; 346 const char *cp = *cpp;
338 347
348 /* Defaults */
349 *lflag = NAME_SORT;
350
339 /* Check for flags */ 351 /* Check for flags */
340 if (cp++[0] == '-') { 352 if (cp++[0] == '-') {
341 for(; strchr(WHITESPACE, *cp) == NULL; cp++) { 353 for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
@@ -352,6 +364,20 @@ parse_ls_flags(const char **cpp, int *lflag)
352 *lflag &= ~VIEW_FLAGS; 364 *lflag &= ~VIEW_FLAGS;
353 *lflag |= NUMERIC_VIEW|LONG_VIEW; 365 *lflag |= NUMERIC_VIEW|LONG_VIEW;
354 break; 366 break;
367 case 'S':
368 *lflag &= ~SORT_FLAGS;
369 *lflag |= SIZE_SORT;
370 break;
371 case 't':
372 *lflag &= ~SORT_FLAGS;
373 *lflag |= TIME_SORT;
374 break;
375 case 'r':
376 *lflag |= REVERSE_SORT;
377 break;
378 case 'f':
379 *lflag &= ~SORT_FLAGS;
380 break;
355 default: 381 default:
356 error("Invalid flag -%c", *cp); 382 error("Invalid flag -%c", *cp);
357 return(-1); 383 return(-1);
@@ -611,8 +637,17 @@ sdirent_comp(const void *aa, const void *bb)
611{ 637{
612 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; 638 SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
613 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; 639 SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
640 int rmul = sort_flag & REVERSE_SORT ? -1 : 1;
614 641
615 return (strcmp(a->filename, b->filename)); 642#define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
643 if (sort_flag & NAME_SORT)
644 return (rmul * strcmp(a->filename, b->filename));
645 else if (sort_flag & TIME_SORT)
646 return (rmul * NCMP(a->a.mtime, b->a.mtime));
647 else if (sort_flag & SIZE_SORT)
648 return (rmul * NCMP(a->a.size, b->a.size));
649
650 fatal("Unknown ls sort type");
616} 651}
617 652
618/* sftp ls.1 replacement for directories */ 653/* sftp ls.1 replacement for directories */
@@ -648,7 +683,10 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
648 colspace = MIN(colspace, width); 683 colspace = MIN(colspace, width);
649 } 684 }
650 685
651 qsort(d, n, sizeof(*d), sdirent_comp); 686 if (lflag & SORT_FLAGS) {
687 sort_flag = lflag & (SORT_FLAGS|REVERSE_SORT);
688 qsort(d, n, sizeof(*d), sdirent_comp);
689 }
652 690
653 for (n = 0; d[n] != NULL && !interrupted; n++) { 691 for (n = 0; d[n] != NULL && !interrupted; n++) {
654 char *tmp, *fname; 692 char *tmp, *fname;