summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Miller <djm@cvs.openbsd.org>2010-01-13 01:40:17 +0000
committerDamien Miller <djm@cvs.openbsd.org>2010-01-13 01:40:17 +0000
commitca27f3b53d7f8a09583536ec7ed7dbccd2921278 (patch)
treedc9dc6d6d3de7ae064e1790abd2c55b3bef3810f
parent64631418c76f9f64f0669c6a1eb8af1e13dcd1fb (diff)
support '-h' (human-readable units) for sftp's ls command, just like
ls(1); ok dtucker@
-rw-r--r--usr.bin/ssh/sftp-common.c19
-rw-r--r--usr.bin/ssh/sftp-common.h4
-rw-r--r--usr.bin/ssh/sftp-server.c4
-rw-r--r--usr.bin/ssh/sftp-server/Makefile4
-rw-r--r--usr.bin/ssh/sftp.111
-rw-r--r--usr.bin/ssh/sftp.c39
6 files changed, 51 insertions, 30 deletions
diff --git a/usr.bin/ssh/sftp-common.c b/usr.bin/ssh/sftp-common.c
index a24649c11c4..e7c2cab944a 100644
--- a/usr.bin/ssh/sftp-common.c
+++ b/usr.bin/ssh/sftp-common.c
@@ -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 Damien Miller. All rights reserved.
@@ -34,6 +34,7 @@
#include <string.h>
#include <time.h>
#include <stdarg.h>
+#include <util.h>
#include "xmalloc.h"
#include "buffer.h"
@@ -182,7 +183,7 @@ fx2txt(int status)
* drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
*/
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;
struct passwd *pw;
@@ -190,6 +191,7 @@ ls_file(const char *name, const struct stat *st, int remote)
struct tm *ltime = localtime(&st->st_mtime);
char *user, *group;
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);
if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
@@ -214,8 +216,15 @@ ls_file(const char *name, const struct stat *st, int remote)
tbuf[0] = '\0';
ulen = MAX(strlen(user), 8);
glen = MAX(strlen(group), 8);
- 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);
+ if (si_units) {
+ fmt_scaled((long long)st->st_size, sbuf);
+ 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);
}
diff --git a/usr.bin/ssh/sftp-common.h b/usr.bin/ssh/sftp-common.h
index 9b5848462a2..9ed86c070dd 100644
--- a/usr.bin/ssh/sftp-common.h
+++ b/usr.bin/ssh/sftp-common.h
@@ -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.
@@ -46,6 +46,6 @@ void stat_to_attrib(const struct stat *, Attrib *);
void attrib_to_stat(const Attrib *, struct stat *);
Attrib *decode_attrib(Buffer *);
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);
diff --git a/usr.bin/ssh/sftp-server.c b/usr.bin/ssh/sftp-server.c
index 06171e1f3ce..216348c2363 100644
--- a/usr.bin/ssh/sftp-server.c
+++ b/usr.bin/ssh/sftp-server.c
@@ -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.
*
@@ -919,7 +919,7 @@ process_readdir(void)
continue;
stat_to_attrib(&st, &(stats[count].attrib));
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++;
/* send up to 100 entries in one message */
/* XXX check packet size instead */
diff --git a/usr.bin/ssh/sftp-server/Makefile b/usr.bin/ssh/sftp-server/Makefile
index c923d4e84e4..84c3777f31e 100644
--- a/usr.bin/ssh/sftp-server/Makefile
+++ b/usr.bin/ssh/sftp-server/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.7 2008/02/04 21:53:00 markus Exp $
+# $OpenBSD: Makefile,v 1.8 2010/01/13 01:40:16 djm Exp $
.PATH: ${.CURDIR}/..
@@ -12,4 +12,6 @@ MAN= sftp-server.8
SRCS= sftp-server.c sftp-common.c sftp-server-main.c
+LDADD+= -lutil
+
.include <bsd.prog.mk>
diff --git a/usr.bin/ssh/sftp.1 b/usr.bin/ssh/sftp.1
index 3ec7a02347b..f6371cf549f 100644
--- a/usr.bin/ssh/sftp.1
+++ b/usr.bin/ssh/sftp.1
@@ -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.
.\"
@@ -22,7 +22,7 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd $Mdocdate: January 9 2010 $
+.Dd $Mdocdate: January 13 2010 $
.Dt SFTP 1
.Os
.Sh NAME
@@ -393,7 +393,7 @@ to
.It Ic lpwd
Print local working directory.
.It Xo Ic ls
-.Op Fl 1aflnrSt
+.Op Fl 1aflhnrSt
.Op Ar path
.Xc
Display a remote directory listing of either
@@ -421,6 +421,11 @@ The default sort order is lexicographical.
.It Fl l
Display additional details including permissions
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
Produce a long listing with user and group information presented
numerically.
diff --git a/usr.bin/ssh/sftp.c b/usr.bin/ssh/sftp.c
index c50516fc857..aac92aefc7f 100644
--- a/usr.bin/ssh/sftp.c
+++ b/usr.bin/ssh/sftp.c
@@ -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>
*
@@ -87,16 +87,17 @@ int remote_glob(struct sftp_conn *, const char *, int,
#define WHITESPACE " \t\r\n"
/* ls flags */
-#define LS_LONG_VIEW 0x01 /* Full view ala ls -l */
-#define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */
-#define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */
-#define LS_NAME_SORT 0x08 /* Sort by name (default) */
-#define LS_TIME_SORT 0x10 /* Sort by mtime */
-#define LS_SIZE_SORT 0x20 /* Sort by file size */
-#define LS_REVERSE_SORT 0x40 /* Reverse sort order */
-#define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */
-
-#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW)
+#define LS_LONG_VIEW 0x0001 /* Full view ala ls -l */
+#define LS_SHORT_VIEW 0x0002 /* Single row view ala ls -1 */
+#define LS_NUMERIC_VIEW 0x0004 /* Long view with numeric uid/gid */
+#define LS_NAME_SORT 0x0008 /* Sort by name (default) */
+#define LS_TIME_SORT 0x0010 /* Sort by mtime */
+#define LS_SIZE_SORT 0x0020 /* Sort by file size */
+#define LS_REVERSE_SORT 0x0040 /* Reverse sort order */
+#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|LS_SI_UNITS)
#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
/* Commands for interactive mode */
@@ -360,7 +361,7 @@ parse_ls_flags(char **argv, int argc, int *lflag)
opterr = 0;
*lflag = LS_NAME_SORT;
- while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) {
+ while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
switch (ch) {
case '1':
*lflag &= ~VIEW_FLAGS;
@@ -376,12 +377,15 @@ parse_ls_flags(char **argv, int argc, int *lflag)
case 'f':
*lflag &= ~SORT_FLAGS;
break;
+ case 'h':
+ *lflag |= LS_SI_UNITS;
+ break;
case 'l':
- *lflag &= ~VIEW_FLAGS;
+ *lflag &= ~LS_SHORT_VIEW;
*lflag |= LS_LONG_VIEW;
break;
case 'n':
- *lflag &= ~VIEW_FLAGS;
+ *lflag &= ~LS_SHORT_VIEW;
*lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
break;
case 'r':
@@ -693,13 +697,14 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
xfree(tmp);
if (lflag & LS_LONG_VIEW) {
- if (lflag & LS_NUMERIC_VIEW) {
+ if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
char *lname;
struct stat sb;
memset(&sb, 0, sizeof(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);
xfree(lname);
} else
@@ -801,7 +806,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
a = do_lstat(conn, g.gl_pathv[i], 1);
if (a != NULL)
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);
xfree(lname);
} else {