diff options
author | mouring <mouring@cvs.openbsd.org> | 2003-05-15 03:44:01 +0000 |
---|---|---|
committer | mouring <mouring@cvs.openbsd.org> | 2003-05-15 03:44:01 +0000 |
commit | 7668d9c3a3d1bab68ad4f8de9893bc4da187e22d (patch) | |
tree | f7779042d999fdad3534a8a1106e589bde70f982 /usr.bin | |
parent | 5377a4d07a4d40870e8300ae08fe2fff49b2ac6b (diff) |
Teach ls how to display multiple column display and allow users to return
to single column format via 'ls -1'. OK @djm
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/ssh/sftp-int.c | 79 | ||||
-rw-r--r-- | usr.bin/ssh/sftp.c | 4 |
2 files changed, 65 insertions, 18 deletions
diff --git a/usr.bin/ssh/sftp-int.c b/usr.bin/ssh/sftp-int.c index fe43a861f27..50bfa678c99 100644 --- a/usr.bin/ssh/sftp-int.c +++ b/usr.bin/ssh/sftp-int.c @@ -25,7 +25,7 @@ /* XXX: recursive operations */ #include "includes.h" -RCSID("$OpenBSD: sftp-int.c,v 1.59 2003/05/15 03:39:07 mouring Exp $"); +RCSID("$OpenBSD: sftp-int.c,v 1.60 2003/05/15 03:43:59 mouring Exp $"); #include <glob.h> @@ -55,6 +55,10 @@ int showprogress = 1; /* Seperators for interactive commands */ #define WHITESPACE " \t\r\n" +/* Define what type of ls view (0 - multi-column) */ +#define LONG_VIEW 1 /* Full view ala ls -l */ +#define SHORT_VIEW 2 /* Single row view ala ls -1 */ + /* Commands for interactive mode */ #define I_CHDIR 1 #define I_CHGRP 2 @@ -309,7 +313,10 @@ parse_ls_flags(const char **cpp, int *lflag) for(; strchr(WHITESPACE, *cp) == NULL; cp++) { switch (*cp) { case 'l': - *lflag = 1; + *lflag = LONG_VIEW; + break; + case '1': + *lflag = SHORT_VIEW; break; default: error("Invalid flag -%c", *cp); @@ -560,15 +567,26 @@ sdirent_comp(const void *aa, const void *bb) static int do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) { - int n; + int n, c = 1, colspace = 0, columns = 1; SFTP_DIRENT **d; if ((n = do_readdir(conn, path, &d)) != 0) return (n); - /* Count entries for sort */ - for (n = 0; d[n] != NULL; n++) - ; + if (!(lflag & SHORT_VIEW)) { + int m = 0, width = 80; + struct winsize ws; + + /* Count entries for sort and find longest filename */ + for (n = 0; d[n] != NULL; n++) + m = MAX(m, strlen(d[n]->filename)); + + if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) + width = ws.ws_col; + + columns = width / (m + 2); + colspace = width / columns; + } qsort(d, n, sizeof(*d), sdirent_comp); @@ -579,7 +597,7 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) fname = path_strip(tmp, strip_path); xfree(tmp); - if (lflag) { + if (lflag & LONG_VIEW) { char *lname; struct stat sb; @@ -589,13 +607,20 @@ do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag) printf("%s\n", lname); xfree(lname); } else { - /* XXX - multicolumn display would be nice here */ - printf("%s\n", fname); + printf("%-*s", colspace, fname); + if (c >= columns) { + printf("\n"); + c = 1; + } else + c++; } xfree(fname); } + if (!(lflag & LONG_VIEW) && (c != 1)) + printf("\n"); + free_sftp_dirents(d); return (0); } @@ -606,9 +631,8 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, int lflag) { glob_t g; - int i; + int i, c = 1, colspace = 0, columns = 1; Attrib *a; - struct stat sb; memset(&g, 0, sizeof(g)); @@ -635,12 +659,30 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, } } + if (!(lflag & SHORT_VIEW)) { + int m = 0, width = 80; + struct winsize ws; + + /* Count entries for sort and find longest filename */ + for (i = 0; g.gl_pathv[i]; i++) + m = MAX(m, strlen(g.gl_pathv[i])); + + if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1) + width = ws.ws_col; + + columns = width / (m + 2); + colspace = width / columns; + } + for (i = 0; g.gl_pathv[i]; i++) { - char *fname, *lname; + char *fname; fname = path_strip(g.gl_pathv[i], strip_path); - if (lflag) { + if (lflag & LONG_VIEW) { + char *lname; + struct stat sb; + /* * XXX: this is slow - 1 roundtrip per path * A solution to this is to fork glob() and @@ -656,12 +698,19 @@ do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path, printf("%s\n", lname); xfree(lname); } else { - /* XXX - multicolumn display would be nice here */ - printf("%s\n", fname); + printf("%-*s", colspace, fname); + if (c >= columns) { + printf("\n"); + c = 1; + } else + c++; } xfree(fname); } + if (!(lflag & LONG_VIEW) && (c != 1)) + printf("\n"); + if (g.gl_pathc) globfree(&g); diff --git a/usr.bin/ssh/sftp.c b/usr.bin/ssh/sftp.c index 8448cd6be46..6060a916911 100644 --- a/usr.bin/ssh/sftp.c +++ b/usr.bin/ssh/sftp.c @@ -24,9 +24,7 @@ #include "includes.h" -RCSID("$OpenBSD: sftp.c,v 1.34 2003/01/10 08:19:07 fgsch Exp $"); - -/* XXX: short-form remote directory listings (like 'ls -C') */ +RCSID("$OpenBSD: sftp.c,v 1.35 2003/05/15 03:44:00 mouring Exp $"); #include "buffer.h" #include "xmalloc.h" |