]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
hwclock: use exclusive_option()
authorSami Kerola <kerolasa@iki.fi>
Thu, 14 Jun 2012 18:58:12 +0000 (20:58 +0200)
committerSami Kerola <kerolasa@iki.fi>
Sun, 17 Jun 2012 15:59:58 +0000 (17:59 +0200)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
hwclock/hwclock.c

index bf1b4beda61b5ee5af9216adc16696203a031703..10ef8da428ce8c232600f4ba3a9ddece1274a3e2 100644 (file)
 #include <time.h>
 #include <unistd.h>
 
+#define OPTUTILS_EXIT_CODE EX_USAGE
+
 #include "c.h"
 #include "clock.h"
 #include "closestream.h"
 #include "nls.h"
+#include "optutils.h"
 #include "pathnames.h"
 #include "strutils.h"
 
+#define EXCL_ERROR "--{adjust,getepoch,hctosys,predict,set,setepoch,show,systohc,systz}"
 #ifdef HAVE_LIBAUDIT
 #include <libaudit.h>
 static int hwaudit_fd = -1;
@@ -1448,6 +1452,29 @@ int main(int argc, char **argv)
        bool permitted;         /* User is permitted to do the function */
        int rc, c;
 
+       enum {
+               EXCL_NONE,
+
+               EXCL_ADJFILE,
+               EXCL_NO_AJDFILE,
+
+               EXCL_LOCALTIME,
+               EXCL_UTC,
+
+               EXCL_ADJUST,
+               EXCL_GETEPOCH,
+               EXCL_HCTOSYS,
+               EXCL_PREDICT,
+               EXCL_SET,
+               EXCL_SETEPOCH,
+               EXCL_SHOW,
+               EXCL_SYSTOHC,
+               EXCL_SYSTZ
+       };
+       int excl_adj = EXCL_NONE;
+       int excl_utc_local = EXCL_NONE;
+       int excl_action = EXCL_NONE;
+
        /* Variables set by various options; show may also be set later */
        /* The options debug, badyear and epoch_option are global */
        bool show, set, systohc, hctosys, systz, adjust, getepoch, setepoch,
@@ -1542,18 +1569,23 @@ int main(int argc, char **argv)
                        break;
                case 'a':
                        adjust = TRUE;
+                       exclusive_option(&excl_action, EXCL_ADJUST, EXCL_ERROR);
                        break;
                case 'r':
                        show = TRUE;
+                       exclusive_option(&excl_action, EXCL_SHOW, EXCL_ERROR);
                        break;
                case 's':
                        hctosys = TRUE;
+                       exclusive_option(&excl_action, EXCL_HCTOSYS, EXCL_ERROR);
                        break;
                case 'u':
                        utc = TRUE;
+                       exclusive_option(&excl_utc_local, EXCL_UTC, "--{utc,localtime}");
                        break;
                case 'w':
                        systohc = TRUE;
+                       exclusive_option(&excl_action, EXCL_SYSTOHC, EXCL_ERROR);
                        break;
 #ifdef __alpha__
                case 'A':
@@ -1571,20 +1603,25 @@ int main(int argc, char **argv)
 #endif
                case OPT_SET:
                        set = TRUE;
+                       exclusive_option(&excl_action, EXCL_SET, EXCL_ERROR);
                        break;
 #ifdef __linux__
                case OPT_GETEPOCH:
                        getepoch = TRUE;
+                       exclusive_option(&excl_action, EXCL_GETEPOCH, EXCL_ERROR);
                        break;
                case OPT_SETEPOCH:
                        setepoch = TRUE;
+                       exclusive_option(&excl_action, EXCL_SETEPOCH, EXCL_ERROR);
                        break;
 #endif
                case OPT_NOADJFILE:
                        noadjfile = TRUE;
+                       exclusive_option(&excl_adj, EXCL_NO_AJDFILE, "--{adjfile,noadjfile}");
                        break;
                case OPT_LOCALTIME:
                        local_opt = TRUE;       /* --localtime */
+                       exclusive_option(&excl_utc_local, EXCL_LOCALTIME, "--{utc,localtime}");
                        break;
                case OPT_BADYEAR:
                        badyear = TRUE;
@@ -1604,12 +1641,15 @@ int main(int argc, char **argv)
                        break;
                case OPT_ADJFILE:
                        adj_file_name = optarg; /* --adjfile */
+                       exclusive_option(&excl_adj, EXCL_ADJFILE, "--{adjfile,noadjfile}");
                        break;
                case OPT_SYSTZ:
                        systz = TRUE;           /* --systz */
+                       exclusive_option(&excl_action, EXCL_SYSTZ, EXCL_ERROR);
                        break;
                case OPT_PREDICT_HC:
                        predict = TRUE;         /* --predict-hc */
+                       exclusive_option(&excl_action, EXCL_PREDICT, EXCL_ERROR);
                        break;
 #ifdef __linux__
                case 'f':
@@ -1644,34 +1684,10 @@ int main(int argc, char **argv)
                      argc);
        }
 
-       if (show + set + systohc + hctosys + systz + adjust + getepoch
-           + setepoch + predict > 1) {
-               warnx(_("You have specified multiple functions.\n"
-                       "You can only perform one function at a time."));
-               hwclock_exit(EX_USAGE);
-       }
-
-       if (utc && local_opt) {
-               warnx(_("The --utc and --localtime options "
-                       "are mutually exclusive.  You specified both."));
-               hwclock_exit(EX_USAGE);
-       }
-
-       if (adjust && noadjfile) {
-               warnx(_("The --adjust and --noadjfile options "
-                       "are mutually exclusive.  You specified both."));
-               hwclock_exit(EX_USAGE);
-       }
-
-       if (adj_file_name && noadjfile) {
-               warnx(_("The --adjfile and --noadjfile options "
-                       "are mutually exclusive.  You specified both."));
-               hwclock_exit(EX_USAGE);
-       }
        if (!adj_file_name)
                adj_file_name = _PATH_ADJPATH;
 
-       if (noadjfile && !(utc || local_opt)) {
+       if (noadjfile && !excl_utc_local) {
                warnx(_("With --noadjfile, you must specify "
                        "either --utc or --localtime"));
                hwclock_exit(EX_USAGE);