vapier      15/08/05 07:00:11

  Modified:             uname-test.c
  Log:
  use getopt to parse the command line

Revision  Changes    Path
1.5                  src/patchsets/coreutils/uname-test/uname-test.c

file : 
http://sources.gentoo.org/viewvc.cgi/gentoo/src/patchsets/coreutils/uname-test/uname-test.c?rev=1.5&view=markup
plain: 
http://sources.gentoo.org/viewvc.cgi/gentoo/src/patchsets/coreutils/uname-test/uname-test.c?rev=1.5&content-type=text/plain
diff : 
http://sources.gentoo.org/viewvc.cgi/gentoo/src/patchsets/coreutils/uname-test/uname-test.c?r1=1.4&r2=1.5

Index: uname-test.c
===================================================================
RCS file: /var/cvsroot/gentoo/src/patchsets/coreutils/uname-test/uname-test.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- uname-test.c        5 Aug 2015 04:37:36 -0000       1.4
+++ uname-test.c        5 Aug 2015 07:00:11 -0000       1.5
@@ -3,12 +3,14 @@
  * Distributed under the terms of the GNU General Public License v2
  */
 
+#include <ctype.h>
+#include <err.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
+#include <unistd.h>
 
 #ifndef ARRAY_SIZE
 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -140,14 +142,21 @@
        static char hardware_platform[257];
        int i;
 
-       for (i = 1; i < argc; ++i) {
-               if (!strcmp(argv[i], "-v"))
+       while ((i = getopt(argc, argv, "v")) != -1) {
+               switch (i) {
+               case 'v':
                        verbose = true;
-               else {
-                       filename = argv[1];
-                       printf(">>> Parsing data out of %s\n", filename);
+                       break;
+               default:
+ usage:
+                       errx(1, "Usage: uname-test [-v] <file>");
                }
        }
+       if (optind + 1 != argc)
+               goto usage;
+
+       filename = argv[optind];
+       printf(">>> Parsing data out of %s\n", filename);
        i = 0;
 
        if (0 > __linux_procinfo (PROCINFO_PROCESSOR, processor, sizeof 
processor)) {




Reply via email to