1
0
mirror of git://anongit.mindrot.org/openssh.git synced 2025-04-01 00:06:45 +00:00

- djm@cvs.openbsd.org 2010/01/13 01:40:16

[sftp.c sftp-server.c sftp.1 sftp-common.c sftp-common.h]
     support '-h' (human-readable units) for sftp's ls command, just like
     ls(1); ok dtucker@
This commit is contained in:
Darren Tucker 2010-01-13 22:44:06 +11:00
parent daaa450051
commit 2901e2daeb
6 changed files with 51 additions and 28 deletions

View File

@ -18,6 +18,10 @@
[canohost.c ssh-keysign.c sshconnect2.c] [canohost.c ssh-keysign.c sshconnect2.c]
Make HostBased authentication work with a ProxyCommand. bz #1569, patch Make HostBased authentication work with a ProxyCommand. bz #1569, patch
from imorgan at nas nasa gov, ok djm@ from imorgan at nas nasa gov, ok djm@
- djm@cvs.openbsd.org 2010/01/13 01:40:16
[sftp.c sftp-server.c sftp.1 sftp-common.c sftp-common.h]
support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@
20100112 20100112
- (dtucker) OpenBSD CVS Sync - (dtucker) OpenBSD CVS Sync

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sftp-common.c,v 1.20 2006/08/03 03:34:42 deraadt Exp $ */ /* $OpenBSD: sftp-common.c,v 1.21 2010/01/13 01:40:16 djm Exp $ */
/* /*
* Copyright (c) 2001 Markus Friedl. All rights reserved. * Copyright (c) 2001 Markus Friedl. All rights reserved.
* Copyright (c) 2001 Damien Miller. All rights reserved. * Copyright (c) 2001 Damien Miller. All rights reserved.
@ -36,6 +36,7 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <stdarg.h> #include <stdarg.h>
#include <util.h>
#include "xmalloc.h" #include "xmalloc.h"
#include "buffer.h" #include "buffer.h"
@ -184,7 +185,7 @@ fx2txt(int status)
* drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
*/ */
char * char *
ls_file(const char *name, const struct stat *st, int remote) ls_file(const char *name, const struct stat *st, int remote, int si_units)
{ {
int ulen, glen, sz = 0; int ulen, glen, sz = 0;
struct passwd *pw; struct passwd *pw;
@ -192,6 +193,7 @@ ls_file(const char *name, const struct stat *st, int remote)
struct tm *ltime = localtime(&st->st_mtime); struct tm *ltime = localtime(&st->st_mtime);
char *user, *group; char *user, *group;
char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
char sbuf[FMT_SCALED_STRSIZE];
strmode(st->st_mode, mode); strmode(st->st_mode, mode);
if (!remote && (pw = getpwuid(st->st_uid)) != NULL) { if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
@ -216,8 +218,15 @@ ls_file(const char *name, const struct stat *st, int remote)
tbuf[0] = '\0'; tbuf[0] = '\0';
ulen = MAX(strlen(user), 8); ulen = MAX(strlen(user), 8);
glen = MAX(strlen(group), 8); glen = MAX(strlen(group), 8);
snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, if (si_units) {
(u_int)st->st_nlink, ulen, user, glen, group, fmt_scaled((long long)st->st_size, sbuf);
(unsigned long long)st->st_size, tbuf, name); snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode,
(u_int)st->st_nlink, ulen, user, glen, group,
sbuf, tbuf, name);
} else {
snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
(u_int)st->st_nlink, ulen, user, glen, group,
(unsigned long long)st->st_size, tbuf, name);
}
return xstrdup(buf); return xstrdup(buf);
} }

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sftp-common.h,v 1.10 2006/08/03 03:34:42 deraadt Exp $ */ /* $OpenBSD: sftp-common.h,v 1.11 2010/01/13 01:40:16 djm Exp $ */
/* /*
* Copyright (c) 2001 Markus Friedl. All rights reserved. * Copyright (c) 2001 Markus Friedl. All rights reserved.
@ -46,6 +46,6 @@ void stat_to_attrib(const struct stat *, Attrib *);
void attrib_to_stat(const Attrib *, struct stat *); void attrib_to_stat(const Attrib *, struct stat *);
Attrib *decode_attrib(Buffer *); Attrib *decode_attrib(Buffer *);
void encode_attrib(Buffer *, const Attrib *); void encode_attrib(Buffer *, const Attrib *);
char *ls_file(const char *, const struct stat *, int); char *ls_file(const char *, const struct stat *, int, int);
const char *fx2txt(int); const char *fx2txt(int);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sftp-server.c,v 1.90 2010/01/09 00:20:26 djm Exp $ */ /* $OpenBSD: sftp-server.c,v 1.91 2010/01/13 01:40:16 djm Exp $ */
/* /*
* Copyright (c) 2000-2004 Markus Friedl. All rights reserved. * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
* *
@ -940,7 +940,7 @@ process_readdir(void)
continue; continue;
stat_to_attrib(&st, &(stats[count].attrib)); stat_to_attrib(&st, &(stats[count].attrib));
stats[count].name = xstrdup(dp->d_name); stats[count].name = xstrdup(dp->d_name);
stats[count].long_name = ls_file(dp->d_name, &st, 0); stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
count++; count++;
/* send up to 100 entries in one message */ /* send up to 100 entries in one message */
/* XXX check packet size instead */ /* XXX check packet size instead */

11
sftp.1
View File

@ -1,4 +1,4 @@
.\" $OpenBSD: sftp.1,v 1.80 2010/01/09 23:04:13 dtucker Exp $ .\" $OpenBSD: sftp.1,v 1.81 2010/01/13 01:40:16 djm Exp $
.\" .\"
.\" Copyright (c) 2001 Damien Miller. All rights reserved. .\" Copyright (c) 2001 Damien Miller. All rights reserved.
.\" .\"
@ -22,7 +22,7 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\" .\"
.Dd $Mdocdate: January 9 2010 $ .Dd $Mdocdate: January 13 2010 $
.Dt SFTP 1 .Dt SFTP 1
.Os .Os
.Sh NAME .Sh NAME
@ -393,7 +393,7 @@ to
.It Ic lpwd .It Ic lpwd
Print local working directory. Print local working directory.
.It Xo Ic ls .It Xo Ic ls
.Op Fl 1aflnrSt .Op Fl 1aflhnrSt
.Op Ar path .Op Ar path
.Xc .Xc
Display a remote directory listing of either Display a remote directory listing of either
@ -421,6 +421,11 @@ The default sort order is lexicographical.
.It Fl l .It Fl l
Display additional details including permissions Display additional details including permissions
and ownership information. and ownership information.
.It Fl h
When used with a long format option, use unit suffixes: Byte, Kilobyte,
Megabyte, Gigabyte, Terabyte, Petabyte, and Exabyte in order to reduce
the number of digits to four or fewer using powers of 2 for sizes (K=1024,
M=1048576, etc.).
.It Fl n .It Fl n
Produce a long listing with user and group information presented Produce a long listing with user and group information presented
numerically. numerically.

37
sftp.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: sftp.c,v 1.118 2010/01/09 11:13:02 dtucker Exp $ */ /* $OpenBSD: sftp.c,v 1.119 2010/01/13 01:40:16 djm Exp $ */
/* /*
* Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
* *
@ -110,16 +110,17 @@ extern char *__progname;
#define WHITESPACE " \t\r\n" #define WHITESPACE " \t\r\n"
/* ls flags */ /* ls flags */
#define LS_LONG_VIEW 0x01 /* Full view ala ls -l */ #define LS_LONG_VIEW 0x0001 /* Full view ala ls -l */
#define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */ #define LS_SHORT_VIEW 0x0002 /* Single row view ala ls -1 */
#define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */ #define LS_NUMERIC_VIEW 0x0004 /* Long view with numeric uid/gid */
#define LS_NAME_SORT 0x08 /* Sort by name (default) */ #define LS_NAME_SORT 0x0008 /* Sort by name (default) */
#define LS_TIME_SORT 0x10 /* Sort by mtime */ #define LS_TIME_SORT 0x0010 /* Sort by mtime */
#define LS_SIZE_SORT 0x20 /* Sort by file size */ #define LS_SIZE_SORT 0x0020 /* Sort by file size */
#define LS_REVERSE_SORT 0x40 /* Reverse sort order */ #define LS_REVERSE_SORT 0x0040 /* Reverse sort order */
#define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */ #define LS_SHOW_ALL 0x0080 /* Don't skip filenames starting with '.' */
#define LS_SI_UNITS 0x0100 /* Display sizes as K, M, G, etc. */
#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW) #define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT) #define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
/* Commands for interactive mode */ /* Commands for interactive mode */
@ -383,7 +384,7 @@ parse_ls_flags(char **argv, int argc, int *lflag)
opterr = 0; opterr = 0;
*lflag = LS_NAME_SORT; *lflag = LS_NAME_SORT;
while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) { while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
switch (ch) { switch (ch) {
case '1': case '1':
*lflag &= ~VIEW_FLAGS; *lflag &= ~VIEW_FLAGS;
@ -399,12 +400,15 @@ parse_ls_flags(char **argv, int argc, int *lflag)
case 'f': case 'f':
*lflag &= ~SORT_FLAGS; *lflag &= ~SORT_FLAGS;
break; break;
case 'h':
*lflag |= LS_SI_UNITS;
break;
case 'l': case 'l':
*lflag &= ~VIEW_FLAGS; *lflag &= ~LS_SHORT_VIEW;
*lflag |= LS_LONG_VIEW; *lflag |= LS_LONG_VIEW;
break; break;
case 'n': case 'n':
*lflag &= ~VIEW_FLAGS; *lflag &= ~LS_SHORT_VIEW;
*lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW; *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
break; break;
case 'r': case 'r':
@ -716,13 +720,14 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
xfree(tmp); xfree(tmp);
if (lflag & LS_LONG_VIEW) { if (lflag & LS_LONG_VIEW) {
if (lflag & LS_NUMERIC_VIEW) { if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
char *lname; char *lname;
struct stat sb; struct stat sb;
memset(&sb, 0, sizeof(sb)); memset(&sb, 0, sizeof(sb));
attrib_to_stat(&d[n]->a, &sb); attrib_to_stat(&d[n]->a, &sb);
lname = ls_file(fname, &sb, 1); lname = ls_file(fname, &sb, 1,
(lflag & LS_SI_UNITS));
printf("%s\n", lname); printf("%s\n", lname);
xfree(lname); xfree(lname);
} else } else
@ -824,7 +829,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
a = do_lstat(conn, g.gl_pathv[i], 1); a = do_lstat(conn, g.gl_pathv[i], 1);
if (a != NULL) if (a != NULL)
attrib_to_stat(a, &sb); attrib_to_stat(a, &sb);
lname = ls_file(fname, &sb, 1); lname = ls_file(fname, &sb, 1, (lflag & LS_SI_UNITS));
printf("%s\n", lname); printf("%s\n", lname);
xfree(lname); xfree(lname);
} else { } else {