From b70f033df1ef23a1f46c577c195979b6eed57bfc Mon Sep 17 00:00:00 2001 From: Ivana Varekova Date: Fri, 19 Jun 2009 17:08:35 +0200 Subject: [PATCH] cgset: Change the handling of options, add --help Change the handling of options - add the possibility to use long options - add --help option - change the type of c (see man 3 getopt_long) Signed-off-by: Ivana Varekova Signed-off-by: Dhaval Giani --- src/tools/cgset.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/tools/cgset.c b/src/tools/cgset.c index 071ffac4..0c153eb9 100644 --- a/src/tools/cgset.c +++ b/src/tools/cgset.c @@ -5,9 +5,18 @@ #include #include #include +#include #include "tools-common.h" +static struct option const long_options[] = +{ + {"rule", required_argument, NULL, 'r'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} +}; + + struct cgroup *copy_name_value_from_rules(int nv_number, struct control_value *name_value) { @@ -63,11 +72,24 @@ scgroup_err: return NULL; } +void usage(int status, char *program_name) +{ + if (status != 0) + fprintf(stderr, "Wrong input parameters," + " try %s --help' for more information.\n", + program_name); + else { + printf("Usage: %s [-r ] ...\n" + " or: %s --copy-from " + " ...\n", + program_name, program_name, program_name); + } +} int main(int argc, char *argv[]) { int ret = 0; - char c; + int c; char *buf; struct control_value *name_value = NULL; @@ -86,8 +108,15 @@ int main(int argc, char *argv[]) } /* parse arguments */ - while ((c = getopt(argc, argv, "r:")) > 0) { + while ((c = getopt_long (argc, argv, + "r:h", long_options, NULL)) != -1) { switch (c) { + case 'h': + usage(0, argv[0]); + ret = 0; + goto err; + break; + case 'r': /* add name-value pair to buffer (= name_value variable) */ @@ -97,8 +126,9 @@ int main(int argc, char *argv[]) realloc(name_value, nv_max * sizeof(struct control_value)); if (!name_value) { - fprintf(stderr, "cgset: " - "not enough memory\n"); + fprintf(stderr, "%s: " + "not enough memory\n", + argv[0]); ret = -1; goto err; } @@ -133,8 +163,7 @@ int main(int argc, char *argv[]) nv_number++; break; default: - fprintf(stderr, "%s: invalid command line option\n", - argv[0]); + usage(1, argv[0]); ret = -1; goto err; break; -- 2.47.2