[systemd-devel] [PATCH 2/2] Add a 'b' option to cgtop, equivalent to the same option in top.

David Strauss david at davidstrauss.net
Wed Jul 25 16:35:04 PDT 2012


Actually, the "continue" could probably get rolled into the else
clause in the main loop.

On Wed, Jul 25, 2012 at 4:33 PM,  <david at davidstrauss.net> wrote:
> From: David Strauss <david at davidstrauss.net>
>
> ---
>  src/cgtop/cgtop.c | 30 +++++++++++++++++++++++-------
>  1 file changed, 23 insertions(+), 7 deletions(-)
>
> diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
> index 70570a8..b4454c54 100644
> --- a/src/cgtop/cgtop.c
> +++ b/src/cgtop/cgtop.c
> @@ -66,6 +66,8 @@ static enum {
>          ORDER_IO
>  } arg_order = ORDER_CPU;
>
> +bool arg_batch = false;
> +
>  static void group_free(Group *g) {
>          assert(g);
>
> @@ -507,6 +509,7 @@ static void help(void) {
>                 "  -i                  Order by IO load\n"
>                 "  -d --delay=DELAY    Specify delay\n"
>                 "  -n --iterations=N   Run for N iterations before exiting\n"
> +               "  -b --batch          Run in batch mode, accepting no input\n"
>                 "     --depth=DEPTH    Maximum traversal depth (default: 2)\n",
>                 program_invocation_short_name);
>  }
> @@ -521,6 +524,7 @@ static int parse_argv(int argc, char *argv[]) {
>                  { "help",       no_argument,       NULL, 'h'       },
>                  { "delay",      required_argument, NULL, 'd'       },
>                  { "iterations", required_argument, NULL, 'n'       },
> +                { "batch",      no_argument,       NULL, 'b'       },
>                  { "depth",      required_argument, NULL, ARG_DEPTH },
>                  { NULL,         0,                 NULL, 0         }
>          };
> @@ -531,7 +535,7 @@ static int parse_argv(int argc, char *argv[]) {
>          assert(argc >= 1);
>          assert(argv);
>
> -        while ((c = getopt_long(argc, argv, "hptcmin:d:", options, NULL)) >= 0) {
> +        while ((c = getopt_long(argc, argv, "hptcmin:bd:", options, NULL)) >= 0) {
>
>                  switch (c) {
>
> @@ -566,6 +570,10 @@ static int parse_argv(int argc, char *argv[]) {
>
>                          break;
>
> +                case 'b':
> +                        arg_batch = true;
> +                        break;
> +
>                  case 'p':
>                          arg_order = ORDER_PATH;
>                          break;
> @@ -656,17 +664,25 @@ int main(int argc, char *argv[]) {
>                  if (arg_iterations && iteration >= arg_iterations)
>                          break;
>
> -                r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL);
> -                if (r == -ETIMEDOUT)
> -                        continue;
> -                if (r < 0) {
> -                        log_error("Couldn't read key: %s", strerror(-r));
> -                        goto finish;
> +                if (!arg_batch) {
> +                        r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL);
> +                        if (r == -ETIMEDOUT)
> +                                continue;
> +                        if (r < 0) {
> +                                log_error("Couldn't read key: %s", strerror(-r));
> +                                goto finish;
> +                        }
> +                }
> +                else {
> +                        usleep(last_refresh + arg_delay - t);
>                  }
>
>                  fputs("\r \r", stdout);
>                  fflush(stdout);
>
> +                if (arg_batch)
> +                        continue;
> +
>                  switch (key) {
>
>                  case ' ':
> --
> 1.7.11.2
>



-- 
David Strauss
   | david at davidstrauss.net
   | +1 512 577 5827 [mobile]


More information about the systemd-devel mailing list