diff options
author | Damien Miller <djm@cvs.openbsd.org> | 2004-06-21 22:04:51 +0000 |
---|---|---|
committer | Damien Miller <djm@cvs.openbsd.org> | 2004-06-21 22:04:51 +0000 |
commit | 45fc3a84bf4970c7d35cceca4621773db5160429 (patch) | |
tree | 9236e53931a10402b5f3f4fc416c581e61fef92b /usr.bin/ssh | |
parent | 2052fcc5c3f920b3d8b5f68ac493c304ebee35d1 (diff) |
introduce sorting for ls, same options as /bin/ls; ok markus@
Diffstat (limited to 'usr.bin/ssh')
-rw-r--r-- | usr.bin/ssh/sftp.c | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/usr.bin/ssh/sftp.c b/usr.bin/ssh/sftp.c index cff075a66c8..d42887b423f 100644 --- a/usr.bin/ssh/sftp.c +++ b/usr.bin/ssh/sftp.c @@ -16,7 +16,7 @@ #include "includes.h" -RCSID("$OpenBSD: sftp.c,v 1.51 2004/06/21 17:36:31 avsm Exp $"); +RCSID("$OpenBSD: sftp.c,v 1.52 2004/06/21 22:04:50 djm Exp $"); #include <glob.h> @@ -51,17 +51,26 @@ int showprogress = 1; /* SIGINT received during command processing */ volatile sig_atomic_t interrupted = 0; +/* I wish qsort() took a separate ctx for the comparison function...*/ +int sort_flag; + int remote_glob(struct sftp_conn *, const char *, int, int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */ /* Separators for interactive commands */ #define WHITESPACE " \t\r\n" -/* Define what type of ls view */ -#define LONG_VIEW 1 /* Full view ala ls -l */ -#define SHORT_VIEW 2 /* Single row view ala ls -1 */ -#define NUMERIC_VIEW 4 /* Long view with numeric uid/gid */ +/* ls flags */ +#define LONG_VIEW 0x01 /* Full view ala ls -l */ +#define SHORT_VIEW 0x02 /* Single row view ala ls -1 */ +#define NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */ +#define NAME_SORT 0x08 /* Sort by name (default) */ +#define TIME_SORT 0x10 /* Sort by mtime */ +#define SIZE_SORT 0x20 /* Sort by file size */ +#define REVERSE_SORT 0x40 /* Reverse sort order */ + #define VIEW_FLAGS (LONG_VIEW|SHORT_VIEW|NUMERIC_VIEW) +#define SORT_FLAGS (NAME_SORT|TIME_SORT|SIZE_SORT) /* Commands for interactive mode */ #define I_CHDIR 1 @@ -332,6 +341,9 @@ parse_ls_flags(const char **cpp, int *lflag) { const char *cp = *cpp; + /* Defaults */ + *lflag = NAME_SORT; + /* Check for flags */ if (cp++[0] == '-') { for(; strchr(WHITESPACE, *cp) == NULL; cp++) { @@ -348,6 +360,20 @@ parse_ls_flags(const char **cpp, int *lflag) *lflag &= ~VIEW_FLAGS; *lflag |= NUMERIC_VIEW|LONG_VIEW; break; + case 'S': + *lflag &= ~SORT_FLAGS; + *lflag |= SIZE_SORT; + break; + case 't': + *lflag &= ~SORT_FLAGS; + *lflag |= TIME_SORT; + break; + case 'r': + *lflag |= REVERSE_SORT; + break; + case 'f': + *lflag &= ~SORT_FLAGS; + break; default: error("Invalid flag -%c", *cp); return(-1); @@ -607,8 +633,17 @@ sdirent_comp(const void *aa, const void *bb) { SFTP_DIRENT *a = *(SFTP_DIRENT **)aa; SFTP_DIRENT *b = *(SFTP_DIRENT **)bb; + int rmul = sort_flag & REVERSE_SORT ? -1 : 1; - return (strcmp(a->filename, b->filename)); +#define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1)) + if (sort_flag & NAME_SORT) + return (rmul * strcmp(a->filename, b->filename)); + else if (sort_flag & TIME_SORT) + return (rmul * NCMP(a->a.mtime, b->a.mtime)); + else if (sort_flag & SIZE_SORT) + return (rmul * NCMP(a->a.size, b->a.size)); + + fatal("Unknown ls sort type"); } /* sftp ls.1 replacement for directories */ @@ -644,7 +679,10 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) colspace = MIN(colspace, width); } - qsort(d, n, sizeof(*d), sdirent_comp); + if (lflag & SORT_FLAGS) { + sort_flag = lflag & (SORT_FLAGS|REVERSE_SORT); + qsort(d, n, sizeof(*d), sdirent_comp); + } for (n = 0; d[n] != NULL && !interrupted; n++) { char *tmp, *fname; |