summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2010-01-13 22:44:06 +1100
committerDarren Tucker <dtucker@zip.com.au>2010-01-13 22:44:06 +1100
commit2901e2daebe3a0890209f31d05d5bb9338cbed5b (patch)
tree8165aef201520ede26777b48b3c9e777245e2616
parentdaaa4500519627abee7c6d1969bc33df6572c1e8 (diff)
- 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@
-rw-r--r--ChangeLog4
-rw-r--r--sftp-common.c19
-rw-r--r--sftp-common.h4
-rw-r--r--sftp-server.c4
-rw-r--r--sftp.111
-rw-r--r--sftp.c39
6 files changed, 52 insertions, 29 deletions
diff --git a/ChangeLog b/ChangeLog
index 7624812b1..d4210354e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -18,6 +18,10 @@
18 [canohost.c ssh-keysign.c sshconnect2.c] 18 [canohost.c ssh-keysign.c sshconnect2.c]
19 Make HostBased authentication work with a ProxyCommand. bz #1569, patch 19 Make HostBased authentication work with a ProxyCommand. bz #1569, patch
20 from imorgan at nas nasa gov, ok djm@ 20 from imorgan at nas nasa gov, ok djm@
21 - djm@cvs.openbsd.org 2010/01/13 01:40:16
22 [sftp.c sftp-server.c sftp.1 sftp-common.c sftp-common.h]
23 support '-h' (human-readable units) for sftp's ls command, just like
24 ls(1); ok dtucker@
21 25
2220100112 2620100112
23 - (dtucker) OpenBSD CVS Sync 27 - (dtucker) OpenBSD CVS Sync
diff --git a/sftp-common.c b/sftp-common.c
index 7ebadcc53..7393fc6a9 100644
--- a/sftp-common.c
+++ b/sftp-common.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sftp-common.c,v 1.20 2006/08/03 03:34:42 deraadt Exp $ */ 1/* $OpenBSD: sftp-common.c,v 1.21 2010/01/13 01:40:16 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001 Markus Friedl. All rights reserved.
4 * Copyright (c) 2001 Damien Miller. All rights reserved. 4 * Copyright (c) 2001 Damien Miller. All rights reserved.
@@ -36,6 +36,7 @@
36#include <string.h> 36#include <string.h>
37#include <time.h> 37#include <time.h>
38#include <stdarg.h> 38#include <stdarg.h>
39#include <util.h>
39 40
40#include "xmalloc.h" 41#include "xmalloc.h"
41#include "buffer.h" 42#include "buffer.h"
@@ -184,7 +185,7 @@ fx2txt(int status)
184 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh 185 * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh
185 */ 186 */
186char * 187char *
187ls_file(const char *name, const struct stat *st, int remote) 188ls_file(const char *name, const struct stat *st, int remote, int si_units)
188{ 189{
189 int ulen, glen, sz = 0; 190 int ulen, glen, sz = 0;
190 struct passwd *pw; 191 struct passwd *pw;
@@ -192,6 +193,7 @@ ls_file(const char *name, const struct stat *st, int remote)
192 struct tm *ltime = localtime(&st->st_mtime); 193 struct tm *ltime = localtime(&st->st_mtime);
193 char *user, *group; 194 char *user, *group;
194 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; 195 char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1];
196 char sbuf[FMT_SCALED_STRSIZE];
195 197
196 strmode(st->st_mode, mode); 198 strmode(st->st_mode, mode);
197 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) { 199 if (!remote && (pw = getpwuid(st->st_uid)) != NULL) {
@@ -216,8 +218,15 @@ ls_file(const char *name, const struct stat *st, int remote)
216 tbuf[0] = '\0'; 218 tbuf[0] = '\0';
217 ulen = MAX(strlen(user), 8); 219 ulen = MAX(strlen(user), 8);
218 glen = MAX(strlen(group), 8); 220 glen = MAX(strlen(group), 8);
219 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, 221 if (si_units) {
220 (u_int)st->st_nlink, ulen, user, glen, group, 222 fmt_scaled((long long)st->st_size, sbuf);
221 (unsigned long long)st->st_size, tbuf, name); 223 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode,
224 (u_int)st->st_nlink, ulen, user, glen, group,
225 sbuf, tbuf, name);
226 } else {
227 snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode,
228 (u_int)st->st_nlink, ulen, user, glen, group,
229 (unsigned long long)st->st_size, tbuf, name);
230 }
222 return xstrdup(buf); 231 return xstrdup(buf);
223} 232}
diff --git a/sftp-common.h b/sftp-common.h
index 9b5848462..9ed86c070 100644
--- a/sftp-common.h
+++ b/sftp-common.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: sftp-common.h,v 1.10 2006/08/03 03:34:42 deraadt Exp $ */ 1/* $OpenBSD: sftp-common.h,v 1.11 2010/01/13 01:40:16 djm Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001 Markus Friedl. All rights reserved. 4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
@@ -46,6 +46,6 @@ void stat_to_attrib(const struct stat *, Attrib *);
46void attrib_to_stat(const Attrib *, struct stat *); 46void attrib_to_stat(const Attrib *, struct stat *);
47Attrib *decode_attrib(Buffer *); 47Attrib *decode_attrib(Buffer *);
48void encode_attrib(Buffer *, const Attrib *); 48void encode_attrib(Buffer *, const Attrib *);
49char *ls_file(const char *, const struct stat *, int); 49char *ls_file(const char *, const struct stat *, int, int);
50 50
51const char *fx2txt(int); 51const char *fx2txt(int);
diff --git a/sftp-server.c b/sftp-server.c
index ab9391cfd..a98ac2b6d 100644
--- a/sftp-server.c
+++ b/sftp-server.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sftp-server.c,v 1.90 2010/01/09 00:20:26 djm Exp $ */ 1/* $OpenBSD: sftp-server.c,v 1.91 2010/01/13 01:40:16 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000-2004 Markus Friedl. All rights reserved.
4 * 4 *
@@ -940,7 +940,7 @@ process_readdir(void)
940 continue; 940 continue;
941 stat_to_attrib(&st, &(stats[count].attrib)); 941 stat_to_attrib(&st, &(stats[count].attrib));
942 stats[count].name = xstrdup(dp->d_name); 942 stats[count].name = xstrdup(dp->d_name);
943 stats[count].long_name = ls_file(dp->d_name, &st, 0); 943 stats[count].long_name = ls_file(dp->d_name, &st, 0, 0);
944 count++; 944 count++;
945 /* send up to 100 entries in one message */ 945 /* send up to 100 entries in one message */
946 /* XXX check packet size instead */ 946 /* XXX check packet size instead */
diff --git a/sftp.1 b/sftp.1
index 3ec7a0234..f6371cf54 100644
--- a/sftp.1
+++ b/sftp.1
@@ -1,4 +1,4 @@
1.\" $OpenBSD: sftp.1,v 1.80 2010/01/09 23:04:13 dtucker Exp $ 1.\" $OpenBSD: sftp.1,v 1.81 2010/01/13 01:40:16 djm Exp $
2.\" 2.\"
3.\" Copyright (c) 2001 Damien Miller. All rights reserved. 3.\" Copyright (c) 2001 Damien Miller. All rights reserved.
4.\" 4.\"
@@ -22,7 +22,7 @@
22.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24.\" 24.\"
25.Dd $Mdocdate: January 9 2010 $ 25.Dd $Mdocdate: January 13 2010 $
26.Dt SFTP 1 26.Dt SFTP 1
27.Os 27.Os
28.Sh NAME 28.Sh NAME
@@ -393,7 +393,7 @@ to
393.It Ic lpwd 393.It Ic lpwd
394Print local working directory. 394Print local working directory.
395.It Xo Ic ls 395.It Xo Ic ls
396.Op Fl 1aflnrSt 396.Op Fl 1aflhnrSt
397.Op Ar path 397.Op Ar path
398.Xc 398.Xc
399Display a remote directory listing of either 399Display a remote directory listing of either
@@ -421,6 +421,11 @@ The default sort order is lexicographical.
421.It Fl l 421.It Fl l
422Display additional details including permissions 422Display additional details including permissions
423and ownership information. 423and ownership information.
424.It Fl h
425When used with a long format option, use unit suffixes: Byte, Kilobyte,
426Megabyte, Gigabyte, Terabyte, Petabyte, and Exabyte in order to reduce
427the number of digits to four or fewer using powers of 2 for sizes (K=1024,
428M=1048576, etc.).
424.It Fl n 429.It Fl n
425Produce a long listing with user and group information presented 430Produce a long listing with user and group information presented
426numerically. 431numerically.
diff --git a/sftp.c b/sftp.c
index 78f8ca178..16f84987c 100644
--- a/sftp.c
+++ b/sftp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sftp.c,v 1.118 2010/01/09 11:13:02 dtucker Exp $ */ 1/* $OpenBSD: sftp.c,v 1.119 2010/01/13 01:40:16 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org> 3 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
4 * 4 *
@@ -110,16 +110,17 @@ extern char *__progname;
110#define WHITESPACE " \t\r\n" 110#define WHITESPACE " \t\r\n"
111 111
112/* ls flags */ 112/* ls flags */
113#define LS_LONG_VIEW 0x01 /* Full view ala ls -l */ 113#define LS_LONG_VIEW 0x0001 /* Full view ala ls -l */
114#define LS_SHORT_VIEW 0x02 /* Single row view ala ls -1 */ 114#define LS_SHORT_VIEW 0x0002 /* Single row view ala ls -1 */
115#define LS_NUMERIC_VIEW 0x04 /* Long view with numeric uid/gid */ 115#define LS_NUMERIC_VIEW 0x0004 /* Long view with numeric uid/gid */
116#define LS_NAME_SORT 0x08 /* Sort by name (default) */ 116#define LS_NAME_SORT 0x0008 /* Sort by name (default) */
117#define LS_TIME_SORT 0x10 /* Sort by mtime */ 117#define LS_TIME_SORT 0x0010 /* Sort by mtime */
118#define LS_SIZE_SORT 0x20 /* Sort by file size */ 118#define LS_SIZE_SORT 0x0020 /* Sort by file size */
119#define LS_REVERSE_SORT 0x40 /* Reverse sort order */ 119#define LS_REVERSE_SORT 0x0040 /* Reverse sort order */
120#define LS_SHOW_ALL 0x80 /* Don't skip filenames starting with '.' */ 120#define LS_SHOW_ALL 0x0080 /* Don't skip filenames starting with '.' */
121 121#define LS_SI_UNITS 0x0100 /* Display sizes as K, M, G, etc. */
122#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW) 122
123#define VIEW_FLAGS (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
123#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT) 124#define SORT_FLAGS (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
124 125
125/* Commands for interactive mode */ 126/* Commands for interactive mode */
@@ -383,7 +384,7 @@ parse_ls_flags(char **argv, int argc, int *lflag)
383 opterr = 0; 384 opterr = 0;
384 385
385 *lflag = LS_NAME_SORT; 386 *lflag = LS_NAME_SORT;
386 while ((ch = getopt(argc, argv, "1Saflnrt")) != -1) { 387 while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
387 switch (ch) { 388 switch (ch) {
388 case '1': 389 case '1':
389 *lflag &= ~VIEW_FLAGS; 390 *lflag &= ~VIEW_FLAGS;
@@ -399,12 +400,15 @@ parse_ls_flags(char **argv, int argc, int *lflag)
399 case 'f': 400 case 'f':
400 *lflag &= ~SORT_FLAGS; 401 *lflag &= ~SORT_FLAGS;
401 break; 402 break;
403 case 'h':
404 *lflag |= LS_SI_UNITS;
405 break;
402 case 'l': 406 case 'l':
403 *lflag &= ~VIEW_FLAGS; 407 *lflag &= ~LS_SHORT_VIEW;
404 *lflag |= LS_LONG_VIEW; 408 *lflag |= LS_LONG_VIEW;
405 break; 409 break;
406 case 'n': 410 case 'n':
407 *lflag &= ~VIEW_FLAGS; 411 *lflag &= ~LS_SHORT_VIEW;
408 *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW; 412 *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
409 break; 413 break;
410 case 'r': 414 case 'r':
@@ -716,13 +720,14 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
716 xfree(tmp); 720 xfree(tmp);
717 721
718 if (lflag & LS_LONG_VIEW) { 722 if (lflag & LS_LONG_VIEW) {
719 if (lflag & LS_NUMERIC_VIEW) { 723 if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
720 char *lname; 724 char *lname;
721 struct stat sb; 725 struct stat sb;
722 726
723 memset(&sb, 0, sizeof(sb)); 727 memset(&sb, 0, sizeof(sb));
724 attrib_to_stat(&d[n]->a, &sb); 728 attrib_to_stat(&d[n]->a, &sb);
725 lname = ls_file(fname, &sb, 1); 729 lname = ls_file(fname, &sb, 1,
730 (lflag & LS_SI_UNITS));
726 printf("%s\n", lname); 731 printf("%s\n", lname);
727 xfree(lname); 732 xfree(lname);
728 } else 733 } else
@@ -824,7 +829,7 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
824 a = do_lstat(conn, g.gl_pathv[i], 1); 829 a = do_lstat(conn, g.gl_pathv[i], 1);
825 if (a != NULL) 830 if (a != NULL)
826 attrib_to_stat(a, &sb); 831 attrib_to_stat(a, &sb);
827 lname = ls_file(fname, &sb, 1); 832 lname = ls_file(fname, &sb, 1, (lflag & LS_SI_UNITS));
828 printf("%s\n", lname); 833 printf("%s\n", lname);
829 xfree(lname); 834 xfree(lname);
830 } else { 835 } else {