Download c source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * 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;
}