#include #include #include #include #include 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; }