Browse Source

Initial commit

Matthias Vogelgesang 8 years ago
commit
8e66ade538
3 changed files with 93 additions and 0 deletions
  1. 4 0
      Makefile
  2. 1 0
      README.md
  3. 88 0
      gd.c

+ 4 - 0
Makefile

@@ -0,0 +1,4 @@
+all: gd
+
+gd: gd.c
+	$(CC) $(CFLAGS) -o $@ $<

+ 1 - 0
README.md

@@ -0,0 +1 @@
+Generate counter data ...

+ 88 - 0
gd.c

@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <stdint.h>
+
+
+struct Options {
+    const char *output;
+    unsigned count;
+} opts;
+
+static void
+usage (void)
+{
+    printf ("Usage: gd [OPTION]\n"
+            "Options:\n"
+            "  -h, --help       Show this help message and exit\n"
+            "  -o, --output     Output filename\n"
+            "  -c, --count      Number of blocks\n");
+}
+
+static void
+parse_options (int argc, char *const *argv)
+{
+    enum {
+        OPT_HELP = 'h',
+        OPT_OUTPUT = 'o',
+        OPT_COUNT = 'c',
+    };
+
+    static struct option long_options[] = {
+        { "help", no_argument, 0, OPT_HELP },
+        { "output", required_argument, 0, OPT_OUTPUT },
+        { "count", required_argument, 0, OPT_COUNT },
+        { 0, 0, 0, 0 },
+    };
+
+    int ret;
+    int index;
+
+    if (argc == 1) {
+        usage ();
+        exit (0);
+    }
+
+    memset (&opts, 0, sizeof (struct Options));
+
+    while ((ret = getopt_long (argc, argv, "o:c:h", long_options, &index)) != -1) {
+        switch (ret) {
+            case OPT_HELP:
+                usage ();
+                exit (0);
+            case OPT_OUTPUT:
+                opts.output = optarg;
+                break;
+            case OPT_COUNT:
+                opts.count = (size_t) atol (optarg);
+                break;
+            default:
+                break;
+        }
+    }
+}
+
+static void
+write_data (void)
+{
+    FILE *fp;
+    uint32_t i;
+
+    fp = opts.output != NULL ? fopen (opts.output, "wb") : stdout;
+
+    for (i = 0; i < opts.count; i++) {
+        fwrite (&i, 4, 1, fp);
+    }
+
+    if (opts.output != NULL)
+        fclose (fp);
+}
+
+int
+main (int argc, char const* argv[])
+{
+    parse_options (argc, (char *const *) argv);
+    write_data ();
+    return 0;
+}