debuggers.hg

view tools/xenstore/xsls.c @ 10942: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>
author kfraser@localhost.localdomain
date Wed Aug 02 15:00:39 2006 +0100 (2006-08-02)
parents 991ccc24bf2e
children 1e6d52d06fa4
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <err.h>
5 #include <xs.h>
6 #include <getopt.h>
7 #include <unistd.h>
8 #include <sys/ioctl.h>
10 static int max_width = 80;
11 static int desired_width = 60;
13 #define TAG " = \"...\""
14 #define TAG_LEN strlen(TAG)
16 #define MIN(a, b) (((a) < (b))? (a) : (b))
18 void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
19 {
20 char **e;
21 char newpath[512], *val;
22 int i;
23 unsigned int num, len;
25 e = xs_directory(h, XBT_NULL, path, &num);
26 if (e == NULL)
27 err(1, "xs_directory (%s)", path);
29 for (i = 0; i<num; i++) {
30 char buf[MAX_STRLEN(unsigned int)+1];
31 struct xs_permissions *perms;
32 unsigned int nperms;
33 int linewid;
35 for (linewid=0; linewid<cur_depth; linewid++) putchar(' ');
36 linewid += printf("%.*s",
37 (int) (max_width - TAG_LEN - linewid), e[i]);
38 sprintf(newpath, "%s%s%s", path,
39 path[strlen(path)-1] == '/' ? "" : "/",
40 e[i]);
41 val = xs_read(h, XBT_NULL, newpath, &len);
42 if (val == NULL) {
43 printf(":\n");
44 }
45 else {
46 if (max_width < (linewid + len + TAG_LEN)) {
47 printf(" = \"%.*s...\"",
48 (int)(max_width - TAG_LEN - linewid), val);
49 }
50 else {
51 linewid += printf(" = \"%s\"", val);
52 if (show_perms) {
53 putchar(' ');
54 for (linewid++;
55 linewid < MIN(desired_width, max_width);
56 linewid++)
57 putchar((linewid & 1)? '.' : ' ');
58 }
59 }
60 }
61 free(val);
63 if (show_perms) {
64 perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms);
65 if (perms == NULL) {
66 warn("\ncould not access permissions for %s", e[i]);
67 }
68 else {
69 int i;
70 fputs(" (", stdout);
71 for (i = 0; i < nperms; i++) {
72 if (i)
73 putchar(',');
74 xs_perm_to_string(perms+i, buf);
75 fputs(buf, stdout);
76 }
77 putchar(')');
78 }
79 }
81 putchar('\n');
83 print_dir(h, newpath, cur_depth+1, show_perms);
84 }
85 free(e);
86 }
88 void usage(int argc, char *argv[])
89 {
90 fprintf(stderr, "Usage: %s [-p] [path]\n", argv[0]);
91 }
93 int main(int argc, char *argv[])
94 {
95 struct winsize ws;
96 int ret;
97 int c;
98 int show_perm = 0;
100 struct xs_handle *xsh = xs_daemon_open();
102 if (xsh == NULL)
103 err(1, "xs_daemon_open");
105 #define PAD 2
107 memset(&ws, 0, sizeof(ws));
108 ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
109 if (!ret)
110 max_width = ws.ws_col - PAD;
112 while (0 < (c = getopt(argc, argv, "p"))) {
113 switch (c) {
114 case 'p':
115 show_perm = 1;
116 max_width -= 16;
117 break;
118 case ':':
119 case '?':
120 default:
121 usage(argc, argv);
122 return 0;
123 }
124 }
126 print_dir(xsh, (argc - optind) == 1 ? argv[optind] : "/", 0, show_perm);
128 return 0;
129 }