Commit 0e580df7 authored by Arie Peterson's avatar Arie Peterson 🐚
Browse files

Use getopt to parse command line options

parent abc7940c
......@@ -11,6 +11,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/reboot.h>
#define U_DISABLE_CURL
......
......@@ -11,18 +11,86 @@ struct config
extern struct config config;
struct config config;
void print_usage()
{
printf("Usage:\n");
printf(" cryptops-api --root-device=DEV --crypt-name=LABEL "
" [--notify-command=COMMAND]\n");
}
void set_config_from_arguments(int argc, char ** argv)
{
if (argc < 4)
// Set defaults.
char * crypt_name = NULL;
char * notify_command = "";
char * root_device = NULL;
int c;
while (true)
{
static struct option long_options[] =
{
{"crypt-name" , required_argument, 0, 'c'},
{"notify-command", required_argument, 0, 'n'},
{"root-device" , required_argument, 0, 'r'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "c:n:r:", long_options, &option_index);
// Check if we have dealt with all options.
if (c == -1)
{
break;
}
switch (c)
{
case 'c':
crypt_name = optarg;
break;
case 'n':
notify_command = optarg;
break;
case 'r':
root_device = optarg;
break;
case '?':
// getopt_long already printed an error message.
break;
default:
abort();
}
}
// Print any remaining command line arguments (not options).
if (optind < argc)
{
printf("non-option ARGV-elements: ");
while (optind < argc)
{
printf("%s ", argv[optind++]);
}
putchar('\n');
}
if (root_device == NULL)
{
printf("Required option --root-device missing.\n");
print_usage();
exit(1);
}
if (crypt_name == NULL)
{
printf("Not enough arguments. Usage:\n");
printf(" cryptops-api ROOTDEV CRYPTNAME NOTIFY_COMMAND\n");
printf("Required option --crypt-name missing.\n");
print_usage();
exit(1);
}
config.root_device = argv[1];
config.mapped_device_name = argv[2];
config.notify_command = argv[3];
config.root_device = root_device;
config.mapped_device_name = crypt_name;
config.notify_command = notify_command;
asprintf(&config.info_partition_device, "%s1", config.root_device);
asprintf(&config.data_partition_device, "%s2", config.root_device);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment