ia64/xen-unstable
changeset 10913:153e69eae665
[TOOLS] Enhance xenstore-ls to optionally display permissions.
Also perform an ioctl to determine the current width of the terminal.
Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>
Also perform an ioctl to determine the current width of the terminal.
Signed-off-by: Michael LeMay <mdlemay@epoch.ncsc.mil>
author | kfraser@localhost.localdomain |
---|---|
date | Wed Aug 02 15:00:39 2006 +0100 (2006-08-02) |
parents | ee6014279103 |
children | 9a4a560d0a23 |
files | tools/xenstore/xsls.c |
line diff
1.1 --- a/tools/xenstore/xsls.c Wed Aug 02 14:59:22 2006 +0100 1.2 +++ b/tools/xenstore/xsls.c Wed Aug 02 15:00:39 2006 +0100 1.3 @@ -3,8 +3,19 @@ 1.4 #include <string.h> 1.5 #include <err.h> 1.6 #include <xs.h> 1.7 +#include <getopt.h> 1.8 +#include <unistd.h> 1.9 +#include <sys/ioctl.h> 1.10 1.11 -void print_dir(struct xs_handle *h, char *path, int cur_depth) 1.12 +static int max_width = 80; 1.13 +static int desired_width = 60; 1.14 + 1.15 +#define TAG " = \"...\"" 1.16 +#define TAG_LEN strlen(TAG) 1.17 + 1.18 +#define MIN(a, b) (((a) < (b))? (a) : (b)) 1.19 + 1.20 +void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms) 1.21 { 1.22 char **e; 1.23 char newpath[512], *val; 1.24 @@ -16,33 +27,103 @@ void print_dir(struct xs_handle *h, char 1.25 err(1, "xs_directory (%s)", path); 1.26 1.27 for (i = 0; i<num; i++) { 1.28 - int j; 1.29 - for (j=0; j<cur_depth; j++) printf(" "); 1.30 - printf("%s", e[i]); 1.31 + char buf[MAX_STRLEN(unsigned int)+1]; 1.32 + struct xs_permissions *perms; 1.33 + unsigned int nperms; 1.34 + int linewid; 1.35 + 1.36 + for (linewid=0; linewid<cur_depth; linewid++) putchar(' '); 1.37 + linewid += printf("%.*s", 1.38 + (int) (max_width - TAG_LEN - linewid), e[i]); 1.39 sprintf(newpath, "%s%s%s", path, 1.40 path[strlen(path)-1] == '/' ? "" : "/", 1.41 e[i]); 1.42 val = xs_read(h, XBT_NULL, newpath, &len); 1.43 - if (val == NULL) 1.44 + if (val == NULL) { 1.45 printf(":\n"); 1.46 - else if ((unsigned)len > (151 - strlen(e[i]))) 1.47 - printf(" = \"%.*s...\"\n", (int)(148 - strlen(e[i])), val); 1.48 - else 1.49 - printf(" = \"%s\"\n", val); 1.50 + } 1.51 + else { 1.52 + if (max_width < (linewid + len + TAG_LEN)) { 1.53 + printf(" = \"%.*s...\"", 1.54 + (int)(max_width - TAG_LEN - linewid), val); 1.55 + } 1.56 + else { 1.57 + linewid += printf(" = \"%s\"", val); 1.58 + if (show_perms) { 1.59 + putchar(' '); 1.60 + for (linewid++; 1.61 + linewid < MIN(desired_width, max_width); 1.62 + linewid++) 1.63 + putchar((linewid & 1)? '.' : ' '); 1.64 + } 1.65 + } 1.66 + } 1.67 free(val); 1.68 - print_dir(h, newpath, cur_depth+1); 1.69 + 1.70 + if (show_perms) { 1.71 + perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms); 1.72 + if (perms == NULL) { 1.73 + warn("\ncould not access permissions for %s", e[i]); 1.74 + } 1.75 + else { 1.76 + int i; 1.77 + fputs(" (", stdout); 1.78 + for (i = 0; i < nperms; i++) { 1.79 + if (i) 1.80 + putchar(','); 1.81 + xs_perm_to_string(perms+i, buf); 1.82 + fputs(buf, stdout); 1.83 + } 1.84 + putchar(')'); 1.85 + } 1.86 + } 1.87 + 1.88 + putchar('\n'); 1.89 + 1.90 + print_dir(h, newpath, cur_depth+1, show_perms); 1.91 } 1.92 free(e); 1.93 } 1.94 1.95 +void usage(int argc, char *argv[]) 1.96 +{ 1.97 + fprintf(stderr, "Usage: %s [-p] [path]\n", argv[0]); 1.98 +} 1.99 + 1.100 int main(int argc, char *argv[]) 1.101 { 1.102 + struct winsize ws; 1.103 + int ret; 1.104 + int c; 1.105 + int show_perm = 0; 1.106 + 1.107 struct xs_handle *xsh = xs_daemon_open(); 1.108 1.109 if (xsh == NULL) 1.110 err(1, "xs_daemon_open"); 1.111 1.112 - print_dir(xsh, argc == 1 ? "/" : argv[1], 0); 1.113 +#define PAD 2 1.114 + 1.115 + memset(&ws, 0, sizeof(ws)); 1.116 + ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); 1.117 + if (!ret) 1.118 + max_width = ws.ws_col - PAD; 1.119 + 1.120 + while (0 < (c = getopt(argc, argv, "p"))) { 1.121 + switch (c) { 1.122 + case 'p': 1.123 + show_perm = 1; 1.124 + max_width -= 16; 1.125 + break; 1.126 + case ':': 1.127 + case '?': 1.128 + default: 1.129 + usage(argc, argv); 1.130 + return 0; 1.131 + } 1.132 + } 1.133 + 1.134 + print_dir(xsh, (argc - optind) == 1 ? argv[optind] : "/", 0, show_perm); 1.135 1.136 return 0; 1.137 }