/** * parse_options - Read and validate the programs command line * * Read the command line, verify the syntax and parse the options. * This function is very long, but quite simple. * * Return: 1 Success * 0 Error, one or more problems */ static int parse_options(int argc, char **argv) { static const char *sopt = "-a:fh?i:n:qVvr"; static const struct option lopt[] = { { "attribute", required_argument, NULL, 'a' }, { "attribute-name", required_argument, NULL, 'n' }, { "force", no_argument, NULL, 'f' }, { "help", no_argument, NULL, 'h' }, { "inode", required_argument, NULL, 'i' }, { "quiet", no_argument, NULL, 'q' }, { "version", no_argument, NULL, 'V' }, { "verbose", no_argument, NULL, 'v' }, { "raw", no_argument, NULL, 'r' }, { NULL, 0, NULL, 0 } }; int c = -1; int err = 0; int ver = 0; int help = 0; int levels = 0; ATTR_TYPES attr = AT_UNUSED; opterr = 0; /* We'll handle the errors, thank you. */ opts.inode = -1; opts.attr = cpu_to_le32(-1); opts.attr_name = NULL; opts.attr_name_len = 0; while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { switch (c) { case 1: /* A non-option argument */ if (!opts.device) { opts.device = argv[optind - 1]; } else if (!opts.file) { opts.file = argv[optind - 1]; } else { ntfs_log_error("You must specify exactly one " "file.\n"); err++; } break; case 'a': if (opts.attr != cpu_to_le32(-1)) { ntfs_log_error("You must specify exactly one " "attribute.\n"); } else if (parse_attribute(optarg, &attr) > 0) { opts.attr = attr; break; } else { ntfs_log_error("Couldn't parse attribute.\n"); } err++; break; case 'f': opts.force++; break; case 'h': case '?': if (strncmp (argv[optind-1], "--log-", 6) == 0) { if (!ntfs_log_parse_option (argv[optind-1])) err++; break; } help++; break; case 'i': if (opts.inode != -1) ntfs_log_error("You must specify exactly one inode.\n"); else if (utils_parse_size(optarg, &opts.inode, FALSE)) break; else ntfs_log_error("Couldn't parse inode number.\n"); err++; break; case 'n': opts.attr_name_len = ntfs_mbstoucs(optarg, &opts.attr_name); if (opts.attr_name_len < 0) { ntfs_log_perror("Invalid attribute name '%s'", optarg); usage(); } case 'q': opts.quiet++; ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); break; case 'V': ver++; break; case 'v': opts.verbose++; ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); break; case 'r': opts.raw = TRUE; break; default: ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); err++; break; } } /* Make sure we're in sync with the log levels */ levels = ntfs_log_get_levels(); if (levels & NTFS_LOG_LEVEL_VERBOSE) opts.verbose++; if (!(levels & NTFS_LOG_LEVEL_QUIET)) opts.quiet++; if (help || ver) { opts.quiet = 0; } else { if (opts.device == NULL) { ntfs_log_error("You must specify a device.\n"); err++; } else if (opts.file == NULL && opts.inode == -1) { ntfs_log_error("You must specify a file or inode " "with the -i option.\n"); err++; } else if (opts.file != NULL && opts.inode != -1) { ntfs_log_error("You can't specify both a file and inode.\n"); err++; } if (opts.quiet && opts.verbose) { ntfs_log_error("You may not use --quiet and --verbose at the " "same time.\n"); err++; } } if (ver) version(); if (help || err) usage(); return (!err && !help && !ver); }
/** * parse_options - Read and validate the programs command line * * Read the command line, verify the syntax and parse the options. * This function is very long, but quite simple. * * Return: 1 Success * 0 Error, one or more problems */ static int parse_options(int argc, char **argv) { static const char *sopt = "-fh?i:k:qVv"; static const struct option lopt[] = { {"force", no_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"inode", required_argument, NULL, 'i'}, {"keyfile", required_argument, NULL, 'k'}, {"quiet", no_argument, NULL, 'q'}, {"version", no_argument, NULL, 'V'}, {"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0} }; int c = -1; int err = 0; int ver = 0; int help = 0; opterr = 0; /* We'll handle the errors, thank you. */ opts.inode = -1; while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { switch (c) { case 1: /* A non-option argument */ if (!opts.device) opts.device = argv[optind - 1]; else if (!opts.file) opts.file = argv[optind - 1]; else { ntfs_log_error("You must specify exactly one " "file.\n"); err++; } break; case 'f': opts.force++; break; case 'h': case '?': help++; break; case 'k': if (!opts.keyfile) opts.keyfile = argv[optind - 1]; else { ntfs_log_error("You must specify exactly one " "key file.\n"); err++; } break; case 'i': if (opts.inode != -1) ntfs_log_error("You must specify exactly one " "inode.\n"); else if (utils_parse_size(optarg, &opts.inode, FALSE)) break; else ntfs_log_error("Couldn't parse inode number.\n"); err++; break; case 'q': opts.quiet++; ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); break; case 'V': ver++; break; case 'v': opts.verbose++; ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); break; default: ntfs_log_error("Unknown option '%s'.\n", argv[optind - 1]); err++; break; } } if (help || ver) { opts.quiet = 0; ntfs_log_set_levels(NTFS_LOG_LEVEL_QUIET); } else { if (!opts.keyfile) { ntfs_log_error("You must specify a key file.\n"); err++; } else if (opts.device == NULL) { ntfs_log_error("You must specify a device.\n"); err++; } else if (opts.file == NULL && opts.inode == -1) { ntfs_log_error("You must specify a file or inode with " "the -i option.\n"); err++; } else if (opts.file != NULL && opts.inode != -1) { ntfs_log_error("You can't specify both a file and " "inode.\n"); err++; } if (opts.quiet && opts.verbose) { ntfs_log_error("You may not use --quiet and --verbose " "at the same time.\n"); err++; } } if (ver) version(); if (help || err) usage(); return (!err && !help && !ver); }