Getopt'ed by Marc Nijdam <marc_nijdam@hp.com>
authorEric Andersen <andersen@codepoet.org>
Fri, 14 Jul 2000 18:39:08 +0000 (18:39 -0000)
committerEric Andersen <andersen@codepoet.org>
Fri, 14 Jul 2000 18:39:08 +0000 (18:39 -0000)
 -Erik

coreutils/date.c
coreutils/echo.c
date.c
dmesg.c
echo.c
util-linux/dmesg.c

index bc6d131370620988992f0938afaa8b7e8417f37a..3ede1237ca84ec4891152df6e58562f22abc8bca 100644 (file)
@@ -159,7 +159,7 @@ int date_main(int argc, char **argv)
        char *date_str = NULL;
        char *date_fmt = NULL;
        char *t_buff;
-       int i;
+       char c;
        int set_time = 0;
        int rfc822 = 0;
        int utc = 0;
@@ -168,49 +168,39 @@ int date_main(int argc, char **argv)
        struct tm tm_time;
 
        /* Interpret command line args */
-       i = --argc;
-       argv++;
-       while (i > 0 && **argv) {
-               if (**argv == '-') {
-                       while (i > 0 && *++(*argv))
-                               switch (**argv) {
-                               case 'R':
-                                       rfc822 = 1;
-                                       break;
-                               case 's':
-                                       set_time = 1;
-                                       if (date_str != NULL)
-                                               usage(date_usage);
-                                       date_str = *argv;
-                                       break;
-                               case 'u':
-                                       utc = 1;
-                                       if (putenv("TZ=UTC0") != 0) 
-                                               fatalError(memory_exhausted);
-                                       break;
-                               case 'd':
-                                       use_arg = 1;
-                                       if (date_str != NULL)
-                                               usage(date_usage);
-                                       date_str = *argv;
-                                       break;
-                               case '-':
-                                       usage(date_usage);
-                               }
-               } else {
-                       if ((date_fmt == NULL) && (**argv == '+'))
-                               date_fmt = *argv + 1;   /* Skip over the '+' */
-                       else if (date_str == NULL) {
-                               set_time = 1;
-                               date_str = *argv;
-                       } else {
+       while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
+               switch (c) {
+               case 'R':
+                       rfc822 = 1;
+                       break;
+               case 's':
+                       set_time = 1;
+                       if ((date_str != NULL) || ((date_str = optarg) == NULL))
                                usage(date_usage);
-                       }
+                       break;
+               case 'u':
+                       utc = 1;
+                       if (putenv("TZ=UTC0") != 0) 
+                               fatalError(memory_exhausted);
+                       break;
+               case 'd':
+                       use_arg = 1;
+                       if ((date_str != NULL) || ((date_str = optarg) == NULL))
+                               usage(date_usage);
+                       break;
+               default:
+                       usage(date_usage);
                }
-               i--;
-               argv++;
        }
 
+       if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
+               date_fmt = &argv[optind][1];   /* Skip over the '+' */
+       else if (date_str == NULL) {
+               set_time = 1;
+               date_str = argv[optind];
+       } else {
+               usage(date_usage);
+       }
 
        /* Now we have parsed all the information except the date format
           which depends on whether the clock is being set or read */
index 6e279d1c6d332431249039bc7909ed76be635049..387ea3fefc5673427a194051800085dd0e78f8f2 100644 (file)
@@ -45,23 +45,24 @@ echo_main(int argc, char** argv)
        int nflag = 0;
        int eflag = 0;
 
-       ap = argv;
-       if (argc)
-               ap++;
-       while ((p = *ap) != NULL && *p == '-') {
-               if (strcmp(p, "-n")==0) {
+
+       while ((c = getopt(argc, argv, "neE")) != EOF) {
+               switch (c) {
+               case 'n': 
                        nflag = 1;
-               } else if (strcmp(p, "-e")==0) {
+                       break;
+               case 'e':
                        eflag = 1;
-               } else if (strcmp(p, "-E")==0) {
+                       break;
+               case 'E':
                        eflag = 0;
+                       break;
+               default: 
+                       usage(uname_usage);
                }
-               else if (strncmp(p, "--", 2)==0) {
-                       usage( uname_usage);
-               } 
-               else break;
-               ap++;
        }
+
+       ap = &argv[optind];
        while ((p = *ap++) != NULL) {
                while ((c = *p++) != '\0') {
                        if (c == '\\' && eflag) {
diff --git a/date.c b/date.c
index bc6d131370620988992f0938afaa8b7e8417f37a..3ede1237ca84ec4891152df6e58562f22abc8bca 100644 (file)
--- a/date.c
+++ b/date.c
@@ -159,7 +159,7 @@ int date_main(int argc, char **argv)
        char *date_str = NULL;
        char *date_fmt = NULL;
        char *t_buff;
-       int i;
+       char c;
        int set_time = 0;
        int rfc822 = 0;
        int utc = 0;
@@ -168,49 +168,39 @@ int date_main(int argc, char **argv)
        struct tm tm_time;
 
        /* Interpret command line args */
-       i = --argc;
-       argv++;
-       while (i > 0 && **argv) {
-               if (**argv == '-') {
-                       while (i > 0 && *++(*argv))
-                               switch (**argv) {
-                               case 'R':
-                                       rfc822 = 1;
-                                       break;
-                               case 's':
-                                       set_time = 1;
-                                       if (date_str != NULL)
-                                               usage(date_usage);
-                                       date_str = *argv;
-                                       break;
-                               case 'u':
-                                       utc = 1;
-                                       if (putenv("TZ=UTC0") != 0) 
-                                               fatalError(memory_exhausted);
-                                       break;
-                               case 'd':
-                                       use_arg = 1;
-                                       if (date_str != NULL)
-                                               usage(date_usage);
-                                       date_str = *argv;
-                                       break;
-                               case '-':
-                                       usage(date_usage);
-                               }
-               } else {
-                       if ((date_fmt == NULL) && (**argv == '+'))
-                               date_fmt = *argv + 1;   /* Skip over the '+' */
-                       else if (date_str == NULL) {
-                               set_time = 1;
-                               date_str = *argv;
-                       } else {
+       while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
+               switch (c) {
+               case 'R':
+                       rfc822 = 1;
+                       break;
+               case 's':
+                       set_time = 1;
+                       if ((date_str != NULL) || ((date_str = optarg) == NULL))
                                usage(date_usage);
-                       }
+                       break;
+               case 'u':
+                       utc = 1;
+                       if (putenv("TZ=UTC0") != 0) 
+                               fatalError(memory_exhausted);
+                       break;
+               case 'd':
+                       use_arg = 1;
+                       if ((date_str != NULL) || ((date_str = optarg) == NULL))
+                               usage(date_usage);
+                       break;
+               default:
+                       usage(date_usage);
                }
-               i--;
-               argv++;
        }
 
+       if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
+               date_fmt = &argv[optind][1];   /* Skip over the '+' */
+       else if (date_str == NULL) {
+               set_time = 1;
+               date_str = argv[optind];
+       } else {
+               usage(date_usage);
+       }
 
        /* Now we have parsed all the information except the date format
           which depends on whether the clock is being set or read */
diff --git a/dmesg.c b/dmesg.c
index df2bce7133b6afb76ed25940560c7cfce63b7f2d..961e532b0b46425877e68a2a9e4ddca75c327281 100644 (file)
--- a/dmesg.c
+++ b/dmesg.c
@@ -44,50 +44,36 @@ static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n"
 
 int dmesg_main(int argc, char **argv)
 {
-       char *buf;
+       char *buf, c;
        int bufsize = 8196;
        int i;
        int n;
        int level = 0;
        int lastc;
        int cmd = 3;
-       int stopDoingThat;
 
-       argc--;
-       argv++;
-
-       /* Parse any options */
-       while (argc && **argv == '-') {
-               stopDoingThat = FALSE;
-               while (stopDoingThat == FALSE && *++(*argv)) {
-                       switch (**argv) {
-                       case 'c':
-                               cmd = 4;
-                               break;
-                       case 'n':
-                               cmd = 8;
-                               if (--argc == 0)
-                                       goto end;
-                               level = atoi(*(++argv));
-                               if (--argc > 0)
-                                       ++argv;
-                               stopDoingThat = TRUE;
-                               break;
-                       case 's':
-                               if (--argc == 0)
-                                       goto end;
-                               bufsize = atoi(*(++argv));
-                               if (--argc > 0)
-                                       ++argv;
-                               stopDoingThat = TRUE;
-                               break;
-                       default:
-                               goto end;
-                       }
+       while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
+               switch (c) {
+               case 'c':
+                       cmd = 4;
+                       break;
+               case 'n':
+                       cmd = 8;
+                       if (optarg == NULL)
+                               usage(dmesg_usage);
+                       level = atoi(optarg);
+                       break;
+               case 's':
+                       if (optarg == NULL)
+                               usage(dmesg_usage);
+                       bufsize = atoi(optarg);
+                       break;
+               default:
+                       usage(dmesg_usage);
                }
-       }
+       }                       
 
-       if (argc > 1) {
+       if (optind < argc) {
                goto end;
        }
 
diff --git a/echo.c b/echo.c
index 6e279d1c6d332431249039bc7909ed76be635049..387ea3fefc5673427a194051800085dd0e78f8f2 100644 (file)
--- a/echo.c
+++ b/echo.c
@@ -45,23 +45,24 @@ echo_main(int argc, char** argv)
        int nflag = 0;
        int eflag = 0;
 
-       ap = argv;
-       if (argc)
-               ap++;
-       while ((p = *ap) != NULL && *p == '-') {
-               if (strcmp(p, "-n")==0) {
+
+       while ((c = getopt(argc, argv, "neE")) != EOF) {
+               switch (c) {
+               case 'n': 
                        nflag = 1;
-               } else if (strcmp(p, "-e")==0) {
+                       break;
+               case 'e':
                        eflag = 1;
-               } else if (strcmp(p, "-E")==0) {
+                       break;
+               case 'E':
                        eflag = 0;
+                       break;
+               default: 
+                       usage(uname_usage);
                }
-               else if (strncmp(p, "--", 2)==0) {
-                       usage( uname_usage);
-               } 
-               else break;
-               ap++;
        }
+
+       ap = &argv[optind];
        while ((p = *ap++) != NULL) {
                while ((c = *p++) != '\0') {
                        if (c == '\\' && eflag) {
index df2bce7133b6afb76ed25940560c7cfce63b7f2d..961e532b0b46425877e68a2a9e4ddca75c327281 100644 (file)
@@ -44,50 +44,36 @@ static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n"
 
 int dmesg_main(int argc, char **argv)
 {
-       char *buf;
+       char *buf, c;
        int bufsize = 8196;
        int i;
        int n;
        int level = 0;
        int lastc;
        int cmd = 3;
-       int stopDoingThat;
 
-       argc--;
-       argv++;
-
-       /* Parse any options */
-       while (argc && **argv == '-') {
-               stopDoingThat = FALSE;
-               while (stopDoingThat == FALSE && *++(*argv)) {
-                       switch (**argv) {
-                       case 'c':
-                               cmd = 4;
-                               break;
-                       case 'n':
-                               cmd = 8;
-                               if (--argc == 0)
-                                       goto end;
-                               level = atoi(*(++argv));
-                               if (--argc > 0)
-                                       ++argv;
-                               stopDoingThat = TRUE;
-                               break;
-                       case 's':
-                               if (--argc == 0)
-                                       goto end;
-                               bufsize = atoi(*(++argv));
-                               if (--argc > 0)
-                                       ++argv;
-                               stopDoingThat = TRUE;
-                               break;
-                       default:
-                               goto end;
-                       }
+       while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
+               switch (c) {
+               case 'c':
+                       cmd = 4;
+                       break;
+               case 'n':
+                       cmd = 8;
+                       if (optarg == NULL)
+                               usage(dmesg_usage);
+                       level = atoi(optarg);
+                       break;
+               case 's':
+                       if (optarg == NULL)
+                               usage(dmesg_usage);
+                       bufsize = atoi(optarg);
+                       break;
+               default:
+                       usage(dmesg_usage);
                }
-       }
+       }                       
 
-       if (argc > 1) {
+       if (optind < argc) {
                goto end;
        }