ia64/xen-unstable

view tools/xenstore/xsls.c @ 12333:9b65a87f140f

[XENSTORE] Fix xenstore-ls program to connect via kernel xenbus
connection by default, like all other client utils.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Nov 09 13:25:47 2006 +0000 (2006-11-09)
parents 1e6d52d06fa4
children eae9dc5e7898
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>
9 #include <termios.h>
11 static int max_width = 80;
12 static int desired_width = 60;
14 #define TAG " = \"...\""
15 #define TAG_LEN strlen(TAG)
17 #define MIN(a, b) (((a) < (b))? (a) : (b))
19 void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
20 {
21 char **e;
22 char newpath[512], *val;
23 int i;
24 unsigned int num, len;
26 e = xs_directory(h, XBT_NULL, path, &num);
27 if (e == NULL)
28 err(1, "xs_directory (%s)", path);
30 for (i = 0; i<num; i++) {
31 char buf[MAX_STRLEN(unsigned int)+1];
32 struct xs_permissions *perms;
33 unsigned int nperms;
34 int linewid;
36 for (linewid=0; linewid<cur_depth; linewid++) putchar(' ');
37 linewid += printf("%.*s",
38 (int) (max_width - TAG_LEN - linewid), e[i]);
39 sprintf(newpath, "%s%s%s", path,
40 path[strlen(path)-1] == '/' ? "" : "/",
41 e[i]);
42 val = xs_read(h, XBT_NULL, newpath, &len);
43 if (val == NULL) {
44 printf(":\n");
45 }
46 else {
47 if (max_width < (linewid + len + TAG_LEN)) {
48 printf(" = \"%.*s...\"",
49 (int)(max_width - TAG_LEN - linewid), val);
50 }
51 else {
52 linewid += printf(" = \"%s\"", val);
53 if (show_perms) {
54 putchar(' ');
55 for (linewid++;
56 linewid < MIN(desired_width, max_width);
57 linewid++)
58 putchar((linewid & 1)? '.' : ' ');
59 }
60 }
61 }
62 free(val);
64 if (show_perms) {
65 perms = xs_get_permissions(h, XBT_NULL, newpath, &nperms);
66 if (perms == NULL) {
67 warn("\ncould not access permissions for %s", e[i]);
68 }
69 else {
70 int i;
71 fputs(" (", stdout);
72 for (i = 0; i < nperms; i++) {
73 if (i)
74 putchar(',');
75 xs_perm_to_string(perms+i, buf);
76 fputs(buf, stdout);
77 }
78 putchar(')');
79 }
80 }
82 putchar('\n');
84 print_dir(h, newpath, cur_depth+1, show_perms);
85 }
86 free(e);
87 }
89 void usage(int argc, char *argv[])
90 {
91 fprintf(stderr, "Usage: %s [-p] [path]\n", argv[0]);
92 }
94 int main(int argc, char *argv[])
95 {
96 struct winsize ws;
97 int ret, c, socket = 0, show_perm = 0;
98 struct xs_handle *xsh;
100 #define PAD 2
102 memset(&ws, 0, sizeof(ws));
103 ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
104 if (!ret)
105 max_width = ws.ws_col - PAD;
107 while (0 < (c = getopt(argc, argv, "ps"))) {
108 switch (c) {
109 case 'p':
110 show_perm = 1;
111 max_width -= 16;
112 break;
113 case 's':
114 socket = 1;
115 break;
116 case ':':
117 case '?':
118 default:
119 usage(argc, argv);
120 return 0;
121 }
122 }
124 xsh = socket ? xs_daemon_open() : xs_domain_open();
125 if (xsh == NULL)
126 err(1, socket ? "xs_daemon_open" : "xs_domain_open");
128 print_dir(xsh, (argc - optind) == 1 ? argv[optind] : "/", 0, show_perm);
130 return 0;
131 }