* [ltt-dev] [LTTCTL PATCH] Add getopt for options management
@ 2010-11-20 19:23 David Goulet
2010-11-20 20:02 ` Mathieu Desnoyers
0 siblings, 1 reply; 2+ messages in thread
From: David Goulet @ 2010-11-20 19:23 UTC (permalink / raw)
This patch also introduce the usage function.
Signed-off-by: David Goulet <david.goulet at polymtl.ca>
---
lttng/lttngtrace.c | 79 +++++++++++++++++++++++++++++++--------------------
1 files changed, 48 insertions(+), 31 deletions(-)
diff --git a/lttng/lttngtrace.c b/lttng/lttngtrace.c
index 20f8dad..37376a3 100644
--- a/lttng/lttngtrace.c
+++ b/lttng/lttngtrace.c
@@ -52,6 +52,16 @@ static int autotrace; /*
* we unlink if needed ?
*/
static int sigfwd_pid;
+static char *progname = NULL;
+
+void usage(void) {
+ fprintf(stderr, "usage : %s [-o trace_name] command\n", progname);
+ fprintf(stderr, "\nTracing tool for LTTng and UST\n\
+\n\
+Options:\n\
+ -o trace_name\t\tOutput file of the trace\n\
+");
+}
static int recunlink(const char *dirname)
{
@@ -151,7 +161,7 @@ static int write_child_pid(pid_t pid, uid_t uid, gid_t gid)
perror("Error writing child pid");
return -errno;
}
-
+
fprintf(fp, "%u", (unsigned int) pid);
ret = fclose(fp);
if (ret)
@@ -163,32 +173,38 @@ static int write_child_pid(pid_t pid, uid_t uid, gid_t gid)
return ret;
}
-static int parse_options(int argc, char *argv[], int *arg)
+static int parse_options(int argc, char **argv, int *arg)
{
- int ret = 0;
-
- for (;;) {
- if (*arg >= argc
- || argv[*arg][0] != '-'
- || argv[*arg][0] == '\0'
- || argv[*arg][1] == '\0'
- || !strcmp(argv[*arg], "--"))
- break;
- switch (argv[*arg][1]) {
- case 'o': if (*arg + 1 >= argc) {
- printf("Missing -o trace name\n");
- ret = -EINVAL;
- break;
- }
- trace_path = argv[*arg + 1];
- (*arg) += 2;
+ int i, c;
+
+ while ((c = getopt(argc, argv, "ho:")) != -1) {
+ switch (c) {
+ case 'o':
+ trace_path = strdup(optarg);
break;
- default: printf("Unknown option -%c\n", argv[*arg][1]);
- ret = -EINVAL;
- return ret;
+ case 'h':
+ usage();
+ exit(EXIT_SUCCESS);
+ case '?':
+ usage();
+ exit(EXIT_FAILURE);
+ default:
+ exit(EXIT_FAILURE);
}
}
- return ret;
+
+ if (argc - optind > 0) {
+ for (i = optind + 1; i < argc; i++) {
+ printf ("Non-option argument %s\n", argv[i]);
+ }
+ } else {
+ usage();
+ exit(EXIT_FAILURE);
+ }
+
+ (*arg) = optind;
+
+ return 0;
}
static int init_trace_path(void)
@@ -263,16 +279,19 @@ int main(int argc, char *argv[])
int arg = 1;
sigset_t saved_mask;
- if (argc < 2)
+ progname = argv[0];
+
+ if (argc < 2) {
+ usage();
return -ENOENT;
+ }
- euid = geteuid();
uid = getuid();
egid = getegid();
gid = geteuid();
if (euid != 0 && uid != 0) {
- printf("%s must be setuid root\n", argv[0]);
+ printf("%s must be setuid root\n", progname);
return -EPERM;
}
@@ -281,11 +300,9 @@ int main(int argc, char *argv[])
printf_dbg("egid: %d\n", egid);
printf_dbg("gid: %d\n", gid);
- if (arg < argc) {
- ret = parse_options(argc, argv, &arg);
- if (ret)
- return ret;
- }
+ ret = parse_options(argc, argv, &arg);
+ if (ret)
+ return ret;
ret = init_trace_path();
gret = (gret == 0) ? ret : gret;
--
1.7.3.2
^ permalink raw reply [flat|nested] 2+ messages in thread* [ltt-dev] [LTTCTL PATCH] Add getopt for options management
2010-11-20 19:23 [ltt-dev] [LTTCTL PATCH] Add getopt for options management David Goulet
@ 2010-11-20 20:02 ` Mathieu Desnoyers
0 siblings, 0 replies; 2+ messages in thread
From: Mathieu Desnoyers @ 2010-11-20 20:02 UTC (permalink / raw)
* David Goulet (david.goulet at polymtl.ca) wrote:
> This patch also introduce the usage function.
Fixed a mistake in the patch (removed the geteuid() from main).
Committed and pushed.
Thanks,
Mathieu
>
> Signed-off-by: David Goulet <david.goulet at polymtl.ca>
> ---
> lttng/lttngtrace.c | 79 +++++++++++++++++++++++++++++++--------------------
> 1 files changed, 48 insertions(+), 31 deletions(-)
>
> diff --git a/lttng/lttngtrace.c b/lttng/lttngtrace.c
> index 20f8dad..37376a3 100644
> --- a/lttng/lttngtrace.c
> +++ b/lttng/lttngtrace.c
> @@ -52,6 +52,16 @@ static int autotrace; /*
> * we unlink if needed ?
> */
> static int sigfwd_pid;
> +static char *progname = NULL;
> +
> +void usage(void) {
> + fprintf(stderr, "usage : %s [-o trace_name] command\n", progname);
> + fprintf(stderr, "\nTracing tool for LTTng and UST\n\
> +\n\
> +Options:\n\
> + -o trace_name\t\tOutput file of the trace\n\
> +");
> +}
>
> static int recunlink(const char *dirname)
> {
> @@ -151,7 +161,7 @@ static int write_child_pid(pid_t pid, uid_t uid, gid_t gid)
> perror("Error writing child pid");
> return -errno;
> }
> -
> +
> fprintf(fp, "%u", (unsigned int) pid);
> ret = fclose(fp);
> if (ret)
> @@ -163,32 +173,38 @@ static int write_child_pid(pid_t pid, uid_t uid, gid_t gid)
> return ret;
> }
>
> -static int parse_options(int argc, char *argv[], int *arg)
> +static int parse_options(int argc, char **argv, int *arg)
> {
> - int ret = 0;
> -
> - for (;;) {
> - if (*arg >= argc
> - || argv[*arg][0] != '-'
> - || argv[*arg][0] == '\0'
> - || argv[*arg][1] == '\0'
> - || !strcmp(argv[*arg], "--"))
> - break;
> - switch (argv[*arg][1]) {
> - case 'o': if (*arg + 1 >= argc) {
> - printf("Missing -o trace name\n");
> - ret = -EINVAL;
> - break;
> - }
> - trace_path = argv[*arg + 1];
> - (*arg) += 2;
> + int i, c;
> +
> + while ((c = getopt(argc, argv, "ho:")) != -1) {
> + switch (c) {
> + case 'o':
> + trace_path = strdup(optarg);
> break;
> - default: printf("Unknown option -%c\n", argv[*arg][1]);
> - ret = -EINVAL;
> - return ret;
> + case 'h':
> + usage();
> + exit(EXIT_SUCCESS);
> + case '?':
> + usage();
> + exit(EXIT_FAILURE);
> + default:
> + exit(EXIT_FAILURE);
> }
> }
> - return ret;
> +
> + if (argc - optind > 0) {
> + for (i = optind + 1; i < argc; i++) {
> + printf ("Non-option argument %s\n", argv[i]);
> + }
> + } else {
> + usage();
> + exit(EXIT_FAILURE);
> + }
> +
> + (*arg) = optind;
> +
> + return 0;
> }
>
> static int init_trace_path(void)
> @@ -263,16 +279,19 @@ int main(int argc, char *argv[])
> int arg = 1;
> sigset_t saved_mask;
>
> - if (argc < 2)
> + progname = argv[0];
> +
> + if (argc < 2) {
> + usage();
> return -ENOENT;
> + }
>
> - euid = geteuid();
> uid = getuid();
> egid = getegid();
> gid = geteuid();
>
> if (euid != 0 && uid != 0) {
> - printf("%s must be setuid root\n", argv[0]);
> + printf("%s must be setuid root\n", progname);
> return -EPERM;
> }
>
> @@ -281,11 +300,9 @@ int main(int argc, char *argv[])
> printf_dbg("egid: %d\n", egid);
> printf_dbg("gid: %d\n", gid);
>
> - if (arg < argc) {
> - ret = parse_options(argc, argv, &arg);
> - if (ret)
> - return ret;
> - }
> + ret = parse_options(argc, argv, &arg);
> + if (ret)
> + return ret;
>
> ret = init_trace_path();
> gret = (gret == 0) ? ret : gret;
> --
> 1.7.3.2
>
>
> _______________________________________________
> ltt-dev mailing list
> ltt-dev at lists.casi.polymtl.ca
> http://lists.casi.polymtl.ca/cgi-bin/mailman/listinfo/ltt-dev
>
--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-11-20 20:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-20 19:23 [ltt-dev] [LTTCTL PATCH] Add getopt for options management David Goulet
2010-11-20 20:02 ` Mathieu Desnoyers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox