diff options
author | Andrew G. Morgan <morgan@kernel.org> | 2020-01-04 16:43:12 -0800 |
---|---|---|
committer | Andrew G. Morgan <morgan@kernel.org> | 2020-01-04 16:43:12 -0800 |
commit | 8a2ac848a2065641e20a8b50384b68e8781cc6fc (patch) | |
tree | c55b52ed1f036c9d50c205a120940a690310fd8b | |
parent | a085eeee6770789a2d73cd3222f8a8c4392c44f1 (diff) | |
download | libcap2-8a2ac848a2065641e20a8b50384b68e8781cc6fc.tar.gz |
Add a --help option to getpcaps.
Browsing the debian bug tracker, I noted this:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=560260
the output of getpcaps has been ugly forever and since I've been
meaning to fix that I figured I'd add some optional arguments too:
Added: --help, --usage, --verbose, --ugly, --legacy
Example:
$ ./progs/getpcaps 1 --verbose 1 --ugly 1
1: =ep
Capabilities for '1': =ep
Capabilities for `1': =ep
Signed-off-by: Andrew G. Morgan <morgan@kernel.org>
-rw-r--r-- | progs/getpcaps.c | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/progs/getpcaps.c b/progs/getpcaps.c index 264dd00..497abcd 100644 --- a/progs/getpcaps.c +++ b/progs/getpcaps.c @@ -11,23 +11,28 @@ #include <stdlib.h> #include <sys/capability.h> -static void usage(void) +static void usage(int exiter) { fprintf(stderr, "usage: getcaps <pid> [<pid> ...]\n\n" " This program displays the capabilities on the queried process(es).\n" " The capabilities are displayed in the cap_from_text(3) format.\n\n" -"[Copyright (c) 1997-8,2007 Andrew G. Morgan <morgan@kernel.org>]\n" +" Optional arguments:\n" +" --help or --usage display this message.\n" +" --verbose use a more verbose output format.\n" +" --ugly or --legacy use the archaic legacy output format.\n\n" +"[Copyright (c) 1997-8,2007,2019 Andrew G. Morgan <morgan@kernel.org>]\n" ); - exit(1); + exit(exiter); } int main(int argc, char **argv) { int retval = 0; + int verbose = 0; if (argc < 2) { - usage(); + usage(1); } for ( ++argv; --argc > 0; ++argv ) { @@ -35,6 +40,16 @@ int main(int argc, char **argv) int pid; cap_t cap_d; + if (!strcmp(argv[0], "--help") || !strcmp(argv[0], "--usage")) { + usage(0); + } else if (!strcmp(argv[0], "--verbose")) { + verbose = 1; + continue; + } else if (!strcmp(argv[0], "--ugly") || !strcmp(argv[0], "--legacy")) { + verbose = 2; + continue; + } + pid = atoi(argv[0]); cap_d = cap_get_pid(pid); @@ -45,7 +60,13 @@ int main(int argc, char **argv) continue; } else { char *result = cap_to_text(cap_d, &length); - fprintf(stderr, "Capabilities for `%s': %s\n", *argv, result); + if (verbose == 1) { + printf("Capabilities for '%s': %s\n", *argv, result); + } else if (verbose == 2) { + fprintf(stderr, "Capabilities for `%s': %s\n", *argv, result); + } else { + printf("%s: %s\n", *argv, result); + } cap_free(result); result = NULL; cap_free(cap_d); |