summaryrefslogtreecommitdiff
path: root/src/unaam.c
blob: 7a4a8536cf5fd4d3480b73c1a8de0b04e4e5187b (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>

#include "io/read_file.h"
#include "util/option.h"

static const char *usage_string =
  "Gebruik: %s [OPTIES]...\n"
  "\n"
  "Toon systeem informatie\n"
  "\n"
  "  -a    Toon alle informatie\n"
  "  -s    Toon kernnaam\n"
  "  -n    Toon netwerk knooppunt gastheernaam\n"
  "  -r    Toon kernuitgave\n"
  "  -v    Toon kernversie\n"
  "  -m    Toon machine harde-goederen-naam\n"
  "  -h    Toon deze hulptekst\n"
  "  -V    Toon versienummer\n";

struct options {
  bool print_all;

  bool print_sysname;
  bool print_nodename;
  bool print_release;
  bool print_version;
  bool print_machine;
};

static void parse_options(int argc, char **argv, struct options *opts) {
  const struct option_spec spec[] = {
    {'a', OPTION_SETBOOL(&opts->print_all)},

    {'s', OPTION_SETBOOL(&opts->print_sysname)},
    {'n', OPTION_SETBOOL(&opts->print_nodename)},
    {'r', OPTION_SETBOOL(&opts->print_release)},
    {'v', OPTION_SETBOOL(&opts->print_version)},
    {'m', OPTION_SETBOOL(&opts->print_machine)},

    {'h', OPTION_HELPUSAGE(usage_string)},
    {'V', OPTION_VERSION()},
    OPTION_SPEC_END
  };

  option_parse(argc, argv, spec);
}

int entry_unaam(int argc, char **argv) {
  struct options opts = {0};
  parse_options(argc, argv, &opts);

  if (opts.print_all == false &&
    opts.print_sysname == false &&
    opts.print_nodename == false &&
    opts.print_release == false &&
    opts.print_version == false &&
    opts.print_machine == false) {
    opts.print_sysname = true;
  }

  struct utsname name;
  uname(&name); // can't fail

  bool first = true;

#define PRINT(typ) \
  if (opts.print_all || opts.print_ ## typ) { \
    printf("%s%s", first ? "" : " ", name.typ); \
    first = false; \
  }

  PRINT(sysname);
  PRINT(nodename);
  PRINT(release);
  PRINT(version);
  PRINT(machine);
  printf("\n");

  return 0;
}