]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
hwclock: improve coding style
authorSami Kerola <kerolasa@iki.fi>
Sun, 24 Jul 2016 21:33:01 +0000 (22:33 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sat, 4 Feb 2017 23:39:38 +0000 (23:39 +0000)
Make string constants to be symbolical declarations.  Use longer variable
name for rtc and cmos function pointer values.  Exclude code that is
architecture specific with preprocessor directives.  And remove message
duplication.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/pathnames.h
sys-utils/hwclock-cmos.c
sys-utils/hwclock-rtc.c

index a5e8507e01469c7d96b41da5160609fcf4612aba..de6abe63e602cb1bfa374430936bef0ed974325c 100644 (file)
 /* ctrlaltdel paths */
 #define _PATH_PROC_CTRL_ALT_DEL        "/proc/sys/kernel/ctrl-alt-del"
 
+/* hwclock-cmos paths */
+#define _PATH_DEV_PORT         "/dev/port"
+#define _PATH_PROC_CPUINFO     "/proc/cpuinfo"
+
 #endif /* PATHNAMES_H */
 
index 30f8765a572e1683c5a7fb33678037da37916d86..284077eb822adcea2164dc93ed37f888a5ee2009 100644 (file)
@@ -54,6 +54,7 @@
 
 #include "c.h"
 #include "nls.h"
+#include "pathnames.h"
 
 #if defined(__i386__) || defined(__x86_64__)
 # ifdef HAVE_SYS_IO_H
@@ -154,12 +155,12 @@ static int is_in_cpuinfo(char *fmt, char *str)
 {
        FILE *cpuinfo;
        char field[256];
-       char format[256];
+       char format[sizeof(field)];
        int found = 0;
 
        sprintf(format, "%s : %s", fmt, "%255s");
 
-       cpuinfo = fopen("/proc/cpuinfo", "r");
+       cpuinfo = fopen(_PATH_PROC_CPUINFO, "r");
        if (cpuinfo) {
                do {
                        if (fscanf(cpuinfo, format, field) == 1) {
@@ -525,9 +526,7 @@ static int read_hardware_clock_cmos(const struct hwclock_control *ctl
                                    __attribute__((__unused__)), struct tm *tm)
 {
        bool got_time = FALSE;
-       unsigned char status, pmbit;
-
-       status = pmbit = 0;     /* just for gcc */
+       unsigned char status = 0, pmbit = 0;
 
        while (!got_time) {
                /*
@@ -644,8 +643,8 @@ static int get_permissions_cmos(void)
        int rc;
 
        if (use_dev_port) {
-               if ((dev_port_fd = open("/dev/port", O_RDWR)) < 0) {
-                       warn(_("cannot open %s"), "/dev/port");
+               if ((dev_port_fd = open(_PATH_DEV_PORT, O_RDWR)) < 0) {
+                       warn(_("cannot open %s"), _PATH_DEV_PORT);
                        rc = 1;
                } else
                        rc = 0;
@@ -664,7 +663,7 @@ static int get_permissions_cmos(void)
        return rc ? 1 : 0;
 }
 
-static struct clock_ops cmos = {
+static struct clock_ops cmos_interface = {
        N_("Using direct I/O instructions to ISA clock."),
        get_permissions_cmos,
        read_hardware_clock_cmos,
@@ -678,11 +677,11 @@ static struct clock_ops cmos = {
  */
 struct clock_ops *probe_for_cmos_clock(void)
 {
-       int have_cmos =
+       static const int have_cmos =
 #if defined(__i386__) || defined(__alpha__) || defined(__x86_64__)
            TRUE;
 #else
            FALSE;
 #endif
-       return have_cmos ? &cmos : NULL;
+       return have_cmos ? &cmos_interface : NULL;
 }
index 5e11aeebd32cdbabd5c2a1ed3914c1656a656470..f54e2633cbef11192485c9d01c2e74a8687f3625 100644 (file)
@@ -38,6 +38,7 @@
 # include <asm/rtc.h>
 #endif
  */
+#ifdef __sparc__
 /* The following is roughly equivalent */
 struct sparc_rtc_time
 {
@@ -49,9 +50,9 @@ struct sparc_rtc_time
        int month;      /* Month of year        1-12 */
        int year;       /* Year                 0-99 */
 };
-
 #define RTCGET _IOR('p', 20, struct sparc_rtc_time)
 #define RTCSET _IOW('p', 21, struct sparc_rtc_time)
+#endif
 
 /*
  * struct rtc_time is present since 1.3.99.
@@ -374,7 +375,7 @@ static int get_permissions_rtc(void)
        return 0;
 }
 
-static struct clock_ops rtc = {
+static struct clock_ops rtc_interface = {
        N_("Using the rtc interface to the clock."),
        get_permissions_rtc,
        read_hardware_clock_rtc,
@@ -385,14 +386,14 @@ static struct clock_ops rtc = {
 /* return &rtc if /dev/rtc can be opened, NULL otherwise */
 struct clock_ops *probe_for_rtc_clock(const struct hwclock_control *ctl)
 {
-       int rtc_fd = open_rtc(ctl);
-       if (rtc_fd >= 0)
-               return &rtc;
-       if (ctl->debug)
-               warn(_("cannot open rtc device"));
-       return NULL;
+       const int rtc_fd = open_rtc(ctl);
+
+       if (rtc_fd < 0)
+               return NULL;
+       return &rtc_interface;
 }
 
+#ifdef __alpha__
 /*
  * Get the Hardware Clock epoch setting from the kernel.
  */
@@ -478,3 +479,4 @@ int set_epoch_rtc(const struct hwclock_control *ctl)
 
        return 0;
 }
+#endif /* __alpha__ */