utils: add options for version and usage information
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Mon, 14 Apr 2014 22:37:21 +0000 (01:37 +0300)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Thu, 24 Apr 2014 04:21:29 +0000 (07:21 +0300)
Added option -v/--version for displaying version information
and -h/--help for displaying usage information.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
configure.ac
utils/chsmack.c
utils/smackaccess.c
utils/smackcipso.c
utils/smackctl.c
utils/smackload.c

index 6e306c9..3f02ced 100644 (file)
@@ -6,6 +6,7 @@ AC_INIT([libsmack],
        [https://github.com/smack-team/smack])
 AC_CONFIG_SRCDIR([libsmack/libsmack.c])
 AC_CONFIG_AUX_DIR([build-aux])
+AC_CONFIG_HEADERS([utils/config.h])
 AM_INIT_AUTOMAKE([-Wall -Werror dist-bzip2 foreign])
 AC_GNU_SOURCE
 AC_CONFIG_MACRO_DIR([m4])
index f72bb56..5320fa5 100644 (file)
 #include <getopt.h>
 #include <errno.h>
 #include <libgen.h>
+#include "config.h"
 
 static const char usage[] =
        "Usage: %s [options] <path>\n"
        "options:\n"  
+       " -v --version       output version information and exit\n"
+       " -h --help          output usage information and exit\n"
        " -a --access        set/remove "XATTR_NAME_SMACK"\n"  
        " -e --exec          set/remove "XATTR_NAME_SMACKEXEC"\n"  
        " -m --mmap          set/remove "XATTR_NAME_SMACKMMAP"\n"  
@@ -114,8 +117,10 @@ static int smack_remove_label_for_path(const char *path,
 /* main */
 int main(int argc, char *argv[])
 {
-       static const char shortoptions[] = "a::e::m::tdL";
+       static const char shortoptions[] = "vha::e::m::tdL";
        static struct option options[] = {
+               {"version", no_argument, 0, 'v'},
+               {"help", no_argument, 0, 'h'},
                {"access", optional_argument, 0, 'a'},
                {"exec", optional_argument, 0, 'e'},
                {"mmap", optional_argument, 0, 'm'},
@@ -185,6 +190,13 @@ int main(int argc, char *argv[])
                                                        basename(argv[0]), options[options_map[c]].name);
                                follow_flag = 1;
                                break;
+                       case 'v':
+                               printf("%s (libsmack) version " PACKAGE_VERSION "\n",
+                                      basename(argv[0]));
+                               exit(0);
+                       case 'h':
+                               printf(usage, basename(argv[0]));
+                               exit(0);
                        default:
                                printf(usage, basename(argv[0]));
                                exit(1);
index 726e3f7..69be9fc 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <libgen.h>
+#include <unistd.h>
+#include <getopt.h>
+#include "config.h"
+
+static const char usage[] =
+       "Usage: %s [options] <subject> <object> <access>\n"
+       "options:\n"
+       " -v --version       output version information and exit\n"
+       " -h --help          output usage information and exit\n"
+;
+
+static const char short_options[] = "vh";
+
+static struct option options[] = {
+       {"version", no_argument, 0, 'v'},
+       {"help", no_argument, 0, 'h'},
+       {NULL, 0, 0, 0}
+};
 
 int main(int argc, char **argv)
 {
+       const char *subject;
+       const char *object;
+       const char *access;
        int ret;
+       int c;
 
-       if (argc < 4) {
-               fprintf(stderr, "Usage: %s <subject> <object> <access>\n", argv[0]);
-               return EXIT_FAILURE;
+       for ( ; ; ) {
+               c = getopt_long(argc, argv, short_options, options, NULL);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'v':
+                       printf("%s (libsmack) version " PACKAGE_VERSION "\n",
+                              basename(argv[0]));
+                       exit(0);
+               case 'h':
+                       printf(usage, basename(argv[0]));
+                       exit(0);
+               default:
+                       printf(usage, basename(argv[0]));
+                       exit(1);
+               }
+       }
+
+       if ((argc - optind) != 3) {
+               printf(usage, basename(argv[0]));
+               exit(1);
        }
 
-       ret = smack_have_access(argv[1], argv[2], argv[3]);
+       subject = argv[optind];
+       object = argv[optind + 1];
+       access = argv[optind + 2];
+
+       ret = smack_have_access(subject, object, access);
        if (ret < 0) {
                fprintf(stderr,"%s: input values are invalid.\n", basename(argv[0]));
                return EXIT_FAILURE;
index f32ea61..9ce65f5 100644 (file)
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/smack.h>
+#include <getopt.h>
+#include <libgen.h>
+#include "config.h"
+
+static const char usage[] =
+       "Usage: %s [options] <subject> <object> <access>\n"
+       "options:\n"
+       " -v --version       output version information and exit\n"
+       " -h --help          output usage information and exit\n"
+;
+
+static const char short_options[] = "vh";
+
+static struct option options[] = {
+       {"version", no_argument, 0, 'v'},
+       {"help", no_argument, 0, 'h'},
+       {NULL, 0, 0, 0}
+};
 
 int main(int argc, char **argv)
 {
+       int c;
+
+       for ( ; ; ) {
+               c = getopt_long(argc, argv, short_options, options, NULL);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'v':
+                       printf("%s (libsmack) version " PACKAGE_VERSION "\n",
+                              basename(argv[0]));
+                       exit(0);
+               case 'h':
+                       printf(usage, basename(argv[0]));
+                       exit(0);
+               default:
+                       printf(usage, basename(argv[0]));
+                       exit(1);
+               }
+       }
+
        if (!smack_smackfs_path()) {
                fprintf(stderr, "SmackFS is not mounted.\n");
                exit(1);
        }
 
-       if (argc > 2) {
-               fprintf(stderr, "Usage: %s <path>\n", argv[0]);
+       if ((argc - optind) > 1) {
+               printf(usage, basename(argv[0]));
                exit(1);
        }
 
index 439c233..7e73a51 100644 (file)
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <libgen.h>
+#include "config.h"
+
+static const char usage[] =
+       "Usage: %s [options] <subject> <object> <access>\n"
+       "options:\n"
+       " -v --version       output version information and exit\n"
+       " -h --help          output usage information and exit\n"
+;
+
+static const char short_options[] = "vh";
+
+static struct option options[] = {
+       {"version", no_argument, 0, 'v'},
+       {"help", no_argument, 0, 'h'},
+       {NULL, 0, 0, 0}
+};
 
 static int apply_all(void)
 {
@@ -46,21 +65,46 @@ static int apply_all(void)
 
 int main(int argc, char **argv)
 {
-       const char *tmp = smack_smackfs_path();
-       if (argc < 2) {
-               fprintf(stderr, "Usage: %s <action>\n", argv[0]);
+       const char *cmd;
+       int c;
+
+       for ( ; ; ) {
+               c = getopt_long(argc, argv, short_options, options, NULL);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'v':
+                       printf("%s (libsmack) version " PACKAGE_VERSION "\n",
+                              basename(argv[0]));
+                       exit(0);
+               case 'h':
+                       printf(usage, basename(argv[0]));
+                       exit(0);
+               default:
+                       printf(usage, basename(argv[0]));
+                       exit(1);
+               }
+       }
+
+       if ((argc - optind) != 1) {
+               printf(usage, basename(argv[0]));
                exit(1);
        }
 
-       if (!strcmp(argv[1], "apply")) {
+       cmd = argv[optind];
+
+       if (!strcmp(cmd, "apply")) {
                if (apply_all())
                        exit(1);
-       } else if (!strcmp(argv[1], "clear")) {
+       } else if (!strcmp(cmd, "clear")) {
                if (clear())
                        exit(1);
-       } else if (!strcmp(argv[1], "status")) {
-               if (tmp)
-                       printf("SmackFS is mounted to %s.\n", tmp);
+       } else if (!strcmp(cmd, "status")) {
+               if (smack_smackfs_path())
+                       printf("SmackFS is mounted to %s.\n",
+                              smack_smackfs_path());
                else
                        printf("SmackFS is not mounted.\n");
                exit(0);
index 73e1b72..0316865 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/smack.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <libgen.h>
+#include "config.h"
 
-static void usage(const char *bin)
-{
-       fprintf(stderr, "Usage: %s [-c] <path>\n", bin);
-       exit(1);
-}
+static const char usage[] =
+       "Usage: %s [options] <subject> <object> <access>\n"
+       "options:\n"
+       " -v --version       output version information and exit\n"
+       " -h --help          output usage information and exit\n"
+       " -c --clear         clear access rules\n"
+;
+
+static const char short_options[] = "vhc";
+
+static struct option options[] = {
+       {"version", no_argument, 0, 'v'},
+       {"help", no_argument, 0, 'h'},
+       {"clear", no_argument, 0, 'c'},
+       {NULL, 0, 0, 0}
+};
 
 int main(int argc, char **argv)
 {
        int clear = 0;
        int c;
 
-       if (!smack_smackfs_path()) {
-               fprintf(stderr, "SmackFS is not mounted.\n");
-               exit(1);
-       }
+       for ( ; ; ) {
+               c = getopt_long(argc, argv, short_options, options, NULL);
+
+               if (c == -1)
+                       break;
 
-       while ((c = getopt(argc, argv, "c")) != -1) {
                switch (c) {
                case 'c':
                        clear = 1;
                        break;
+               case 'v':
+                       printf("%s (libsmack) version " PACKAGE_VERSION "\n",
+                              basename(argv[0]));
+                       exit(0);
+               case 'h':
+                       printf(usage, basename(argv[0]));
+                       exit(0);
                default:
-                       usage(argv[0]);
+                       printf(usage, basename(argv[0]));
+                       exit(1);
                }
        }
 
+       if (!smack_smackfs_path()) {
+               fprintf(stderr, "SmackFS is not mounted.\n");
+               exit(1);
+       }
+
+       if ((argc - optind) > 1) {
+               printf(usage, basename(argv[0]));
+               exit(1);
+       }
+
        if (optind == argc) {
                if (apply_rules(NULL, clear))
                        exit(1);