Add parameter name back to the chsmack error message
authorJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tue, 22 Oct 2013 19:26:26 +0000 (22:26 +0300)
committerJarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Wed, 23 Oct 2013 05:41:45 +0000 (08:41 +0300)
This patch add parameter name back to the error message. Instead
of relaying on hazardous longindex parameter of getopt_long(), a
look-up table is constructed to map short option to the corresponding
struct option entry.

Additionally, the basename of the application is added to the error
message. Also usage message is converted to use basename instead of
the full path name.
(cherry picked from commit d98a04ff7ccbcfcf36d9d4d43de68448b70da9fa)

utils/chsmack.c

index 1a20c81..ecb3a87 100644 (file)
 
 int main(int argc, char *argv[])
 {
-       static struct option long_options[] = {
+       static struct option options[] = {
                {"access", required_argument, 0, 'a'},
                {"exec", required_argument, 0, 'e'},
                {"mmap", required_argument, 0, 'm'},
                {"transmute", no_argument, 0, 't'},
-               {0, 0, 0, 0}
+               {NULL, 0, 0, 0}
        };
 
        /*  Buffers are zeroed automatically by keeping them static variables.
@@ -45,6 +45,7 @@ int main(int argc, char *argv[])
        static char access_buf[SMACK_LABEL_LEN + 1];
        static char exec_buf[SMACK_LABEL_LEN + 1];
        static char mmap_buf[SMACK_LABEL_LEN + 1];
+       static int options_map[128];
 
        int transmute_flag = 0;
        int option_flag = 0;
@@ -52,14 +53,18 @@ int main(int argc, char *argv[])
        int c;
        int i;
 
-       while ((c = getopt_long(argc, argv, "a:e:m:t", long_options,
+       for (i = 0; options[i].name != NULL; i++)
+               options_map[options[i].val] = i;
+
+       while ((c = getopt_long(argc, argv, "a:e:m:t", options,
                                NULL)) != -1) {
                if ((c == 'a' || c == 'e' || c == 'm')
                    && strnlen(optarg, SMACK_LABEL_LEN + 1)
                       == (SMACK_LABEL_LEN + 1)) {
-                       fprintf(stderr, "label \"%s\" "
+                       fprintf(stderr, "%s: %s: \"%s\" " \
                                        "exceeds %d characters.\n",
-                               optarg, SMACK_LABEL_LEN);
+                               basename(argv[0]), options[options_map[c]].name, optarg,
+                                        SMACK_LABEL_LEN);
                        exit(1);
                }
 
@@ -77,7 +82,7 @@ int main(int argc, char *argv[])
                                transmute_flag = 1;
                                break;
                        default:
-                               printf("Usage: %s [options] <path>\n", argv[0]);
+                               printf("Usage: %s [options] <path>\n", basename(argv[0]));
                                printf("options:\n");
                                printf(" [--access|-a] set security.SMACK64\n");
                                printf(" [--exec|-e] set security.SMACK64EXEC\n");