ia64/xen-unstable

changeset 15576:eae9dc5e7898

[xenstore] adds a -w (wide) flag to xenstore-ls to support seeing full
contents of xenstore entries. There is a bit of code cleanup as well
(snprintf vs. sprintf), one formatting fix, and comments).

There is no change to the behavior of xenstore-ls without -w.

Signed-off-by: John Zulauf <john.zulauf@intel.com>
author kfraser@localhost.localdomain
date Wed Jul 11 10:52:19 2007 +0100 (2007-07-11)
parents 73b6733e4bb1
children 2099e4af5aed
files tools/xenstore/xsls.c
line diff
     1.1 --- a/tools/xenstore/xsls.c	Wed Jul 11 10:49:43 2007 +0100
     1.2 +++ b/tools/xenstore/xsls.c	Wed Jul 11 10:52:19 2007 +0100
     1.3 @@ -8,6 +8,7 @@
     1.4  #include <sys/ioctl.h>
     1.5  #include <termios.h>
     1.6  
     1.7 +#define STRING_MAX PATH_MAX
     1.8  static int max_width = 80;
     1.9  static int desired_width = 60;
    1.10  
    1.11 @@ -19,7 +20,8 @@ static int desired_width = 60;
    1.12  void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
    1.13  {
    1.14      char **e;
    1.15 -    char newpath[512], *val;
    1.16 +    char newpath[STRING_MAX], *val;
    1.17 +    int newpath_len;
    1.18      int i;
    1.19      unsigned int num, len;
    1.20  
    1.21 @@ -33,13 +35,26 @@ void print_dir(struct xs_handle *h, char
    1.22          unsigned int nperms;
    1.23          int linewid;
    1.24  
    1.25 -        for (linewid=0; linewid<cur_depth; linewid++) putchar(' ');
    1.26 +        /* Print indent and path basename */
    1.27 +        for (linewid=0; linewid<cur_depth; linewid++) {
    1.28 +            putchar(' ');
    1.29 +        }
    1.30          linewid += printf("%.*s",
    1.31                            (int) (max_width - TAG_LEN - linewid), e[i]);
    1.32 -        sprintf(newpath, "%s%s%s", path, 
    1.33 +
    1.34 +        /* Compose fullpath and fetch value */
    1.35 +        newpath_len = snprintf(newpath, sizeof(newpath), "%s%s%s", path, 
    1.36                  path[strlen(path)-1] == '/' ? "" : "/", 
    1.37                  e[i]);
    1.38 -        val = xs_read(h, XBT_NULL, newpath, &len);
    1.39 +        if ( newpath_len < sizeof(newpath) ) {
    1.40 +            val = xs_read(h, XBT_NULL, newpath, &len);
    1.41 +        }
    1.42 +        else {
    1.43 +            /* Path was truncated and thus invalid */
    1.44 +            val = NULL;
    1.45 +        }
    1.46 +
    1.47 +        /* Print value */
    1.48          if (val == NULL) {
    1.49              printf(":\n");
    1.50          }
    1.51 @@ -88,7 +103,7 @@ void print_dir(struct xs_handle *h, char
    1.52  
    1.53  void usage(int argc, char *argv[])
    1.54  {
    1.55 -    fprintf(stderr, "Usage: %s [-p] [path]\n", argv[0]);
    1.56 +    fprintf(stderr, "Usage: %s [-w] [-p] [path]\n", argv[0]);
    1.57  }
    1.58  
    1.59  int main(int argc, char *argv[])
    1.60 @@ -104,11 +119,14 @@ int main(int argc, char *argv[])
    1.61      if (!ret)
    1.62          max_width = ws.ws_col - PAD;
    1.63  
    1.64 -    while (0 < (c = getopt(argc, argv, "ps"))) {
    1.65 +    while (0 < (c = getopt(argc, argv, "psw"))) {
    1.66          switch (c) {
    1.67 +        case 'w':
    1.68 +            max_width= STRING_MAX - PAD;
    1.69 +            desired_width = 0;
    1.70 +            break;
    1.71          case 'p':
    1.72              show_perm = 1;
    1.73 -            max_width -= 16;
    1.74              break;
    1.75          case 's':
    1.76              socket = 1;
    1.77 @@ -121,6 +139,11 @@ int main(int argc, char *argv[])
    1.78          }
    1.79      }
    1.80  
    1.81 +    /* Adjust the width here to avoid argument order dependency */
    1.82 +    if ( show_perm ) {
    1.83 +        max_width -= 16;
    1.84 +    }
    1.85 +
    1.86      xsh = socket ? xs_daemon_open() : xs_domain_open();
    1.87      if (xsh == NULL)
    1.88          err(1, socket ? "xs_daemon_open" : "xs_domain_open");