Download c source code

/*
 * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
 */

#include <stdio.h>

#include "arguments.h"

int analyze_arguments(int argc, char* argv[]
    , shared_thread_data_t* shared_data) {
  int error = 0;
  if (argc == 8) {
    if (sscanf(argv[1], "%zu", &shared_data->product_count) != 1
      || shared_data->product_count == 0) {
        fprintf(stderr, "error: invalid product count\n");
        error = 2;
    } else if (sscanf(argv[2], "%zu", &shared_data->producer_count) != 1
      || shared_data->producer_count == 0) {
        fprintf(stderr, "error: invalid producer count\n");
        error = 3;
    } else if (sscanf(argv[3], "%zu", &shared_data->consumer_count) != 1
      || shared_data->consumer_count == 0) {
        fprintf(stderr, "error: invalid consumer count\n");
        error = 4;
    } else if (sscanf(argv[4], "%u"
      , &shared_data->min_producer_delay) != 1) {
        fprintf(stderr, "error: invalid min producer delay\n");
        error = 5;
    } else if (sscanf(argv[5], "%u"
      , &shared_data->max_producer_delay) != 1) {
        fprintf(stderr, "error: invalid max producer delay\n");
        error = 6;
    } else if (sscanf(argv[6], "%u"
      , &shared_data->min_consumer_delay) != 1) {
        fprintf(stderr, "error: invalid min consumer delay\n");
        error = 7;
    } else if (sscanf(argv[7], "%u"
      , &shared_data->max_consumer_delay) != 1) {
        fprintf(stderr, "error: invalid max consumer delay\n");
        error = 8;
    }
  } else {
    fprintf(stderr, "usage: producer_consumer product_count producers consumers"
      " min_producer_delay max_producer_delay"
      " min_consumer_delay max_consumer_delay\n");
      error = 1;
  }
  return error;
}