From ea11b88bee941efb5a451b1509b752913c9b9ec6 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 22 May 2015 10:46:37 -0300 Subject: [PATCH] gp2rml/gp2rml.c (main): use getopt --- gp2rml/gp2rml.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/gp2rml/gp2rml.c b/gp2rml/gp2rml.c index ce7be84..5fbf857 100644 --- a/gp2rml/gp2rml.c +++ b/gp2rml/gp2rml.c @@ -1,8 +1,8 @@ /* * gp2rml.c - Convert from gnuplot to RML * - * Written 2010-2013 by Werner Almesberger - * Copyright 2010-2013 Werner Almesberger + * Written 2010-2013, 2015 by Werner Almesberger + * Copyright 2010-2013, 2015 Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -197,42 +198,41 @@ static void usage(const char *name) } -int main(int argc, const char **argv) +int main(int argc, char *const *argv) { - const char *name; FILE *file; char *end; int i; double p[3]; + int c; - name = *argv; + while ((c = getopt(argc, argv, "s:")) != EOF) + switch (c) { + case 's': + z_scale = strtod(optarg &end); + if (*end) + usage(*argv); + break; + default: + usage(*argv); + } - if (argc > 2 && !strcmp(argv[1], "-s")) { - if (argc < 3) - usage(name); - z_scale = strtod(argv[2], &end); - if (*end) - usage(name); - argc -= 2; - argv += 2; - } - - switch (argc) { - case 4: + switch (argc - optind) { + case 3: file = stdin; break; - case 5: - file = fopen(argv[4], "r"); + case 4: + file = fopen(argv[optind + 3], "r"); if (!file) { - perror(argv[4]); + perror(argv[optind + 3]); return 1; } break; default: - usage(name); + usage(*argv); } for (i = 0; i != 3; i++) { - p[i] = strtod(argv[i+1], &end); + p[i] = strtod(argv[optind + i], &end); /* * Allow the clearance to have a unit, for consistency in * mkmk-simple @@ -240,7 +240,7 @@ int main(int argc, const char **argv) if (!i && *end && !strcmp(end, "mm")) continue; if (*end || p[i] <= 0) - usage(name); + usage(*argv); } process_file(file);