Add argument parsing

master
Nick Krichevsky 2019-09-21 21:36:38 -04:00
parent 8ec844245a
commit 369cafe0c1
2 changed files with 15 additions and 2 deletions

16
main.c
View File

@ -73,8 +73,20 @@ static int get_input(int num_threads, struct input **res) {
int main(int argc, char *argv[]) {
struct input *inputs = NULL;
// TODO: Get input to get actual number of inputs. 5 is just for funzies.
int num_inputs = get_input(5, &inputs);
if (argc < 2 || argc > 3) {
printf("%s\n", USAGE_STRING);
return -1;
} else if (argc == 3) {
printf("Not yet implemented.");
return -1;
}
int num_threads = strtol(argv[1], NULL, 10);
if (errno == ERANGE || (num_threads == 0 && errno == EINVAL)) {
printf("Invalid number of threads.\n");
}
int num_inputs = get_input(num_threads, &inputs);
for (int i = 0; i < num_inputs; i++) {
printf("#%d recipient=%d value=%d\n", i, inputs[i].recipient, inputs[i].value);
}

1
main.h
View File

@ -5,6 +5,7 @@
#define MAXTHREAD 10
// The max size of the input buffer
#define BUFFER_SIZE 128
#define USAGE_STRING "./mailbox num_threads [nb]"
// input represents a pair of values inputted at the command line
struct input {